useradd Command Examples in Linux

There are several ways in which a user can be added to a Linux Operating System. Here, we discuss the 7 most commonly used methods of adding users in Linux.

The syntax

The basic syntax to create a user is:

# useradd [username]

To be able to log in as the newly created user, you need to set the user password. Run the passwd command followed by the username:

# passwd [username]

It will prompt you to enter and confirm the password. Make sure to use a strong password.

Output:

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

1. Add a New User and Create Home Directory

Mostly when creating a new user account with the useradd command the user home directory is not created. Use the -m (–create-home) option to create the user home directory as /home/username.

# useradd -m [username]

2. Creating a User with Specific Home Directory

If you want to create the user’s home directory in other location then the default /home directory use the -d (–home) option.

For example, to create a new user name username1 with a home directory of /opt/username1 run the following command:

# useradd -m -d /opt/username1 username1

3. Creating a User with Specific User ID

Users are identified by unique UID and username. User identifier (UID) is a unique positive integer assigned by the Linux system to each user. The UID along with other access control policies is used to determine the types of actions a user can perform on system resources.

Use the -u (–uid) option to create a user with a specific UID. For example, to create a new user name username1 with UID of 1100:

# useradd -u 1100 username1

Verify the user’s UID, using the id command:

# id -u username1
1100

4. Creating a User with Specific Group ID

The main purpose of groups is to define a set of privileges such as reading, writing, or executing permission for a given resource that can be shared among the users within the group.

Use the -g (–gid) option to create a user with a specific initial login group. You can specify either the group name or the GID number. The group name or GID must already exist.

For example to create a new user name username1 and set the login group to users:

# sudo useradd -g users username1

Verify the user’s GID, use the id command:

# id -gn username1
users

5. Creating a User and Assign Multiple Groups

There are two types of groups in Linux operating systems Primary group and Secondary or supplementary group. Each user can belong to exactly one primary group and zero or more secondary groups.

-G (–groups) option allows you to specify a list of supplementary groups which the user will be a member of. The following command will create a new user name username1 with primary group users and secondary groups wheel and docker:

# useradd -g users -G wheel,docker username1

Verify the user groups, use the id command:

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

6. Creating a User with Custom Comment

-c (–comment) option allows you to add a short description for the new user. Typically the user’s full name or the contact information are added as a comment.

In the following example we are creating a new user name username1 with text string User Account Test as a comment:

# useradd -c "User Account Test" username1

The comment is saved in /etc/passwd file:

# grep username1 /etc/passwd
username:x :1001:1001:Test User Account:/home/username1:/bin/sh

7. Creating a User with an Expiry Date

-e (–expiredate) option allows you to define a time at which the new user accounts will expire. This option is useful for creating temporary accounts. The date must be specified using the YYYY-MM-DD format.

For example to create a new user account name username1 with expiry time set to December 31 2020 you would run:

# useradd -e 2020-12-31 username1

You can use the #chage command to verify the user account expiry date:

# chage -l username1

The output will look something like this:

Last password change : November 1, 2020
Password expires : never
Password inactive : never
Account expires : Dec 31, 2020
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