How to create users in Linux with useradd

Written by
Date: 2020-05-31 15:35:00 00:00

Linux as all Unix like operating systems are multi users, that means that multiple users can log into the system, actually multiple users can be logged at the same time, all of then using the computer resources.

Users can also be asigned to groups, that gives Linux systems a lot of flexibility.

If you are on a Debian or Ubuntu machine, you will probable use adduser which works interactively and asks you questions in order to determine how to create the user, it also takes care of the group and home folder.

If you are working with another system, like Arch Linux, you will have to use useradd command instead.

Only administrators can create new users on the system, that means you need to be root or be in the sudoers file

Every new users needs three things in order to be able to log in, and work on the system.

  • A username
  • A password
  • A home folder

Some Linux distributions create the home folder automatically, but some of them does not, so to be on safe ground, use the next commands to create new users.

sudo useradd -m username

The -m option, will create the home folder at: /home/username, then set the password

sudo password username

You will have to enter the password twice.

If you want to change the default location of the home folder, use this command.

sudo useradd -m -d /some/folder username

And if you want to assign the user to an extra group.

sudo useradd -m -G wheel username

That will create the user username create the home folder at /home/username, and set the main group of the user to username, thus creating that group first, it will also add the user to the group wheel, you can add the user to multiple groups.

sudo useradd -m -G wheel printers username

You can also change the default group at the time of creating.

sudo useradd -m -g groupx username

You will usually not need anymore that those commands, but let's see some other options.

Create a user with another shell

If you do not want your user to use the default shell (usually bash)

sudo useradd -m -s /usr/bin/zsh

Create a temporary user

If you want a user set to expire at a given date

sudo useradd -m -e 2020-12-31 username

On January 1, 2021 that user will not be able to log in into the system

Create a user with no home, or right to login

You can create users that will be used by the system, and will not have a password, nor a home.

sudo useradd -r username