Ubuntu useradd command

Ubuntu useradd command is a traditional Linux command to create a new user in the system. It is a low level utility for adding user or change new user default information. If you are new in Ubuntu, use the upper level utility, the adduser command instead.

Here are basic useradd command information:
  • Name: useradd - create a new user or update default new user information.
  • Synopsis:
useradd [options] LOGIN

useradd -D

useradd -D [options]

When invoked without the -D option, the useradd command creates a new user account using the values specified on the command line plus the default values from the system.

Ubuntu useradd command complete options:

-b, --base-dir BASE_DIR base directory for the new user account
home directory
-c, --comment COMMENT set the GECOS field for the new user account
-d, --home-dir HOME_DIR home directory for the new user account
-D, --defaults print or save modified default useradd
configuration
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-f, --inactive INACTIVE set password inactive after expiration
to INACTIVE
-g, --gid GROUP force use GROUP for the new user account
-G, --groups GROUPS list of supplementary groups for the new
user account
-h, --help display this help message and exit
-k, --skel SKEL_DIR specify an alternative skel directory
-K, --key KEY=VALUE overrides /etc/login.defs defaults
-m, --create-home create home directory for the new user
account
-o, --non-unique allow create user with duplicate
(non-unique) UID
-p, --password PASSWORD use encrypted password for the new user
account
-r, --system create a system account
-s, --shell SHELL the login shell for the new user account
-u, --uid UID force use the UID for the new user account


Here are some examples on how to use useradd options when creating a new user account:

Example 1 - Ubuntu useradd command for creating a normal user account.

luzar@ubuntu:~$ sudo useradd -c 'Normal user' -d /home/jimi -m jimi
luzar@ubuntu:~$ sudo passwd jimi
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
luzar@ubuntu:~$ ls -l /home/

total 12
drwxr-xr-x 2 aura aura 4096 2011-05-23 22:36 aura
drwxr-xr-x 2 jimi jimi 4096 2011-05-23 23:48 jimi
drwxr-xr-x 7 luzar luzar 4096 2011-05-23 05:43 luzar
luzar@ubuntu:~$

I used -c option to add comment to the user account. The -d is a home directory name and -m is to create home directory for the new user account. Finally, create a password for the new account to enabled it.

Example 2 - Ubuntu useradd command for creating user account for a temporary stuff.

luzar@ubuntu:~$ sudo useradd -c 'Temporary stuff' -e 2011-05-01 -f 7 -d /home/alex -m alex
luzar@ubuntu:~$ sudo passwd alex
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

In the example above, I add -e option to change default values for expired date and -f for the number of days the account will be disabled after the password has expired. Then, as usual, don't forget to create a password for the new account.