10+ useradd Command with Examples in Linux

This tutorial explains Linux “useradd” command, options and its usage with examples.

useradd – Create a new user or update default new user information

Description :

useradd is a low-level utility for adding users to a system. In general, the more friendly adduser command should be used instead.

Your operating system may come with a slightly different version of useradd; check your documentation before using it to create new accounts. This documentation refers to some options frequently used on Debian-based variants of Linux, but is representative of useradd’s general use.

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. Depending on command line options, the useradd command will update system files and may also create the new user’s home directory and copy initial files.

Usage :


useradd [-c comment] [-d home_dir] [-e expire_date] [-f inactive_time] [-g initial_group] [-G group[,…]] [-m [-k skeleton_dir] | -M] [-n] [-o] [-p passwd] [-r] [-s shell] [-u uid] login


useradd -D [-g default_group] [-b default_home] [-e default_expire_date] [-f default_inactive] [-s default_shell]

Options :

-c comment
The new user’s password file comment field.
-d home_dir
The new user will be created using home_dir as the value for the user’s login directory. The default is to append the login name to default_home and use that as the login directory name.
-e expire_date
The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD.
-f inactive_days
The number of days after a password expires until the account is permanently disabled. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature. The default value is -1.
-g initial_group
The group name or number of the user’s initial login group. The group name must exist. A group number must refer to an already existing group. The default group number is 1 or whatever is specified in /etc/default/useradd.
-G group,[…]
A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. The default is for the user to belong only to the initial group.
The user’s home directory will be created if it does not exist. The files contained in skeleton_dir will be copied to the home directory if the -k option is used, otherwise the files contained in /etc/skel will be used instead. Any directories contained in skeleton_dir or /etc/skel will be created in the user’s home directory as well. The -k option is only valid in conjunction with the -m option. The default is to not create the directory and to not copy any files.
The user home directory will not be created, even if the system wide settings from /etc/login.defs is to create home dirs.
A group having the same name as the user being added to the system will be created by default. This option will turn off this Red Hat Linux specific behavior.
Allow create user with duplicate (non-unique) UID.
-p passwd
The encrypted password, as returned by crypt(3). The default is to disable the account.
This flag is used to create a system account. That is, a user with a UID lower than the value of UID_MIN defined in /etc/login.defs and whose password does not expire. Note that useradd will not create a home directory for such an user, regardless of the default setting in /etc/login.defs. You have to specify -m option if you want a home directory for a system account to be created. This is an option added by Red Hat.
-s shell
The name of the user’s login shell. The default is to leave this field blank, which causes the system to select the default login shell.
-u uid
The numerical value of the user’s ID. This value must be unique, unless the -o option is used. The value must be non-negative. The default is to use the smallest ID value greater than 99 and greater than every other user. Values between 0 and 99 are typically reserved for system accounts.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Changing the default values

When invoked with the -D option, useradd will either display the current default values, or update the default values from the command line. The valid options are

-b default_home
The initial path prefix for a new user’s home directory. The user’s name will be affixed to the end of default_home to create the new directory name if the -d option is not used when creating a new account.
-e default_expire_date
The date on which the user account is disabled.
-f default_inactive
The number of days after a password has expired before the account will be disabled.
-g default_group
The group name or ID for a new user’s initial group. The named group must exist, and a numerical group ID must have an existing entry .
-s default_shell
The name of the new user’s login shell. The named program will be used for all future new user accounts.

If no options are specified, useradd displays the current default values.

Examples :


1. Display the defaults for new users.

# useradd -D

2. Adding a new User

For example, to add a new user called ‘abc‘, use the following command.

# useradd abc

When we add a new user in Linux with ‘useradd‘ command it gets created in locked state and to unlock that user account, we need to set a password for that account with ‘passwd‘ command.

# passwd abc
Changing password for user abc.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

Once a new user created, it’s entry automatically added to the ‘/etc/passwd‘ file. The file is used to store users information and the entry should be.



