How to Create Users in Linux 2024: Useradd Command for Ubuntu, CentOS, Debian

If you’re a Linux system administrator, it’s essential to understand how to manage multiple user accounts efficiently. Linux is a versatile, multi-user system that allows numerous users to interact with the same system concurrently. Therefore, managing the system’s users and groups is a crucial aspect of system administration. In this article, we will discuss the useradd command, which is an excellent tool for creating new user accounts and assigning them to different groups. Keep reading to learn more about how to create new user accounts in Linux with ease using the useradd command.

How to Create Users in Linux

useradd Command

Check out the useradd command syntax, it’s pretty straightforward. Here it is:

ShellScript
useradd [OPTIONS] USERNAME

Only users with root or sudo privileges can utilize the useradd command for creating new user accounts.

The useradd command generates a new user account based on the specified options and default values present in the /etc/default/useradd file.

The variables defined in the /etc/default/useradd file may vary from one distribution to another, leading to diverse outcomes on various systems upon using the useradd command.

The /etc/login.defs file contains configurations for the shadow password suite, such as password expiration policies, ranges of user IDs, and more, which useradd also reads.

How to Create a New User in Linux

To add a new user account, use the useradd command and specify the username as a parameter. For instance, if you want to create a user account with the name “username”, simply enter the following command:

ShellScript
sudo useradd username

This will create a new user account with the specified name.

By default, the useradd command creates a new user account using the settings specified in the /etc/default/useradd file. Using useradd without any options allows for a quick and easy setup of a new user on a system.

If you want to create a new user and grant them access, you can add their credentials to important system files such as /etc/passwd, /etc/shadow, /etc/group, and /etc/gshadow using the command prompt. Once the user has been created, you will need to set a password for them to log in. Simply execute the passwd command, followed by the username, to accomplish this task.

ShellScript
sudo passwd username

When setting up your account, you will be asked to provide and verify a password. It is crucial to use a strong and secure password to protect your account from unauthorized access. Ensure that your password contains a combination of uppercase and lowercase letters, numbers, and symbols. Avoid using common phrases or personal information that can be easily guessed. By selecting a robust password, you can enhance the security of your account and safeguard your sensitive information.

Output

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

How to Add a New User and Create a Home Directory

To create a new user’s home directory on Linux distributions, use the useradd command with the -m (–create-home) option. Without this option, the home directory will not be automatically created. To create the user’s home directory as /home/username, run the following command with sudo privileges:

ShellScript
sudo useradd -m username

This command not only creates the new user’s home directory but also copies files from the /etc/skel directory to the user’s home directory. To verify if the initialization files have been successfully copied, list the files in the /home/username directory using the command:

ShellScript
ls -la /home/username/

By using the -m option, you can ensure that the user’s home directory is created and initialized correctly on Linux distributions, facilitating the setup and management of user accounts.

Output

drwxr-xr-x 2 username username 4096 Jan 23 07:12 .
drwxr-xr-x 4 root root 4096 Jan 23 07:12 ..
-rw-r–r– 1 username username 220 Feb 5 2023 .bash_logout
-rw-r–r– 1 username username 3771 Feb 5 2023 .bashrc
-rw-r–r– 1 username username 807 Feb 5 2023 .profile

How to Create a User with a Specific Home Directory

To create a new user and customize their home directory location, use the d (–home) option with the useradd command. By default, useradd creates the user’s home directory in /home, but this can be changed as per your requirements.

For instance, if you wish to create a new user named “username” with a home directory located at /opt/username, you can use the following command with sudo access:

ShellScript
sudo useradd -m -d /opt/username username

This will create a new user account with a specified home directory location at /opt/username.

How to Create a User with a Specific User ID

In Linux or Unix-like operating systems, every user has a unique UID and username. UID, or user identifier, is a distinct, positive integer given by the Linux system to each user. It, along with other access control policies, determines what actions a user can take on system resources.

When creating a new user, the system assigns the next available UID from the range of user IDs set in the login.defs file. However, if you want to create a user with a specific UID, you can use the -u option with the useradd command. For instance, to create a new user named “username” with a UID of 1500, you’d enter:

ShellScript
sudo useradd -u 1500 username

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

ShellScript
id -u username

Output

1500

How to Create a User with a Specific Group ID

In Linux, organizing and managing user accounts is accomplished through the use of Linux groups. The primary function of these groups is to allocate a set of privileges such as reading, writing, or executing permissions for a particular resource that can be shared among the users within the group.

