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 user with the command: id username
- set a password for the user with the command: passwd username
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:
You should see output similar to the following:
uid=1001(username) gid=1001(username) groups=1001(username),10(wheel)
To verify a use in Linux, you can also use the grep command to search the /etc/passwd file. This file contains information about all the users on the system, including their usernames, user IDs, group IDs, home directories, and shell.
Type the following command:
grep username /etc/passwd
This will display the new user account that you created.
Understanding 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.
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 /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow.
- It creates a new home directory. By default, the new home directory is a copy of the /etc/skel directory and its contents.
- It sets 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:
This will display a list of all the options for the useradd command, along with a brief description of what each option does.
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
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 home directory does not have to exist. It will be created if it is missing.
Here is an example showing how to create a new user named howtouselinux with a home directory of /opt/howtouselinux:
sudo useradd -m -d /opt/howtouselinux howtouselinux
sudo command here is used to run the following command as a superuser or root user, which allows the user to execute commands with administrative privileges.
To verify if the above command was executed successfully, you can follow these steps:
Check the contents of the “/etc/passwd” file using the following command:
cat /etc/passwd | grep howtouselinux
This command will search for the “howtouselinux” entry in the “/etc/passwd” file, which contains information about all the users on the system. If the user was created successfully, you should see an entry for “howtouselinux” with the correct home directory (“/opt/howtouselinux”) and other user-related information.
Verify that the home directory was created using the following command:
ls -l /opt/howtouselinux
This command will check if the “/opt/howtouselinux” directory was created. If the user was created successfully, this directory should exist and should be owned by the “howtouselinux” user.
Create a User with Specific User ID in Linux
In Linux, each user is assigned a unique identifier called a User ID (UID). The UID is a numerical value used by the system to identify the user and determine their access privileges to files, directories, and other system resources.
Note that some system services and applications may require specific UIDs to function properly. For example, the Apache web server typically runs as a user with a specific UID (e.g., “www-data” on Debian-based systems), which is used to enforce access controls and ensure that the server has the necessary privileges to access files and directories.
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
You can verify the UID (user ID) of a user using the id command. The id command displays the user and group IDs for the current user or for the specified username.
To verify the UID of a user, open a terminal and type the following command:
id -u username
Replace username with the actual username of the user you want to verify the UID for. This will display the UID for the specified user.
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
If you want to verify the shell for the currently logged-in user, you can use the echo command with the $SHELL environment variable, like this:
This will display the shell that is currently being used by the user who is logged in.
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.
You can also setup default password expiry for all new users in Linux with /etc/login.defs file.
Open file /etc/login.defs using text editor and set the following values as per your requirements.
Changes made to /etc/login.defs affect only new users created on the system. For existing users, you must use the chage command.
You can set the same configuration for existing users with:
$ sudo chage --mindays 7 --maxdays 90 --warndays 5 user1
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
You can use the following command to check if a user is a system user:
$ id -u <username>
Replace <username> with the username you want to check. If the output is a UID value lower than 1000, the user is a system user.
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:
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.
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.