Linux is a multi-user operating system that allows multiple users to interact with the system at the same time. The system administrator is responsible for managing these users and tasks related to user management, such as creating and deleting user accounts and managing groups of users. The useradd command is a Linux utility that is used to create new user accounts on the system for new users. In this article, I will describe all the features and uses of the useradd command in Linux with multiple examples.
A. Description
The useradd command is a Linux utility that allows you to create new user accounts on the system. This command is typically used by system administrators to create new user accounts for other users on the system.
B. Syntax
The syntax of the useradd command in Linux is pretty simple, as shown below.
useradd [OPTION]... user_name
C. Options
There are numerous options available for the useradd command. I have listed a few of them below. You can learn more about the useradd command, its options and their uses by checking the man page.
man useradd
Useful Options
- -c: Specify a comment or description for the user account.
- -d: Set the user’s home directory.
- -D: Set the user default value.
- -e,–expiredate: Set the date on which the user account will be disabled.
- -f,–inactive: Set password inactivity period of the new account.
- -g,–gid: Set the user’s primary group.
- -G,–groups: Set the user’s secondary groups.
- -h,–help: Display the help message.
- -m: Create the user’s home directory if it does not already exist.
- -M: Do not create the user’s home directory.
- -p,–password: encrypted password of the new account.
- -r,–system: Create a system account.
- -s: Set the user’s login shell.
Practical Examples of the “useradd” Command in Linux
In Linux, the useradd command is a helpful tool to create and manage a new user account for other users. In the section below, I will show you some of the most useful applications for the useradd command in Linux.
Example 1: Add a New User With the “useradd” Command in Linux
You can add a new user by simply running the useradd command. Here, I will create a new user called user2 with the useradd command. You can do the same by following the steps below:
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt:
sudo useradd user2
➌ Now, press the ENTER button.
➍ Type your Password.
➎ Press ENTER again.
Output >
In the image below, you can see that I have created a new user named “user2″.
Similar Readings
- The “usermod” Command in Linux [14+ Practical Examples]
- The “users” Command in Linux [4 Practical Examples]
- The “who” Command in Linux [6 Practical Examples]
Example 2: Create a New User With Specific Home Directory Using the “useradd” Command in Linux
You can create a new user with a specific home directory by simply running the useradd command with the option -d. Here, I will create a new user called user3 by specifying a home directory with the useradd command. You can do the same by following the steps below:
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt:
sudo useradd -d /home/star user3
➌ Now, press the ENTER button.
➍ Type your Password.
➎ Press ENTER again.
➏ Now, type the following command into the command prompt:
grep user3 /etc/passwd
➐ Press ENTER button.
Output >
In the following image, you can see that I have created a new user named “user3” in the /home/star directory.
Example 3: Create a User With a Specific User ID With the “useradd” Command in Linux
You can create a new user with a specific user ID by running the useradd command with the option -u. Here, I will create a new user called “user2“ by giving him the UID “2023” with the useradd command. You can do the same by following the steps below:
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt:
sudo useradd -u 2023 user2
➌ Now, press the ENTER button.
➍ Type your Password.
➎ Press ENTER again.
➏ You can check the UID by typing the following command.
grep user2 /etc/passwd
➐ Press ENTER button.
Output >
In the image below, you can see that I have created a new user named “user2” with the UID “2023”.
Example 4: Create a User With a Specific Group ID
You can create a new user with a specific group ID by running the useradd command with the option -g. Here, I will create a new user called “user5“ by giving it the GID “27” with the useradd command. You can do the same by following the steps below:
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt:
sudo useradd -g 27 user5
➌ Now, press the ENTER button.
➍ Type your Password.
➎ Press ENTER again.
➏ Type the command below to check the GID.
id user5
➐ press ENTER button.
Output >
In the following image, you can see that I have created a new user named “user5” with the GID “27”.
Example 5: Create a User Without a Home Directory
You can create a new user without any home directory by running the useradd command with the option -M. Here, I will create a new user called “user6“ without a home directory with the useradd command. You can do the same by following the steps below:
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt:
sudo useradd -M user6
➌ Now, press the ENTER button.
➍ Type your Password.
➎ After that press the ENTER button.
➏ To check the non-existence of home directory, type the following command into the command prompt:
sudo ls -l /home/user6
➐ Press the ENTER button.
Output >
In the following image, you can see that I have created a user named “user6” without the home directory.
Example 6: Create a User With an Expiry Date
In this section, I will create a new user called “user6“ with an expiry date of January 4, 2023, using the useradd command followed by the option -e. You can do the same by following the steps below:
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt:
sudo useradd -e 2023-01-04 temp
➌ Now, press the ENTER button.
➍ Type your Password.
➎ Press ENTER again.
➏ Now, type the following command into the command prompt:
sudo chage -l temp
➐ Press ENTER button.
Output >
In the following image, you can see that I have created a user “temp” with an expiry date.
Example 7: Create a User With a Comment Using the “useradd” Command in Linux
You can create a new user with a comment, as it is helpful for future reference and also to note something important. Here, I will create a new user called “user7“ with a comment using the useradd command. You can do the same by following the steps below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt:
sudo useradd -c "For example 7, this user is created" user7
➌ Now, press the ENTER button.
➍ Type your Password.
➎ Press ENTER again.
➏ Now, type the following command into the command prompt:
cat /etc/passwd | grep user7
➐ Press ENTER button.
Output >
In the image below, you can see that I have created a new user named “user7” with the comment “For example 7, this user is created.”
Example 8: Specify the Shell for a User With the “useradd” Command in Linux
You can specify a shell for a user by using the useradd command followed by the option -s. In this section, I will create a new user called “user8“ with a specific shell called “/bin/sh.” You can do the same by following the steps below:
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt:
sudo useradd -s /bin/sh user8
➌ Now, press the ENTER button.
➍ Type your Password.
➎ Press ENTER again.
➏ Now, type the following command into the command prompt:
cat /etc/passwd | grep user8
➐ Press ENTER button.
Output >
In the following image, You can see that I have created a new user “user8” under the /bin/sh shell.
Example 9: Set an Encrypted Password for the New User
At the time of creating a user, you may set a password by using the useradd command followed by the option -p. Here, I will set a password for a new user called “user9“. You can do the same by following the steps below:
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt:
sudo useradd -p abcdef123 user9
➌ Now, press the ENTER button.
➍ Type your Password.
➎ Press ENTER again.
➏ Now, type the following command into the command prompt:
sudo grep user9 /cat/shadow
➐ Press ENTER button.
Output >
You can see that I have created a new user named “user9” along with setting its password.
Example 10: Change the Default “useradd” Values
You can see the default value (of the useradd command) by using the useradd command followed by the option -D.
useradd -D
In the image below, you can see the default value for the useradd command.Now, I will change the default value for the shell from /bin/sh to /bin/bash. You can do the same by following the steps below:
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt:
sudo useradd -D -s /bin/bash
➌ Now, press the ENTER button.
➍ Type your Password.
➎ Press ENTER again.
➏ Now, type the following command into the command prompt:
sudo useradd -D | grep -i shell
➐ Press ENTER button.
Output >
In the following image, you can see that the shell value is changed to /bin/bash.
Example 11: Create a System User Using the “useradd” Command in Linux
The system user, also known as the root user, is responsible for creating normal users and is automatically created when the operating system is first installed. Additionally, you can also create system users for specific applications and programs. In this section, I will create a new system user called “user11“. You can do the same by following the steps below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt:
sudo useradd -r user11
OR,
sudo useradd --system user11
➌ Now, press the ENTER button.
➍ Type your Password.
➎ Press ENTER again.
➏ Now, type the following command into the command prompt:
id user11
➐ Press ENTER button.
Output >
In the Image below, you can see that I have created a system user named “user11”.
Example 12: Help Page for the “useradd” Command in Linux
In Linux, the help page for the useradd command contains a brief description. You can see the help page by following the steps that I mentioned below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt:
Useradd --help
➌ Now, press the ENTER button.
Output >
In the following image, you can see the help page of the useradd command in Linux.
Conclusion
In this article, I’ve shown the operation of the useradd command. I’ve also provided some relevant examples. You should be able to use the useradd command effectively on your own if you properly read this article and follow the above-mentioned guidelines. I hope these examples will help you on your journey to be a power user of the Linux command line.
Similar Readings