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 these two commands in feature-wise. Adduser command is friendlier and some Debian/Ubuntu administrators love using it.
Table of Contents
Useradd Command and Syntax
You should run
useradd command either with root or sudo user privilege. The users can be added simply by giving the username followed by the command.
# useradd <username>
Let's check how to use
useradd command and its available options.
1) Create a new user
Let's check how to create a new user 'bill' and set a password with. The command adds an entry to the /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files.
# useradd bill # passwd bill
2) Add a new user and create a home directory
To create a user with a home directory use
# useradd -m bill
This creates a user with home directory as
/home/bill and copies files from /etc/skel directory to the user's home directory.
To create a user with a specific home directory use
-d option. Make sure the home directory already exists before running this command.
# useradd -m -d /accounts/bill bill
Output root@li2074-146:~# useradd -m -d /dev/bill bill root@li2074-146:~# ls -al /dev/bill 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 root@li2074-146:~#
3) Add Full name to the user
Full name of the user or the short description about the user (a comment) is provided with -c option.
# useradd -c "Bill Drape" bill
This comment can be seen in the user's entry in /etc/passwd file
# grep bill /etc/passwd bill:x:504:505:Bill Drape:/home/bill:/bin/bash
4) Creating User with Specify UID
The user will be created with the UID specified with -u option as:
# 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.
5) Define User's Shell
By default the users' shell is /bin/bash (as seen in the /etc/passwd file in previous output). This behavior can be changed. We can specify the shell associated with user's account with -s option:
# useradd -s /bin/tcsh bill # grep bill /etc/passwd bill:x:504:505::/home/bill:/bin/tcsh
6) Change Home Directory
The home directories of users are by default located in /home directory. /home is the base directory where all the home directories reside. The base directory can be changed with -b option as:
# useradd -b /mnt bill # grep bill /etc/passwd bill:x:504:505::/mnt/bill:/bin/bash
/mnt directory is provided as the base directory here. So the home directory of bill user is created as /mnt/bill. The home directory can be provided manually with -d option, which by default is created by appending the LOGIN name to BASE_DIR. The home directory provided with -d will be created automatically and must not exist at the time of user creation.
# useradd -d /mnt/billhome bill # grep bill /etc/passwd bill:x:504:505::/mnt/billhome:/bin/bash
# ls -a /mnt/billhome/ . .. .bash_logout .bash_profile .bashrc .emacs .mozilla
7) Disabling the account
The user accounts can be disabled with -e option on a certain date. The format of the date is YYYY-MM-DD.
# useradd -e 2013-06-23 bill
This can be checked in the last field of /etc/shadow file.
# grep bill /etc/shadow bill:!!:15813:0:99999:7::15879:
This last field (15879) expresses expiration date in terms of number of days since Jan 1, 1970 the account is disabled. The -f option provides the number of days after a password expires until the account is permanently disabled.
# useradd -f 20 bill # grep bill /etc/shadow bill:!!:15813:0:99999:7:20::
As shown in shadow file, the number of days is set to 20 (in second last column).
8) Creating a User and adding to 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.
# useradd -g xero bill
# 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 for providing supplementary groups which a user is also member of.
# useradd -G g1,g2,g3 bill # 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 add to multiple groups. The command will create a new user named 'bill' with primary group 'teams' and secondary groups 'admin' and 'apache'.
# useradd -g teams -G admin,apache bill
9) Duplicate UID
The -o option allows the creation of a user account with a duplicate (non-unique) UID. The UID must be provided with -u option. If you try giving a UID without -o option, error is displayed.
# useradd -u 503 bill useradd: UID 503 is not unique
# useradd -o -u 503 bill
# grep 503 /etc/passwd jenny:x:503:503::/home/jenny:/bin/bash bill:x:503:505::/home/bill:/bin/bash
Here we can see that there exist two accounts with 503 ID.
10) Create System Account
A system account can be created with -r option. 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.
# useradd -r bill # 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.
11) Add user without home directory
If you wish not to create a home directory by itself, -M option can be given.
# useradd -M bill # grep bill /etc/passwd bill:x:504:505::/home/bill:/bin/bash
# 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.
12) Set Password with useradd
The user's password is disabled by default. It must be manually specified later with 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).
# useradd -p '$1$ZsQB81$RCCVDuI5VR6q905nUPz4a/' bill
13) 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:
$ useradd -D GROUP=100 HOME=/home INACTIVE=-1 EXPIRE= SHELL=/bin/sh SKEL=/etc/skel CREATE_MAIL_SPOOL=no
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.