How to Create Users on Linux (useradd Command)

Introduction

Before we begin talking about how to create users on Linux using useradd command, let's briefly understand – What is useradd command?

The useradd command on Linux is a powerful tool used to create user accounts. Linux is a multi-user operating system, which means that multiple people can use the system at the same time.

As a system administrator, you're in charge of managing the system's users and groups, which includes adding and removing users and assigning them to various groups.

In this tutorial, you will create users on Linux using useradd command. We will also address a few FAQs on how to create users on Linux using useradd command.

Advantages of useradd command

  1. User Management: useradd simplifies the addition of new users, including specifying usernames, user IDs, and other details.
  2. Resource Access Control: It enables administrators to control user access to system resources, ensuring security and data integrity.
  3. Efficient Account Configuration: useradd facilitates the setup of home directories, default shells, and other user account configurations.
  4. Automation and Scripting: It can be incorporated into scripts, allowing system administrators to automate user account creation tasks.
  5. Flexible Options: useradd offers various options to customize user accounts, accommodating different requirements and preferences.

useradd Command

The useradd command has the following general syntax:

useradd [OPTIONS] USERNAME

The useradd command can only be used by root or users with sudo access to create new user accounts.

When useradd is run, it creates a new user account based on the command line parameters and the default values in the /etc/default/useradd file.

Because the variables defined in this file differ from one distribution to the next, the useradd command produces various results on different systems.

The contents of the /etc/login.defs file are likewise read by useradd. This file provides shadow password suite configuration, such as password expiration policies, user ID ranges used for creating system and normal users, and more.

How to Create a New User on Linux

Invoke the useradd command, followed by the user's name, to establish a new user account.

To create a new user named username, for example, type:

sudo useradd username
ℹ️
Useradd creates a new user account using the default settings supplied in the /etc/default/useradd file when run without any options.

The command updates the /etc/passwd, /etc/shadow, /etc/group, and /etc/gshadow files with a new entry.

You must set the user password before you may log in as the newly formed user. To do so, use the passwd command with the username:

sudo passwd username

You'll be asked to type in and confirm your password. Make sure your password is strong.

Output

Changing password for user username.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

How to Add a New User and Create Home Directory

When using useradd to establish a new user account, the user's home directory is not created on most Linux distributions.

Create the user home directory as /home/username with the -m (--create-home) option.

sudo useradd -m username

The command above creates a new user's home directory and moves files from the /etc/skel directory into it. The initialization files can be found by listing the files in the /home/username directory:

ls -la /home/username/
Output

drwxr-xr-x 2 username username 4096 Dec 11 11:23 .
drwxr-xr-x 4 root     root     4096 Dec 11 11:23 ..
-rw-r--r-- 1 username username  220 Apr  4  2018 .bash_logout
-rw-r--r-- 1 username username 3771 Apr  4  2018 .bashrc
-rw-r--r-- 1 username username  807 Apr  4  2018 .profile

The user can write, edit, and delete files and folders in their home directory.

Creating a User with Specific Home Directory

useradd creates the user's home directory in /home by default. Use the d (--home) option to build the user's home directory in another place.

An example of how to establish a new user named username with the home directory /opt/username is as follows:

sudo useradd -m -d /opt/username username

Creating a User with Specific User ID

Users are identifiable in Linux and Unix-like operating systems by a unique UID and username.

The Linux system assigns each user a unique positive integer called a user identifier (UID). The type of actions a user can conduct on system resources is determined by the UID and other access control restrictions.

By default, the system allocates the next available UID from the range of user IDs defined in the login.defs file when a new user is established.

To create a user with a given UID, use the -u (--uid) option with useradd. To create a new user named username with the UID 1500, for example, type:

sudo useradd -u 1500 username

The id command can be used to validate the user's UID:

id -u username
Output

1500

Creating a User with Specific Group ID

Linux groups are organizational units that are used on Linux to organize and manage user accounts. The fundamental goal of groups is to define a set of privileges for a certain resource that may be shared across the users inside the group, such as reading, writing, or executing permission.

The useradd command creates a group with the same name as the username and the same GID as the UID by default when establishing a new user.

You can create a user with a defined initial login group by using the -g (--gid) option. Either the group name or the GID number can be specified. The GID or group name must already exist.

The following example demonstrates how to create a new user named username and assign it to the users type login group:

sudo useradd -g users username

