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
Check out the
useradd command syntax, it’s pretty straightforward. Here it is:
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:
sudo useradd username
This will create a new user account with the specified name.
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.
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.
OutputChanging password for user username.
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:
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:
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.
Outputdrwxr-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:
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:
sudo useradd -u 1500 username
To verify the user’s UID, use the
id -u username
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”:
sudo useradd -g users username
To confirm the user’s GID, use the following command:
id -gn username
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”:
sudo useradd -g users -G wheel,developers username
To check the groups that a user belongs to, type the following command:
Outputuid=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:
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:
grep username /etc/passwd
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:
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:
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.
Outputusername: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:
sudo useradd -e 2030-05-12 username.
To confirm the expiration date of the user account, use the chage command by typing:
sudo chage -l username
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:
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.
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:
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:
sudo useradd -D | grep -i shell
By following these simple steps, you can change the default shell on your system and begin enjoying the benefits of your preferred shell.
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.