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 useradd
and 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.
The useradd
command has the following general syntax, as provided in the manpages (Command: man useradd
):
useradd [OPTIONS] LOGIN
useradd -D
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 useradd
Command
To print the defaults in use by the useradd
command, we run only the following command:
useradd -D
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 -m
/ --create-home
option.
To modify where the home directory is created, we can use some more options.
The -b
/ --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 -d
/ --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 -e
/ --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 -u
/ --uid
flag to specify the UID.
For GID/group we use the -g
/ --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 -G
/ --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,
groups delta
Here is how the command looks on Linux machine,
Conclusion
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 useradd
command.
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
man useradd
And to get basic information and help, we can call useradd
in either of the following manners:
useradd -h
useradd --help