The above entry contains a set of seven colon-separated fields, each field has it’s own meaning. Let’s see what are these fields:
Username: User login name used to login into system. It should be between 1 to 32 charcters long.
Password: User password (or x character) stored in /etc/shadow file in encrypted format.
User ID (UID): Every user must have a User ID (UID) User Identification Number. By default UID 0 is reserved for root user and UID’s ranging from 1-99 are reserved for other predefined accounts. Further UID’s ranging from 100-999 are reserved for system accounts and groups.
Group ID (GID): The primary Group ID (GID) Group Identification Number stored in /etc/group file.
User Info: This field is optional and allow you to define extra information about the user. For example, user full name. This field is filled by ‘finger’ command.
Home Directory: The absolute location of user’s home directory.
Shell: The absolute location of a user’s shell i.e. /bin/bash.

3. Create a User with Different Home Directory

By default ‘useradd‘ command creates a user’s home directory under /home directory with username.
However, this action can be changed by using ‘-d‘ option along with the location of new home directory (i.e. /data/projects). For example, the following command will create a user ‘abc1‘ with a home directory ‘/data/proj‘.

# useradd -d /data/proj abc1

4. Create a User with Specific User ID

We can create user’s with custom userid with ‘-u‘ option.

# useradd -u 999 abc2

Verification of UID assignment

# cat /etc/passwd | grep abc2

5. Create a User with Specific Group ID

Similarly, every user has its own GID (Group Identification Number). We can create users with specific group ID’s as well with -g option.

# useradd -g 500 abc3

Now, see the assigned user id and group id in ‘/etc/passwd‘ file.

# cat /etc/passwd | grep abc3

6. Add a User to Multiple Groups

Here in this example, we are adding a user ‘abc‘ into multiple groups like admins and webadmin

# useradd -G admins,webadmin abc

7. Add a User without Home Directory

To create user’s without their home directories, ‘-M‘ is used. For example, the following command will create a user ‘abc4‘ without a home directory.

# useradd -M abc4

Now, let’s verify that the user is created without home directory, using ls command.

# ls -l /home/abc4
ls: cannot access /home/abc4: No such file or directory

8. Create a User with Account Expiry Date

By default, when we add user’s with ‘useradd‘ command user account never get expires i.e their expiry date is set to 0 (means never expired). However, we can set the expiry date using ‘-e‘ option, that sets date in YYYY-MM-DD format. This is helpful for creating temporary accounts for a specific period of time. Here in this example, we create a user ‘abc5‘ with account expiry date i.e. 27th April 2014 in YYYY-MM-DD format.

# useradd -e 2014-03-27 abc5

9. Create a User with Password Expiry Date

The ‘-f‘ argument is used to define the number of days after a password expires. A value of 0 inactive the user account as soon as the password has expired. By default, the password expiry value set to -1 means never expire.

Here in this example, we will set a account password expiry date i.e. 45 days on a user ‘abc6’ using ‘-f‘ option.

# useradd -f 45 abc6

10. Change User Login Shell

Sometimes, we add users which has nothing to do with login shell or sometimes we require to assign different shells to our users. We can assign different login shells to a each user with ‘-s‘ option.

Here in this example, will add a user ‘abc‘ without login shell i.e. ‘/sbin/nologin‘ shell.

# useradd -s /sbin/nologin abc

You can check assigned shell to the user in ‘/etc/passwd‘ file.

# tail -1 /etc/passwd

11. Add a User with Specific Home Directory, Default Shell and Custom Comment

The following command will create a user ‘ravi‘ with home directory ‘/var/www/ravi‘, default shell /bin/bash and adds extra information about user.

# useradd -m -d /var/www/ravi -s /bin/bash -c "Owner" -U ravi

In the above command ‘-m -d‘ option creates a user with specified home directory and the ‘-s‘ option set the user’s default shell i.e. /bin/bash. The ‘-c‘ option adds the extra information about user and ‘-U‘ argument create/adds a group with the same name as the user.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.