1. Overview

In Linux, the useradd command helps to manage user accounts. System administrators can use it to create new users and its various options to customize their environment.

In this tutorial, we’ll learn about the basic usage of the useradd command which includes its syntax, options, and examples.

2. Creating a Basic User

The useradd command implements a basic syntax:

useradd [options] user

Let’s break this syntax down:

  • useradd – specifies the command to create new users
  • [options] – represents additional settings we can apply when creating a new user
  • user – defines the name we assign the new user

So, let’s create a new user without using any options:

$ sudo useradd maurice

This command creates a new user account maurice. To clarify, we add sudo to execute this command with superuser privileges since only a superuser can add a new user to the system. Additionally, since we’re not using any options, the useradd command uses the default settings described in the /etc/default/useradd file to create new user accounts. These settings include the home directory, login shell, expiration date of a user account as well as other configurations.

Now, let’s use useradd with options to customize user creation.

3. Customizing User Creation With Options

Using useradd without any options doesn’t create a home directory for the new user by default. That’s why we need to use the -m option:

$ sudo useradd -m maurice

Above, the -m option instructs useradd to create the home directory in the location /home/maurice if it doesn’t exist. Alternatively, we can specify a custom home directory when creating the new user account:

$ sudo useradd -m -d /home/user_maurice maurice

This command customizes the home directory to /home/user_maurice when creating the user account maurice.

Additionally, using useradd without any options doesn’t assign the new user a password. For this reason, we can use the passwd command after creating the user:

$ sudo passwd maurice
New password: 
Retype new password: 
passwd: password updated successfully

passwd prompts us to enter and confirm the new password for the user maurice.

Next, we can use the -g option to add the new user to a group:

$ sudo useradd -g developers maurice

Here, we assign the new user to the user group developers.

Further, we can set which shell the new user will interact with after logging into the system:

$ sudo useradd -s /bin/bash maurice

Above, the -s option helps create the user with the Bash shell as their default login shell. The login shell can also be set to another preferred shell.

Also, we can add the new user to multiple user groups during creation:

$ sudo useradd -G developers,admins maurice

In this example, maurice is created and added to the developers and admins groups.

Another option we can use is -c for comments:

$ sudo useradd -c "Project Developer" maurice

Here, -c specifies a comment meant to add additional information about the new user.

Finally, we can set an expiration date for a new user:

$ sudo useradd -e 2024-08-01 maurice

The -e option specifies that this account expires on August 1, 2024, which creates a temporary user who only requires access for a specific period.

Normally, we would execute useardd with all the needed options for the user we want to create. For instance:

$ sudo useradd -m -d /home/user_maurice -g developers -s /bin/bash -G developers,admins -c "Project Developer" -e 2024-08-01 maurice

Above, we combine all the options into a single command.

4. Conclusion

In this article, we discussed the basics of the useradd command.

This command created new users in the Linux system while the various options modified the default user settings such as home directory, group membership, and login shell.