How to Create Users Using Useradd Command in Linux

create user using useradd command in linux

In Linux and Unix-like operating systems, the most common command to create users is 'useradd' or 'adduser'. As a system administrator, it often requires to add users, remove users, creating groups, and assign users.

There's no difference between above mentioned two commands in feature-wise. The adduser command is friendlier and some Debian/Ubuntu administrators love using it. Useradd is binary compiled with the Linux system and work as a backend for adduser.

In this tutorial, we learn how to create users in Linux from the terminal using useradd command.

Useradd Command and Syntax

The following line shows the syntax for useradd command:

useradd [OPTIONS] username

A user can be added simply by giving the username followed by the command. You should run useradd command either with root or sudo user privilege.

Let's learn more about useradd command and its options.

1) How to Create a new user in Linux

As mentioned above, to create a new user we use useradd command.

The following command creates a new user named 'bill' and the second command sets the user ('bill') a new password.

$ sudo useradd bill
$ sudo passwd bill
useradd command

This creates the user with Home directory as /home/bill and copies files from '/etc/skel' directory to the user's home directory.

$ ls -al /home/bill/
total 20
drwx------. 2 bill bill 4096 Mar  9 06:49 .
drwxr-xr-x. 4 root root 4096 Mar  9 06:49 ..
-rw-r--r--. 1 bill bill   18 Nov  8 16:21 .bash_logout
-rw-r--r--. 1 bill bill  141 Nov  8 16:21 .bash_profile
-rw-r--r--. 1 bill bill  312 Nov  8 16:21 .bashrc

This command adds an entry to the /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files.

Note: On Ubuntu and Debian related distribution useradd won't create a default home directory unless you have to specify with options. Instead, Debian recommends using adduser command.

2) Create a user with specific home directory

To create a user with a specific home directory as the value for the user login use -d option. Make sure to create the base home directory using mkdir command before running command.

Let's create the base home directory called 'devops':

$ sudo mkdir /devops

Now the following command creates the user 'bill' and sets his home directory as '/devops/billhome'.

$ sudo useradd -d /devops/billhome bill
$ sudo useradd -d /devops/billhome bill
$ ls -al /devops/billhome
total 12
drwxr-xr-x  2 bill bill  100 Dec 31 05:13 .
drwxr-xr-x 17 root root 3680 Dec 31 05:13 ..
-rw-r--r--  1 bill bill  220 Apr  4  2018 .bash_logout
-rw-r--r--  1 bill bill 3771 Apr  4  2018 .bashrc
-rw-r--r--  1 bill bill  807 Apr  4  2018 .profile

You may use a combination of both -m (create a home directory if doesn't exist) and -d (set given path as the user's login directory name) to create a user.

$ sudo useradd -m -d /home/bobhome bob

3) Creating a User with Specific User ID

By default when you create a user, it assigns next available UID defined in the login.defs file. But using -u option you can create a user with specific UID.

In the following example user bill is created with specific UID (3210).

$ useradd -u 3210 bill 
$ grep bill /etc/passwd bill:x:3210:3210::/home/bill:/bin/bash

The UID can be seen in the '/etc/passwd' file as shown in the above output.

4) Create User with Specific Login Shell

Sometimes we need to create a user with a specific login shell (like ksh, tcsh, zsh, etc) or with no shell login ('/sbin/nologin').

We can specify login shell associated with user's account with -s option as shown below:

$ useradd -s /bin/tcsh bill

You can verify login shell by grepping user name from /etc/passwd file:

$ grep bill /etc/passwd bill:x:504:505::/home/bill:/bin/tcsh

5) Create Specific Base Home Directory

By default users home directories are  located in '/home' (variable in /etc/default/useradd )directory. By using -b option you can have a different base home directory for the new user.

The following command set '/mnt' directory as the base directory and create a home directory of the user 'bill' as '/mnt/bill'.

$ useradd -b /mnt bill 
$ grep bill /etc/passwd bill:x:504:505::/mnt/bill:/bin/bash

6) Create a user with an expiry date

While creating a new user can you set an expiry date for the user account using -e option.

The following example set expiry date (format of the date is YYYY-MM-DD ) for the user 'bill'.

$ sudo useradd -e 2013-06-23 bill

