How to Create Users in Linux

Useradd command is used to create users to the Linux system. It also updates the default values for adding the users. The users can be added simply by giving the username to be added as the argument to useradd command:

# useradd <username>

This will create bill user on the system. Some profile information of the user is set by default such as his/her home directory location, files in his home directory, shell etc. Below I explain some options of useradd command.

1) Full name

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

2) Specify UID

The user will be created with the UID specified with -u option as:

# useradd -u 3210 bill

# grep bill /etc/passwd

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

3) 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

4) 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

/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

# ls -a /mnt/billhome/
. .. .bash_logout .bash_profile .bashrc .emacs .mozilla

5) 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

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

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

6) User's groups

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

7) 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

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

8) 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

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

9) Do not create home directory

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

# useradd -M bill

# 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.

10) Password

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

11) Changing the default options

When a user is added on a system, some values are taken as default, for example shell, base directory etc. 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

The useradd command is a system command. Only root user can add users to the system. But 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.

Read Also:

Bobbin Zachariah 8:01 am


Your email address will not be published. Required fields are marked *

All comments are subject to moderation.