5 useradd command to create users in linux

In Linux, the ‘useradd‘ command is a low-level utility used for adding or creating user accounts in Linux and other Unix-like operating systems. The ‘adduser‘ command is very similar to the ‘useradd‘ command, as it is just a symbolic link to it.

In some Linux distributions, the ‘useradd‘ command may have a slightly different version. I suggest reading your documentation before using our instructions to create new user accounts in Linux.

When we run the ‘useradd‘ command in the Linux terminal, it performs the following major tasks:

  • It edits /etc/passwd/etc/shadow/etc/group, and /etc/gshadow files for the newly created user accounts.
  • Creates and populates a home directory for the new user.
  • Sets permissions and ownerships to the home directory.

Useradd Command Syntax

The basic syntax of the ‘useradd‘ command is:

The basic syntax of the ‘useradd‘ command is:

useradd [options] username

In this article, we will demonstrate the 15 most commonly used ‘useradd‘ commands with practical examples in Linux.

1. How to Add a New User in Linux

To add or create a new user, you have to use the ‘useradd‘ or ‘adduser‘ command followed by the ‘username‘. The ‘username‘ is the login name a user uses to log into the system.

Only one user can be added, and the username must be unique, and not already exist on the system.

For example, to add a new user named ‘chennaihosting‘ use the following command:

useradd chennaihosting

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

passwd chennaihosting

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

chennaihosting:x:1000:1000:chennaihosting:/home/chennaihosting:/bin/bash

The above entry contains a set of seven colon-separated fields, each field having its own meaning.

Let’s see what these fields are:

  • Username – The user login name is used to log into the system. It should be between 1 and 32 characters long.
  • Password – The user password (or 'x' character) is stored in the ‘/etc/shadow‘ file in an encrypted format.
  • User ID (UID) – Every user must have a User ID (UID), which stands for User Identification Number. By default, UID 0 is reserved for the root user, and UIDs ranging from 1 to 99 are reserved for other predefined accounts. Additionally, UIDs ranging from 100 to 999 are reserved for system accounts and groups.
  • Group ID (GID) – The primary Group ID (GID), which stands for Group Identification Number, is stored in the ‘/etc/group‘ file.
  • User Info – This field is optional and allows you to define extra information about the user, such as the user’s full name. This information can be filled in using the finger command.
  • Home Directory – The absolute location of the user’s home directory.
  • Shell – The absolute location of a user’s shell i.e. /bin/bash.

2. How to Create a User with a Different Home Directory

By default, the ‘useradd‘ command creates a user’s home directory under the ‘/home‘ directory with the username. For example, as seen above, the default home directory for the user ‘chennaihosting‘ is ‘/home/chennaihosting‘.

However, this behavior can be changed by using the '-d' option along with the location of the new home directory (e.g., ‘/data/projects‘). For instance, the following command will create a user ‘shruti‘ with a home directory set to ‘/data/projects‘.

# useradd -d /data/projects shruti
# passwd Shruti

You can view the user’s home directory and other user-related information, such as user ID, group ID, shell, and comments using the following cat command.cat /etc/passwd | grep anusha

cat /etc/passwd | grep shruti

shruti:x:1001:1001::/data/projects:/bin/bash

3. How to Create a User with a Specific User ID

In Linux, every user has their own UID (Unique Identification Number). By default, when we create a new user account in Linux, it assigns user IDs 500501502, and so on.

However, we can create users with custom user IDs using the '-u' option. For example, the following command will create a user ‘helen‘ with a custom user ID ‘1002‘.

useradd -u 1002 helen

Now, let’s verify that the user created with a defined userid (1002) using the following command.

cat /etc/passwd | grep helen

helen:x:1002:1002::/home/helen:/bin/bash

4. How to Create a User with a Specific Group ID

Similarly, every user has their own GID (Group Identifier). We can create users with specific group IDs as well using the '-g' option.

In this example, we will add a user ‘jane‘ with a specific UID and GID simultaneously with the help of the '-u' and '-g' options.

useradd -u 1005 -g chennaihosting jane

Now, check the assigned user ID and group ID in the ‘/etc/passwd‘ file.

cat /etc/passwd | grep jane

jane:x:1005:1000::/home/jane:/bin/bash

To verify the user’s GID, use the id command:

id -gn jane

5. How to Add a User to Multiple Groups

The '-G' option is used to add a user to additional groups. Each group name is separated by a comma, with no intervening spaces.

In this example, we are adding a user ‘chennaihosting‘ to multiple groups, such as adminswebadmin, and developers.

groupadd admins
groupadd webadmin
groupadd developers
usermod -a -G admins,webadmin,developers chennaihosting
useradd -G admins,webadmin,developers paddy

Next, verify that the multiple groups are assigned to the user with the id command.

id chennaihosting

uid=1000(chennaihosting) gid=1000(chennaihosting)
groups=1000(chennaihosting),1007(admins),1008(webadmin),1009(developers)
context=root:system_r:unconfined_t:SystemLow-SystemHigh

We hope you’ve found this useful!!