This can be verified by checking in the last field of '/etc/shadow' file. The last field (15879) shows the expiration date in terms of the number of days since Jan 1, 1970 the account is disabled.

$ sudo grep bill /etc/shadow bill:!!:15813:0:99999:7::15879:

More easy way to verify user expiry date is using chage command as shown below:

$ sudo chage -l bill
Last password change                                    : Feb 10, 2013
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : Jun 23, 2013
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7

The -f option provides the number of days after a password expires until the account is permanently disabled. The value of 0 disables the account as soon as the password has expired.

$ sudo useradd -f 20 bill

As shown in the shadow file, the number of days is set to 20 (in the second last column).

$ sudo grep bill /etc/shadow bill:!!:15813:0:99999:7:20::

7) Creating a user and adding to multiple groups

Each user in Linux should stay in one primary group and zero or more secondary groups (supplementary group).

A user's primary group to which he belongs can be set with -g option. The group provided with this option must exist already.

$ sudo useradd -g xero bill
$ sudo id bill uid=504(bill) gid=505(xero) groups=505(xero) context=root:system_r:unconfined_t:SystemLow-SystemHigh

The group can be checked with id command. The -G option is used to add a user to additional groups.

$ sudo useradd -G g1,g2,g3 bill

$ sudo id bill uid=504(bill) gid=509(bill) groups=509(bill),506(g1),507(g2),508(g3) context=root:system_r:unconfined_t:SystemLow-SystemHigh

Let's check how to create a user and assign to multiple groups. The command will create a new user named 'bill' with primary group 'teams' and secondary groups 'admin' and 'apache'.

$ sudo useradd -g teams -G admin,apache bill

8) Create Duplicate User ID

The -o option allows to create a user account with a duplicate user (non-unique) ID. You must provide a UID number with -u option.

Check the following example:

$ sudo useradd -o -u 503 bill

Here we can see that there exist two accounts with 503 ID.

$ sudo grep 503 /etc/passwd 

9) Create a User as System Account

Normally, the UIDs till 500 or 1000 are reserved for system accounts and are not allocated to normal users. With -r option, an account with ID in the range of system users is given to the user being created.

$ sudo useradd -r bill 
$ sudo grep bill /etc/passwd bill:x:101:102::/home/bill:/bin/bash

Here, the home directory for the user is not created. It must be created manually or provided with -m option.

10) Create a User without Home directory

If you wish not to create a home directory by itself, -M option can be given.

$ sudo useradd -M bill 
$ sudo grep bill /etc/passwd 
$ ls /home/bill 
ls: /home/bill: No such file or directory

In the output of ls command, it can be seen that the home directory for bill user does not exist.

11) Create a User with Password

The user's password is disabled by default. You can manually set the password using passwd command. But if you want to provide the password at the time of user creation, you can use -p option.

But the password provided here must be encrypted, as returned by crypt(3).

$ sudo useradd -p '$1$ZsQB81$RCCVDuI5VR6q905nUPz4a/' bill

12) Changing the default options

When a user is added on a system, some values are taken as default, for example shell, base directory etc. The base directory is the directory where the users' home directories reside. On most of the Linux systems, home directories of users reside in /home directory.

For example, user tom's home directory will be '/home/tom' by default. So /home will be his base directory. These default options are defined in '/etc/defaults/useradd' file. These defaults can be viewed with -D option to useradd command:

$ sudo useradd -D GROUP=100 HOME=/home INACTIVE=-1 EXPIRE= SHELL=/bin/sh SKEL=/etc/skel CREATE_MAIL_SPOOL=no

The above command can be run by normal users (non root users) as well. The -D option is used not only for viewing defaults, but for changing the defaults as well.

13) Create a User with custom comment

We can provide full name or a short description about the user (a comment) while creating a user. You have to use -c option to add custom comments.

Let me show you an example:

$ sudo useradd -c "Bill Drape" bill

This comment can be seen in the user's entry in '/etc/passwd' file.

$ sudo grep bill /etc/passwd bill:x:504:505:Bill Drape:/home/bill:/bin/bash


In this tutorial, we learned how to create users in Linux and different options of useradd command. I hope you enjoyed reading and please leave your suggestions in the below comment section.

Leave a Comment