How to create a user in Linux (useradd command)
When we want to create a user in Linux, we can head to our handy command prompt and use the
useradd command, an essential utility on all Linux systems, and a tool every SysAdmin, or owner of a shared computer needs to know.
We should not confuse
adduser commands. Both these commands are used to create Users. The main difference is that
adduser command sets up user folders, directories, and other necessary functions easily, whereas
useradd creates a new user without adding the directories as mentioned above and settings.
useradd command has the following general syntax, as provided in the manpages (Command:
useradd [OPTIONS] LOGIN
useradd -D [OPTIONS]
We will see how to use the
useradd command, and a few options it offers to optimize and configure the edits we will be doing.
Create new user in Linux
We can add a new user, by just specifying the username along with the
useradd command. We need to ensure the username is unique, not in use by anyone else on the system. For example, to create a user with name
delta, we will run the following command:
sudo useradd delta
After adding the user, we need to activate the user account before using it and to activate the user account we use the
passwd command with the new username as an argument, to apply a password for the user account, and to update necessary system files.
sudo passwd delta
When you run the passwd command, you will be prompted to provide a password, twice.
To check if the user has been created successfully, just run the following command, and if the user has been created, their UID, GID, and groups will be printed on the terminal.
tail -n1 /etc/passwd
Let's see what happens when we run these commands on live terminal,
Printing default options of
To print the defaults in use by the
useradd command, we run only the following command:
And we get an output like the following,
Create Home Directory for User in Linux
When we create a user, we may want to create them as a permanent addition to the system, and a part of that is giving a folder which is their storage space.
To create that folder, we can use the
To modify where the home directory is created, we can use some more options.
--base-dir option is followed by a name, which acts as the base directory, which by default is /home, as seen in the previous section (default settings).
sudo useradd -m -b /work delta
If the name for the home directory is not specified using
--home-dir, the username is concatenated with either the option to
-b, or the default value in /etc/default/useradd (usually /home).
Create a User with account expiry date in Linux
If a user account is to be created which can not be used after a certain amount of time, we can set an expiry date using the
--expiredate option of the
useradd command. The date is specified in YYYY-MM-DD format.
Once the user has been created, we run the
chage command on the user account to verify details. The
chage command is used to see the history of password changes for a user and other events like account expiry etc.
sudo useradd -e 2021-08-30 delta
Let's see the commands in action,
Create User with Custom UID, GID in Linux
By default, the largest present user ID is incremented and used as the UID/GID for any new user created in linux, but to create a user with a UID of your choice, we use the
--uid flag to specify the UID.
For GID/group we use the
--gid flag, which accepts only the GID of a group that already exists.
For example, a user with the UID 420 is created in the following manner.
sudo useradd -u 420 delta
To verify, just get the latest entry from the /etc/passwd file and the 3rd entry is the UID.
Create user and add it to multiple groups in Linux
Once a user has been created, we can add a user to a group. We can also create a user and add it to a group or multiple groups, using a simgle command too.
We use the
--groups option with the
useradd command to achieve this task. For example, if I want a user to belong to the sudo, and docker groups. I would run the following to achieve it:
sudo useradd -G sudo,docker delta
And to verify the same, I will run the following command,
Here is how the command looks on Linux machine,
This tutorial covered How to create a user in Linux, modify the default options, edit the account's expiry date, add them to groups and much more. All of this using the
If anyone wants to dive in deeper, they can always head to the center of all essential knowledge in Linux, the manpages, by running the following
And to get basic information and help, we can call
useradd in either of the following manners: