AWS Google Cloud

Create Users with useradd Command on Linux – Complete Guide

Create Users and Manage with useradd Command – Linux Complete Guide. In Linux and other Unix operating systems you can create multiple users and assign them to a group or multiple groups and provide necessary privileges to the new users. By default root user has the highest privilege on the system.

In this guide you are going to learn how to create users with useradd command and also explore the options that can be used with this command.

You can also use this guide on any Linux OS bases systems or servers or on Cloud or VPS or any dedicated servers also.

Prerequisites

Logged in to the server or system SSH as root or the user with sudo privileges.

useradd Command Syntax

The general syntax for the useradd command is as follows.

useradd username

When you run this command your will be prompted with several questions to create the user.

The useradd command also allow several options to be added with the following syntax.

useradd options username

Create a New User in Linux

To create a new user account type useradd followed by the username.

sudo useradd username

In its simplest form when used without any option, useradd will create a new user account with the default settings specified in the /etc/default/useradd file.

To log in as the newly created user, you need to set the user password using the passwd command as shown below.

sudo passwd username

You will be prompted to enter and confirm the password. Make sure you use a strong password.

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

Create New User with Home Directory

You can use the -m (--create-home) option to create the user with home directory as /home/username

sudo useradd -m username

This command creates the new home directory with the username as directory name.

Create User with Different Home Directory

If you want to create the user’s home directory in other location than the default /home directory you can use the d (--home) option as shown below.

sudo useradd -m -d /new/path username

Create User with Specific User ID

By default when a new user is created the system assigns the next available UID from the range of user IDs specified in the login.defs file.

You can use the -u (--uid) option to create a user with a specific UID.

sudo useradd -u 1500 username

You can verify the user’s UID, using the id command:

id -u username
Output
1500

Create User with Specific Group ID

When creating a new user the default behavior of the useradd command is to create a group with the same name as the username, and same GID as UID.

You can 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 should exist.

sudo useradd -g users username

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

id -gn username
Output
users

Create 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.

The -G (--groups) option allows you to specify a list of supplementary groups which the user will be a member of.

sudo useradd -g users -G wheel,docker username

Create User with Specific Login Shell

By default, the new user’s login shell is set to the one specified in the /etc/default/useradd file. In some Linux distributions such as Ubuntu 18.04 the default shell is set to /bin/sh while in others it is set to /bin/bash.

The -s (--shell) option allows you to specify the new user’s login shell.

To create a new user named username with /bin/sh as a login shell type you can use the following command.

sudo useradd -s /bin/sh username

Create User with a Comment

The -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 named username with text string New User Account as a comment:

sudo useradd -c "New User Account" username

The comment field is also known as GECOS.

Create User with an Expiry Date

The -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.

To create a new user account named username with expiry time set to December 31 2025 you can follow the below example. The date must be specified using the YYYY-MM-DD format.

sudo useradd -e 2025-25-31 username

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

sudo chage -l username

Create a System User

There is no real technical difference between the system and regular (normal) users. Typically system users are created when installing the OS and new packages.

In some situations, you may need to create a system user that will be used by some application.

Use the -r (--system) option to create a system user account. For example, to create a new system user named username you would run:

sudo useradd -r username

System users are created with no expiry date. Their UIDs are chosen from the range of system user IDs specified in the login.defs file which is different than the range used for normal users.

Change the Default useradd Values

The default useradd options can be viewed and changed using the -D--defaults option or by manually editing the values in the /etc/default/useradd file.

To view the current default options type:

useradd -D

The output will look something like this:

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

Let’s say you want to change the default login shell from /bin/sh to /bin/bash. To do that specify the new shell as shown below:

sudo useradd -D -s /bin/bash

You can verify that the default shell value is changed by running the following command:

sudo useradd -D | grep -i shell
SHELL=/bin/bash

Conclusion

Now you have learned how to create user with useradd command and also explored all options related to this command.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Cloudbooklet builds a large collection of Linux based guides and tutorials on Cloud platforms like Google Cloud, AWS, Azure, DigitalOcean and more

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.