The useradd command Explained
Creating new user accounts is an essential task for effective user management. The useradd command adds a new user account on Linux. It supports many options to customize the user account. Understanding these options helps you to create user accounts for specific requirements. This tutorial explains how to use the useradd command and its options through various examples.
The useradd command syntax
The useradd command uses the following syntax.
useradd [options] username
The above command creates a new user account and adds its entry at the end of the /etc/passwd file. The username is the login name you assign to the user account. If you do not specify any option, it uses the default values of all compulsory options. The /etc/default/useradd file stores the default values of all mandatory options.
Example
- Add a new user account.
- Do not use any option with the useradd command.
- View the last entry of the /etc/passwd file.
- List the default entries of the /etc/default/useradd file.
- Verify the useradd command uses the default value to add the user account.
#useradd user1 #tail -1 /etc/passwd #cat /etc/default/useradd

The useradd command options
The useradd command supports options in two formats: short and long. The short format uses a single letter followed by a hyphen. The extended format uses a word or string followed by double hyphens.
The -b (--base-dir) option
This option sets the base directory for other options. Other options use this value to configure their parameters. For example, the -d option creates the user's home directory in this directory. If we do not use this option, the useradd command uses the base directory specified by the HOME variable in the /etc/default/useradd file.
In the previous example, we added the user account with default values. With the default values, the useradd command creates the home directory in the /home directory.
Example
- Create a new directory.
- Add a new user account.
- Use the -b option and specify the new directory path as the argument.
- Verify the new user gets its home directory in the new directory instead of the /home directory.
#mkdir /userdir #useradd -b /userdir user2 #ls /home #ls /userdir

The -c (--comment) option
The -c option adds a short description or full name of the user.
Example
- Add a new user account
- Use the -c option to set the full name of the user
- List the user entry to verify the comment
#useradd -c 'john rambo' user3 #tail -1 /etc/passwd

The -d (--home-dir) option
The -d option customizes the home directory. The user's home directory contains default profile files. The useradd command copies these files from the /etc/skel directory to the user's home directory. If you specify an existing directory path with the -d option, the useradd command does not copy profile files in the home directory. If you specify a nonexisting directory path, the useradd command creates the specified directory and copies profile files in this directory.
Example
- Add a new user account.
- Use the -d option and specify a nonexisting directory path.
- Create a new directory.
- Add a new user account.
- Use the -d option and specify the new directory's path.
- List both users' home directories.
#useradd -d /user5 user5 #mkdir /user6 #useradd -d /user6 user6 #ls -a /user5 $ls -a /user6 #tail -2 /etc/passwd

The -D option
The -D option lists the default values for the options. The /etc/default/useradd file saves the default values.
#useradd -D

The -e (--expiredate) option
This option sets the date on which the user account will be disabled. It accepts the date in the format YYYY-MM-DD. If we do not use this option, it uses the default expiry date specified by the EXPIRE variable in the /etc/default/useradd file or an empty string (no expiry) by default.

The -f (--inactive) option
This option configures the 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. If we do not use this option, it will use the default inactivity period specified by the INACTIVE variable in /etc/default/useradd, or -1 (Never) by default.

The -g (--gid) option
The useradd command automatically creates the primary group for the user. By default, it uses the username to choose the group name. It uses the following two steps to select the group ID.
- It checks the value of the GROUP variable in the /etc/default/useradd file. This variable defines the first group ID the useradd command can use for a group.
- It checks the /etc/group for already used group IDs and selects the next available group ID in ascending order. For example, if the value of the GROUP variable is 100 and the /etc/group file has an entry for the group ID 101, it will use 102.
This option allows us to customize the primary group name or group ID. If we use this option, we must create the group first. The group name or number must refer to an already existing group.

The -G (--groups) option
The secondary groups are optional. The useradd command does not create them. We must manually add them. The groupadd command adds new group accounts. This option allows us to add the user to secondary groups. 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 -h (--help) option
This option displays the help message. It lists all options and their meanings.

The -k (--skel) option
This option copies profile files from the given directory instead of the default /etc/skel/ directory. The useradd command copies profile files in the user's home directory. If we use this option, we must allow the useradd command to create the user's home directory. We must use this option with the -m (or --create-home) option.

The -m (--create-home) option
The variable CREATE_HOME in the /etc/login.defs controls the home directory creation process. The default value of it is Yes. If we change it to No, the useradd command does not create the home directory. This option allows us to create the user's home directory when the CREATE_HOME variable is not enabled.

The -M (--no-create-home) option
If we use this option, the useradd command does not create the user's home directory. However, it adds the home directory path to the user account's entry.

The -N (--no-user-group) option
If we use this option, the useradd command does not create a group with the same name as the user but adds the user to the group specified by the -g option or the GROUP variable in /etc/default/useradd.
The -o (--non-unique) option
This option allows us to create a user account with a duplicate (non-unique) UID. This option is only valid in combination with the -u option.
The -r (--system) option
This option creates a system account. System accounts have no aging policy. The useradd command uses the value of the SYS_UID_MIN-SYS_UID_MAX range defined in the /etc/login.defs file to choose their numeric identities.

The -s (--shell) option
This option allows us to select a default shell for the user account. By default, the useradd command leaves this field blank, which causes the system to select the default login shell specified by the SHELL variable in the /etc/default/useradd file.

The -u (--uid) option
This option allows us to select a custom user ID. We must choose an ID that is unique and non-negative. By default, the useradd command does not allow two user accounts to use the same user ID. If we want to assign the same user ID to two user accounts, we must use the -o option with this option.

Exit values of the useradd command
The following table lists the exit values of the useradd command.
| 0 | success |
| 1 | can't update password file |
| 2 | invalid command syntax |
| 3 | invalid argument to an option |
| 4 | UID already in use (and no -o) |
| 6 | specified group doesn't exist |
| 9 | username already in use |
| 10 | can't update group file |
| 12 | can't create the home directory |
| 14 | can't update SELinux user mapping |
This tutorial is part of the tutorial series 'Linux User and Group Management Explained with Examples'. Other parts of this series are the following.
Chapter 01 Difference between the root user and super (sudo) user
Chapter 02 Linux User Management Explained with Examples
Chapter 03 Linux Group Management Explained with Examples
Chapter 04 Password aging policy Explained with chage command
Chapter 05 The /etc/group File Explained
Chapter 06 The /etc/gshadow File Explained
Chapter 07 The /etc/passwd file in Linux Explained with Examples
Chapter 08 The /etc/shadow file in Linux Explained with Examples
Chapter 09 The useradd command Explained
Chapter 10 The gpasswd command Explained
Chapter 11 The chage command Examples and Usages
Conclusion
User management is a fundamental aspect of system administration. It involves creating, managing, modifying, and deleting user accounts. The useradd command adds new accounts. It supports various options that allow us to create user accounts for particular requirements. Learning these options helps manage user accounts effectively.
By ComputerNetworkingNotes Updated on 2026-04-12