When creating a new user in Linux, the default behavior of the useradd command is to generate a group with the same name as the username and the same GID as UID. However, the -g (–gid) option can be used to create a user with a specific initial login group, identified by either the group name or the GID number. It is essential to note that the group name or GID must already exist.

To illustrate, here’s an example of creating a new user with a login group of “users”:

ShellScript
sudo useradd -g users username

To confirm the user’s GID, use the following command:

ShellScript
id -gn username

Output

Users

How to Create a User and Assign Multiple Groups

In Linux operating systems, there are two types of groups: Primary and Secondary (or Supplementary) groups. Each user can belong to precisely one Primary group and one or more Secondary groups. To add a user to a list of Supplementary groups, use the -G (–groups) option. For instance, the command below creates a new user named “username” with a Primary group of “users” and Secondary groups of “wheel” and “docker”:

ShellScript
sudo useradd -g users -G wheel,developers username

To check the groups that a user belongs to, type the following command:

ShellScript
id username

Output

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

How to Create a User with a Specific Login Shell

When creating a new user, their login shell is set to the default specified in the /etc/default/useradd file. This could be /bin/sh or /bin/bash depending on the Linux distribution. However, you can use the -s (–shell) option with the useradd command to specify a different shell. For instance, to create a new user called username with /usr/bin/zsh as their login shell, enter this command:

ShellScript
sudo useradd -s /usr/bin/zsh username

Once done, you can verify the user’s login shell by checking their entry in the /etc/passwd file using this command:

ShellScript
grep username /etc/passwd

Output

username:x :1001:1001::/home/username:/usr/bin/zsh

How to Create a User with a Custom Comment

If you want to provide a brief overview of a new user, you can use the -c (–comment) option. This option is useful for including the user’s full name or contact information as a comment. For instance, you can create a new user with the username “username” and a comment “Test User Account” using the following command:

ShellScript
sudo useradd -c "Test User Account" username

Once created, the comment will be saved in the /etc/passwd file. To verify the comment, you can use the following command:

ShellScript
grep username /etc/passwd

By using the -c option, you can easily add a comment to a new user’s account, making it easier to identify and manage the account in the future.

Output

username:x :1001:1001:Test User Account:/home/username:/bin/sh

How to Create a User with an Expiry Date

To create temporary user accounts with an expiration date, use the -e (–expiredate) option followed by the date in YYYY-MM-DD format. This is a useful feature when setting up new user accounts. For instance, to create a temporary user account named username that expires on May 12, 2030, run the command:

ShellScript
sudo useradd -e 2030-05-12 username.

To confirm the expiration date of the user account, use the chage command by typing:

ShellScript
sudo chage -l username

Output

Output
Last password change: June 10, 2023
Password expires: never
Password inactive: never
Account expires: May 12, 2023
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

How to Create a System User

When installing the operating system and new packages, system users are usually created. Although there is no significant technical distinction between system users and regular users, it is worth noting that system users are created with no expiry date and have unique User IDs (UIDs) within a particular range reserved for system users, distinct from the range utilized for regular users. To create a system user account, use the -r (–system) option, followed by the command:

ShellScript
sudo useradd -r username

How to Change the Default useradd Values

To customize the default useradd options in Linux, you can use the “-D” or “–defaults” option, or edit the settings in the “/etc/default/useradd” file manually. If you want to check the current default options, simply enter “useradd -D” in the command line. Take control of your Linux user management and customize your settings to your liking.

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

If you are looking to change the default login shell on your system, you can easily do so by specifying the new shell you want to use. For instance, let’s say you want to switch from /bin/sh to /bin/bash, all you have to do is run the command below with sudo privileges:

ShellScript
sudo useradd -D -s /bin/bash

To confirm that the new default shell has been set, you can run the following command with sudo privileges:

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

By following these simple steps, you can change the default shell on your system and begin enjoying the benefits of your preferred shell.

Conclusion

So that’s all about how to easily create new user accounts in Linux with the useradd command. This comprehensive guide applies to all major Linux distributions, such as Ubuntu, CentOS, RHEL, Debian, Fedora, and Arch Linux. If you’re using Debian or Ubuntu, you can use the more user-friendly adduser command instead. If you have any questions, don’t hesitate to leave a comment below. Follow these simple steps and create new user accounts in Linux hassle-free.

Nico Lewis

Nico Lewis

Hi, this is Nico Lewis, a tech professional and a core member of TrioTeam who has been writing solutions to technical problems, how to tutorials, new technology, tools and websites, and so on for TrioGuide.

We will be happy to hear your thoughts

Leave a reply

TrioGuide
Logo