Use the id command to validate the user's GID:

id -gn username
Output

users

Creating a User and Assign Multiple Groups

In the Linux operating system, there are two sorts of groups: primary and secondary (or supplementary). A user can only be a member of one primary group and one or more secondary groups.

With the -G (--groups) option, you can define a list of extra groups to which the user will belong.

The command below creates a new user named username, with the primary group users and the subsidiary groups wheel and docker.

sudo useradd -g users -G wheel,developers username

You can look for the user groups by typing:

id username
output

uid=1002(username) gid=100(users) groups=100(users),10(wheel),993(docker)

Creating a User with Specific Login Shell

The login shell for the new user is assigned to the one given in the /etc/default/useradd file by default. The default shell in some distributions is /bin/sh, but in others it is /bin/bash.

You can define the new user's login shell with the -s (--shell) option.

To create a new user named username using the login shell typing /usr/bin/zsh, for example:

sudo useradd -s /usr/bin/zsh username

Verify the user's login shell by looking it up in the /etc/passwd file:

grep username /etc/passwd
output

username:x :1001:1001::/home/username:/usr/bin/zsh

Creating a User with Custom Comment

You can add a short description for the new user with the -c (--comment) option. A comment usually include the user's complete name or contact information.

We'll create a new account named username with the text string Test User Account as a comment in the following example:

sudo useradd -c "Test User Account" username

The following comment is stored in the /etc/passwd file:

grep username /etc/passwd
Output

username:x :1001:1001:Test User Account:/home/username:/bin/sh

GECOS is another name for the comment field.

Creating a User with an Expiry Date

Use the -e (--expiredate) option to set an expiration date for new user accounts. This comes in handy when creating temporary accounts.

The date must be entered in the format YYYY-MM-DD.

For example, to establish a new user account with the name username and an expiration date of January 22, 2019, run:

sudo useradd -e 2019-01-22 username

Verify the user account expiry date with the chage command:

sudo chage -l username

You will get an output like below:

Output

Last password change					: Dec 11, 2018
Password expires					: never
Password inactive					: never
Account expires						: Jan 22, 2019
Minimum number of days between password change		: 0
Maximum number of days between password change		: 99999
Number of days of warning before password expires	: 7

Creating a System User

The system and regular (normal) users have no real technical differences. When installing the operating system and new packages, system users are typically established.

To create a system user account, use the -r (--system) option. To create a new system user named username, for example, type:

sudo useradd -r username

Users in the system are created with no expiration date. Their UIDs are picked from the login.defs file's range of system user IDs, which is different from the range used for normal users.

Changing the Default useradd Values

The -D, --defaults option, or manually altering the settings in the /etc/default/useradd file, can be used to inspect and change the default useradd options.

Type the below command to see the current default options:

useradd -D

You will get an output like below:

Output

GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no

Let's imagine you wish to switch from /bin/sh to /bin/bash as your default login shell. To do so, use the following syntax to specify the new shell:

sudo useradd -D -s /bin/bash

Run the following command to see if the default shell value has been changed:

sudo useradd -D | grep -i shell
output

SHELL=/bin/bash

FAQs to Create Users on Linux (useradd Command)

How do I use the useradd command on Linux?

To use the useradd command, open a terminal and enter "useradd" followed by the desired username. Additional options can be specified to configure various attributes of the user.

Can I specify a custom user ID (UID) while creating a user?

Yes, you can specify a custom user ID by using the -u option followed by the desired UID: useradd -u UID username.

How can I assign a custom home directory to a user?

You can assign a specific home directory using the -d option: useradd -d /path/to/directory username. Replace "/path/to/directory" with the desired location.

What if I want to assign a custom shell to the user?

You can assign a custom shell by using the -s option followed by the desired shell path: useradd -s /path/to/shell username.

How do I set a user's password during account creation?

Passwords can be set using the passwd command after creating the user: passwd username. Follow the prompts to set the password.

How can I automatically create a user's home directory?

To automatically create a user's home directory, use the -m option: useradd -m username.

How can I create users with default configurations?

You can use the -D option to set default configurations, and then use useradd username to create users with those defaults.

Conclusion

We've taught you how to use the useradd command to create new user accounts. Any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, Fedora, and Arch Linux, can use these instructions.

Because useradd is a low-level function, Debian and Ubuntu users should instead use the more user-friendly adduser command.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.