Linux is a multi-user operating system, which means multiple users can work on the system simultaneously. Every user has to be added manually and system administrators can use the useradd command to create new users on Linux and UNIX-like operating systems.
In this tutorial, we will learn about useradd command in Linux and its useful options to control the user’s default behavior.
- A computer running Linux operating system.
- A user with the sudo privileges.
- Basic understanding of Linux command line interface.
- Willingness to learn new Linux commands.
What Does Useradd Command do in Linux?
The useradd command is used for creating new users in Linux. The useradd command lets you specify certain options, such as user groups, custom user IDs, new user’s home directory, user’s login shell, password expiry date, and account expiry date when creating a user account.
Based on the option passed, useradd command updates some of the configuration files in /etc and /home directory.
Only the root user or users with the sudo privileges can create new user accounts using this command line utility.
Useradd Command Linux Syntax
The basic syntax of the useradd command:
useradd [options] new_user_name
Useradd Command Options
The following table lists the main options of the useradd commands:
|Specify the new user’s home directory.|
|Set new system default values for creating new users.|
|Specify expiry date for the new user accounts. An account does not have a default expiry date.|
|Specify primary group name for the new user accounts.|
|Specify a list of secondary user groups, which contain the user as a member.|
|Create a user's home directory, if it does not exist.|
|Specify this option to not create a user's home directory.|
|Create an account as the system account.|
|Specify the ID for the user as a numerical value.|
Which Files Useradd Command Update?
When a new user account is created using the useradd command, the command updates some or all of the following files based on the specified options:
- /etc/passwd - contains user account information.
- /etc/shadow - contains secure user account information.
- /etc/group - contains group account information.
- /etc/gshadow - contains secure group account information.
- /etc/default/useradd - contains default account creation values.
- /home - adds a directory for the new user under the home directory.
Differences between Useradd and Adduser
Both useradd and adduser are used to create new users in Linux. The useradd command is a built-in command line utility, which is compiled with the system. However, adduser is a Perl script, which uses the useradd command binaries as a base.
The adduser command is a much more friendly command. By default, it creates the home directory, groups, password, and copy /etc/skel. Whereas useradd requires specific options for each requirement.
Linux Useradd Command Examples
Let's check how to use useradd command to create new user accounts and go through some of its options with practical examples.
Create a User Account
The useradd command can add one user at a time. The user name must be unique and must be unique in the internal and external user databases ( such as NIS or LDAP).
To create a user, type useradd command followed by the specific user name. The following command to add a new user named
sudo useradd oper
You can confirm the username by filtering the username in the /etc/passwd file:
cat /etc/passwd | grep oper
By default, the user that you created in the preceding example is locked. It does not have a default password. To unlock the account, set a user password using the passwd command:
sudo passwd oper
You may create user and set a password in a single command using
Create a User with Specific Home Directory
By default, the useradd command automatically creates a home directory for new users with the given username under the /home directory. In some Linux distributions such as Ubuntu, you may not see a home directory created, in that case use
However, you can use the
-d option you can provide a specific home directory for the user:
sudo useradd -m -d /data/projects/dev devuser
Create a User with Specific User ID
Linux system assigns Unique Identification Number (UID) or user IDs to every user. By default, sequential user IDs are assigned whenever a new user is added, for example, 1001 for the first user, 1002 for the second user, 1003 for the third user, and so on.
However, you can assign custom user IDs using the
sudo useradd -u 1021 colouduser
In the preceding example, UID
1021 is assigned to the user named
clouduser. The command also created a group
clouduser with the same GID and added the user to the group.
Create User and assign to Specific Group
We can use useradd command to assign a new user to one or more groups while creating. A Linux user can be part of two group types: Primary and Secondary. When an account is created the default group is called the primary group. A user can be a member of one or more secondary groups and one primary group.
The following command creates a new user named
docuser and adds it to the secondary groups named
sudo useradd -G backup,dev,doc docuser
The following command creates a user named tom and adds to the primary group:
sudo useradd -g devs tom
For adding an existing user to a group you may use usermod command.
The following command creates a new user docmgr with doc as the primary group and backup, dev, and test as secondary groups:
sudo useradd –g doc –G backup,dev,test docmgr
Create User and set Account Expiry Date
By default, a user account never expires in Linux. However, you can use the
-e option to set the expiry date in the format YYYY-MM-DD for user accounts. This option is helpful in creating temporary accounts.
The following command creates a new user
devtemp, which will expire on the 28th of July, 2022:
sudo useradd -e 2022-07-28 devtemp
To confirm the account expiry date for a given user, use the following chage command:
sudo chage -l devtemp
Create a User with Comments
-c option can be used to add custom comments for the new user. The useradd command will update the comments in the /etc/passwd file.
The following command adds the custom comment
Temp Doc User to the user named
sudo useradd -c “Temp Doc User” doctemp
Create a User with Login Shell
In some scenarios, you may need to create a user with a specific login shell. For example, if you need to create an sftp user with nologin:
sudo useradd -s /sbin/nologin -U sftpuser
Based on your requirement you can combine multiple options. The following command sets home directory, comments, and login shell for the user:
sudo useradd -m -d /var/www -c “New Web Developer” -s /bin/bash -U webuser
When you add a new user using the useradd command, some default system values are automatically set for the user. Use the following command to check the default system values:
sudo useradd -D
In the preceding example, 100 is the default group ID for user's initial login group, /home is the default directory under which user’s home directory will be created, and /bin/bash is the default shell. The user’s password expires only if specified while creating the user account because the expiry field is empty by default.
These default options are applied to every user account creation request, unless these options are specified in the command line. You can change the default useradd values either manually editing the in the
/etc/default/useradd file or from the command line.
The following command change the default login shell to /sbin/nologin:
sudo useradd -D -s /sbin/nologin
In this tutorial, we learned about useradd command in Linux. The useradd options described in this tutorial will work in most Linux distributions such Ubuntu, Debian, Rocky Linux, AlmaLinux, Arch Linux, and Fedora.