howtouselinux

How To Create Users In Linux – Step By Step Tutorial

Table of Contents

In this step-by-step guide, we will show you how to add users in Linux. This process is different on every distro, so we have provided instructions for the most popular ones. We will also show you how to set up a password.

In Linux, user accounts can be created in two ways: through the command line or through a graphical interface. In this how-to guide, we will show you how to create a user account through the command line.

How to Create a New User in Linux

To create a new user in Linux, follow these steps:

  •  Log in to your Linux server and switch user to root
  • Type the following command to create a new user account: useradd username
  • Press Enter to create the user account.

 

verify the new account from /etc/passwd file or id command

To verify that the new user account has been created in Linux, you can use the id command. This command displays information about a user, including their UID, GID, and other information.

For example, to verify the new user account we created in the previous step, type the following command:

id username

You should see output similar to the following:

uid=1001(username) gid=1001(username) groups=1001(username),10(wheel)

You can also check the /etc/passwd file to see the new user account. This file contains a list of all the users on the system, along with their username, UID, GID, and other information.

Type the following command: grep username /etc/passwd

This will display the new user account that you created.

Set a Password for a User in Linux

After you have created a new user account in Linux, it is important to set a password for that account. This will help to protect your server from unauthorized access.

To set a password for a user in Linux, follow these steps:

  •  Log in to your Linux server and switch user to root
  • Type the following command to set the password for the new user account: passwd username
  • Press Enter to continue.
  • Enter the new password for the user account, and press Enter.
  • Enter the new password again to confirm it, and press Enter.

 

Create a user with useradd Command in Linux

The useradd command allows you to create a new user account on your Linux server. To use the useradd command, you must login to your server as the root user.

To create a new user account, type the following command:

useradd username

where username is the name of the new user. Once you have created the user account, you can login to your server using that account.

The useradd command performs the below tasks in Linux:

  • It edits the files for newly created user like /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow.
  • It creates and opens a new home directory. By default, the new home directory is a copy of the /etc/skel directory and its contents.
  • It allows us to set ownerships and permissions to the home directory.

 

The useradd command has several options that you can use to configure the new user account. To see a list of all the options, type the following command:

useradd -h

This will display a list of all the options for the useradd command, along with a brief description of what each option does.

Some of the most common options that you will use are:

  • -d : Specifies the home directory for the new user account. By default, the home directory is /home/username.
  • -e : Specifies the date when the account will expire. The format for this option is YYYY-MM-DD.
  • -f : Specifies the number of days after the account expires until it is permanently disabled.
  • -g : Specifies the primary group for the new user account. By default, this is the group with the same name as the username.
  • -k : Copies files from the /etc/skel directory into the new user’s home directory. These files typically contain default configuration settings for the shell and other applications.
  • -m : Creates the user’s home directory if it does not already exist.
  • -s : Specifies the user’s login shell. This is the program that will run when the user logs in to the account. By default, this is set to /bin/bash.
  • -u : Specifies the user ID for the new user account. This must be a unique number on the system. By default, the next available UID is used.

 

After you have specified the options that you want to use, press Enter to create the user account.

The following two files define the default info for new user.

  • /etc/login.defs – This file defines entries for mail, password complexity and limitations, UID and GID minimum and maximum values, and whether the user’s home directory is created by default.
  • /etc/default/useradd – This file defines the defaults of shell,home directory,skel file,group id etc.

 

Create a User with a Specific Home Directory in Linux

By default, useradd creates the user’s home directory in the /home directory. If you want to specify a different home directory, you can use the -m (–create-home) and -d (–home-dir ) options.

The new user will be created using home dir as the value for the user’s login directory. 

The home directory does not have to exist but will not be created if it is missing.

Here is an example showing how to create a new user named username with a home directory of /opt/username:

sudo useradd -m -d /opt/username username

Create a User with Specific User ID in Linux

If you want to create a user with a specific UID, you can use the u (–uid) option. This is useful if you are migrating users from another system, and you need to maintain the same UIDs.

Here is an example showing how to create a new user named username with a UID of 1000:

sudo useradd -u 1000 username

Create a User in a Specific Group in Linux

understanding user initial group and supplementary group

When you create a user account, you can also specify the user’s initial group and supplementary groups. The initial group is the group that the user is automatically added to when they login for the first time. The supplementary groups are groups that the user can be added to later if needed.

You can specify the initial group and supplementary groups with the -g (–group GROUPS) and -G (–groups GROUPS) options. The format for these options is:

  • -g : Specifies the initial group for the new user.
  • -G : Specifies the supplementary groups for the new user.

 

By default, useradd creates the user in the same group as their username. If you want to create the user in a specific initial group, you can use the g (–gid) option. The group name must exist.

Here is an example showing how to create a new user named username in the group developers:

sudo useradd -g developers username

You can also specify the GID instead of the group name. In this case, useradd will use the GID of the group that you specify.

Here is an example showing how to create a new user named username with a GID of 1000:

sudo useradd -g 1000 username

Create a User with a Specific Login Shell in Linux

The login shell is the program that runs when the user logs in to their account. By default, useradd sets the login shell to /bin/bash. If you want to specify a different login shell, you can use the s (–shell) option.

Here is an example showing how to create a new user named username with the login shell set to /bin/zsh:

sudo useradd -s /bin/zsh username

Create a User with All Options in Linux

The useradd command has many options that you can use to customize the user account. These options can be used together to create a user with specific settings.

Here is an example showing how to create a new user named username with a home directory of /opt/username, a UID of 1000, and a login shell of /bin/zsh:

sudo useradd -m -d /opt/username -u 1000 -s /bin/zsh username

Create a User with an expiration date in Linux

If you want to create a user with an expiration date, you can use the -e (–expiredate) option. This will cause the user’s password to expire, and they will be prompted to change it the next time they try to log in.

Here is an example showing how to create a new user named username with an expiration password:

sudo useradd -e `date -d “next month” +%Y-%m-%d` username

In this example, the user’s password will expire next month. You can use any date format that the date command supports.

Create a User with Inactive date in Linux

If you want to create a user with inactive date, you can use the -f (–inactive) option. This will cause the user’s account to be disabled after a number of days when a password expires.

Here is an example showing how to create a new user named username with an inactive status of 30 days:

sudo useradd -f 30 username

Create a System User in Linux

A system user is a user account that is used for system purposes, such as running services or daemons. System users are not meant to be logged into directly, and they do not have passwords.

The most common use for system users is to run services. For example, the Apache web server can be run as a system user. This allows the web server to run as a privileged user, and it also ensures that the web server process will be killed if the user logs out.

If you want to create a system user, you can use the -r (–system) option. This will cause the user’s account to be created with a UID of less than 1000.

Here is an example showing how to create a new system user named username:

sudo useradd -r username

Change the Default useradd Values in Linux

The default values for useradd can be changed by editing the /etc/default/useradd file. This file contains a number of variables that control the behavior of useradd.

Here is an example of the /etc/default/useradd file:

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

The most important variables are GROUP, HOME, INACTIVE, EXPIRE, and SHELL. These variables control the default values for the -g, -d, -f, -e, and -s options of useradd.

You can change these values by editing the /etc/default/useradd file and setting the desired value for each variable.

Conclusion

In this article, you have learned how to use the useradd command to create new users in Linux. You should now be able to create new users with the settings that you desire.

If you have any questions, please leave a comment below.

 

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.