This example will show you how to create users in Linux, looking at the two different available commands: the default utility,
useradd; and a script that acts as more friendly front-end for the default utility, which is named
For this example, Linux Mint 17.03 has been used.
1. How users are organized
The existing users of the system are registered in the file
/etc/passwd. This file defines who has legit access to the system. This is an example of a line of the file:
Which follows the following format:
usernameis the account name for the login.
passwordfield is actually not used in modern systems. The users credentials are stored in
uid(user id) and
gid(group id) are the unique identifiers of the user and the group it belongs to, respectively.
real_nameis that, the user’s real name.
home_directoryis the working directory of each user, usually
- Finally, the
command_shellis the program that is ran at login. Usually, this is the path to a shell. If not set,
It’s better not to touch manually this file to add (or modify/remove) users. To add users, we should use the methods that we will see in this tutorial.
2. Using native binary: useradd
useradd is the native, low level, binary of Linux systems. Its use is very simple:
sudo useradd [options] username # superuser privileges are needed.
So, we could create a user named
sudo useradd john_doe
Now, a new user named
john_doe has been created in the users database. We can check it in the
grep "john_doe" /etc/passwd
Which will show:
2.1. Setting a password
We have created a user without a password! We can check it in the
grep "john_doe" /etc/shadow
That exclamation mark
! means that no password is set for the user.
Setting a password for each user is not an advice, but mandatory. For this, we have two options: create the user and then set the password (with
passwd command), or specify it at creation time with
--password) option. The recommended option is the first one, since the second one has two obvious downsides:
- The password is visible in the command line.
- We are not asked for confirmation, so we won’t notice if we make a miss typing the password.
Use always the
passwd command to set the passwords. We only have to run it specifying the user, as in the following example:
sudo passwd john_doe
And we will be asked to set the password (with confirmation).
2.2. Creating the home directory
Now that we have this new user, we can try to login in the system with it:
sudo -u john_doe -i # Login with user john_doe.
But we will get an error:
sudo: unable to change directory to /home/john_doe: No such file or directory
This is because
useradd sets the home directory for new users, but it does not create it by default. We can fix it by creating manually the directory, but is better to create the home directory at user creation time. This is achieved passing the
--create-home) option to
sudo userdel john_doe # To delete it. sudo useradd john_doe -m
This will create a directory for the new user. The default behavior for this option is to create the directory with the same name as the created user, in the
2.3. Setting a different home directory
For some reason, we might want to set the home directory in a different place from
/home. This is allowed using the
--base-dir) option. For example:
sudo useradd john_doe -b /tmp
Will create the following entry in
Note that we only have specified the directory where the home directory will be placed, not the home directory name itself.
When we use this option, we also have to tell
userrad to create the home directory, as in the example of the previous section:
sudo useradd john_doe -b /tmp -m
2.4. Setting the shell
You may have noticed that the in the line for our user, the value for the shell is not set. Usually, we would want to use
/bin/bash instead of the default
/bin/sh. To specify the shell, we have to use the
sudo useradd john_doe -m -s /bin/bash
2.5. Other options
Let’s see other common options for
2.5.1. Specifying the primary group
The default behavior when creating a user is to create a group for it, with the same name, and set it as primary. But we have the option to avoid this and specify a group name (or
gid) to be the primary of the creating user. For this,
--gid) option is used, as in the following example:
sudo useradd john_doe -g developers
john_doe will be created with
developers as primary group. We can check it with the
2.5.2. Setting secondary groups
Similarly to the primary group, we may want to set secondary group(s) for a user at creation time. This time,
-groups) option has to be used, specifying the list of groups separated by commas, without whitespaces, e.g.:
sudo useradd john_doe -G developers,another_secondary
2.5.3. Setting an expiration date
This option is useful when we have to create accounts for users that we know beforehand have to have access to the system only until a certain date. For this, we have to use the
--expiredate) option, specifying the date in
YYYY-MM-DD format. Let’s see it with an example:
sudo useradd john_doe -e 2017-01-01
2.5.4. Setting personal information
Actually, we can set any type of additional comments, but this option is usually used to specify personal information, such as real name. We have to use the
--comment) option, specifying the information between quotes (single or double, doesn’t matter) if the comment contains whitespaces, e.g.:
sudo useradd john_doe -c 'John Doe'
Will generate the following entry:
3. Using a user-friendly wrapper for useradd: adduser
useradd, we have seen that creating users is not actually difficult, but, by default, it doesn’t perform some actions that can be supposed as essential, like creating the home directory. We can even create a user without a password, and do not notice it.
To make user creation easier and in a more comfortable way,
adduser was created. This is just a Perl script for an interactive use of
If we try to create a user with
sudo adduser john_doe
And we will see that, only typing that,
adduser does many things for us:
Adding user `john_doe’ …
Adding new group `john_doe’ (1001) …
Adding new user `john_doe’ (1002) with group `john_doe’ …
Creating home directory `/home/john_doe’ …
Copying files from `/etc/skel’ …
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for john_doe
Enter the new value, or press ENTER for the default
Full Name : John Doe
Room Number : 1
Work Phone : 111-111-111
Home Phone : 222-222-222
Other : 333-333-333
Is the information correct? [Y/n] Y
(In italic the values specified by hand).
That is, apart from creating the home directory and setting the password with
passwd, also allows to set personal information about the user. And also sets
/bin/bash for the shell. This is the line that has been added in
/etc/passwd for the user we have just created:
3.1. Changing the options
adduser does makes more comfortable the user creation, we can change the options. Let’s see the equivalents for
adduser that we have seen for
The format is the same as with
sudo adduser <username> [option1] <value1>...[optionN] <valueN>
- Changing the home directory:
- Changing the shell:
- Specifying the primary group:
adduser utility does not provide options for setting secondary groups and and an expiration date.
This example has shown how to create users in Linux systems, with two different commands:
adduser. As we have seen,
adduser can be considered a better (in terms of usability) option, since it performs two essential actions that
useradd does not perform by default: create a home directory, and set a password. Even if a user creation can require more options, those two are always fundamental.