Running useradd command in the Linux terminal enables you to execute or perform various significant functions. For instance, you can edit /etc/passwd, /etc/shadow, /etc/group, and /etc/gshadow files for the user accounts that are newly built. Moreover, you can generate a home directory for the new user with the useradd command. Also, this command helps you to set permission and ownerships to the home directory and much more. We have come up with this guide to provide you with insights into creating or adding users in Linux by utilizing the ‘useradd’ command. You can encounter a slightly different version of the ‘useradd’ command in some Linux distributions. Thus, it will be best to first go through your documentation before you make use of our guide to create new user accounts in Linux.
Let us highlight the basic syntax of the useradd command –
# useradd [options] username
How to Create a New User in Linux?
For adding or creating a new user, you can utilize either ‘useradd‘ or ‘adduser‘ command. Further, you can add only one user at a time. The command goes like –
[[email protected] ~]# useradd username
For instance, if you wish to add a user Linux called ‘technet’, you need to enter the command –
[[email protected] ~]# useradd technet
The username must be unique, i.e., it must be distinct from other usernames existing on your system. It serves as a user login name and is employed by a user for logging in to the system.
Add a User with Home Directory
It can be possible that the user either has been assigned a home directory or not according to the /etc/defaults/useradd file and settings file. In order to force the addition of the new user with home directory, use the useradd command in Linux as –
sudo useradd -m technet
After entering this command, a /home/test folder for the user technet will get created.
Create a User using Different Home Directory
If you wish the home folder of an user to be at a different place than the default, you need to utilize the -d switch in the following command –
sudo useradd -m -d /test technet
After entering this command, a folder will be created for the user technet under the root folder, which will be called technet.
Add User with Distinct User ID
Each user holds its own UID in Linux. The UID stands for Unique Identification Number. Whenever you create a new user account in Linux, it gets the userid as 500, 501, 502, and so on by default. However, you have the possibility to create or add users with custom user id by employing the ‘-u‘ option. For instance, to create a user ‘troy’ with the custom user id ‘1006’, the command will be entered as –
[[email protected] ~]# useradd -u 1006 troy
You can verify the creation of the user account with a defined userid (1006) by executing the command-
[[email protected] ~]# cat /etc/passwd | grep troy
You will get the output as –
Also, you need to ensure that the value of a user ID is distinct from the ones already created on the system.
Create User with Specific Group ID
Just like the UID, each user holds its own GID. The GID stands for Group Identifier. You can create or add users with specific group IDs with the help of -g option.
For instance, let’s add a user ‘ronak’ with a specific UID and GID simultaneously with the help of ‘-u‘ and ‘-g‘ options.
[[email protected] ~]# useradd -u 1005 -g tech ronak
Now, you can verify the assigned user id and group id in ‘/etc/passwd‘ file as –[[email protected] ~]# cat /etc/passwd | grep ronak
After entering this command you will get result as –
Further, in order to verify the user’s GID, you can enter the following command –
[[email protected] ~]# id -gn ronak
Add User in Linux with Account Expiry Date
When we add or create a user account by using the ‘useradd‘ command, by default the user account never expires. This means the expiry date is set to 0 by default ( 0 means never expired). However, if you wish to set expiry date, you can utilize the ‘-e‘ option. The ‘-e‘ option will help you to set the expiry date in YYYY-MM-DD format. Usually, this is beneficial when you are looking to create temporary accounts for a particular duration of time.
For instance, you need to create a user ‘sonar’ with account expiry date i.e. 18th December 2021 in YYYY-MM-DD format. You can do this by entering the following command –
[[email protected] ~]# useradd -e 2021-12-18 sonar
Next, if you want to verify whether the expiry date has been implemented or not to the age of the user account, you can run the following command and get a confirmation –
[[email protected] ~]# chage -l sonar
Create a User without Home Folder
In case the login.defs file has the CREATE_HOME yes option that is set already when you generate a user in Linux, the home folder gets simultaneously created. However, for creating a user that does not have a home folder regardless of the settings, you can enter the following command as-
sudo useradd -M test
Add a User to Multiple Groups
For adding a user to multiple groups, you need to use the ‘-G‘ option. Every group name has to be parted by a comma but with no intervening spaces. Let’s take an example where you add a user ‘technet‘ into multiple groups – admins, webadmin, and developer. To do this, you need to enter the following commands –
[[email protected]:~]# groupadd admins
[[email protected]:~]# groupadd webadmin
[[email protected]:~]# groupadd developers
[[email protected]:~]# usermod -a -G admins,webadmin,developers technet
[[email protected]:~]# useradd -G admins,webadmin,developers paddy
You can verify whether the multiple groups have got assigned to the user with the id command by entering the following command –
[[email protected] ~]# id technet
And, you must get the output as –
Add a New User with Expiry Date of Password
You also have the option of setting the expiry date of your password while adding or creating a new user. To do so, you can utilize the ‘-f‘ argument. The ‘-f‘ argument helps in defining the number of days after a password expires. Generally, the password expiry value is set at -1 that implies no expiry date.
Let’s take an instance where you need to set an account password expiry date i.e. 56 days on a user ‘monal‘. In this case, the command you need to enter will be as –
[[email protected] ~]# useradd -e 2014-04-27 -f 56 monal
Create a User with Specific Shell Login
There can be times when you need to assign different shells to your users. So, you can employ the ‘-s‘ option for doing so. Follow the command –[[email protected] ~]# useradd -s /sbin/nologin technet
You can verify whether the shell gets assigned to the user or not in the ‘/etc/passwd‘ file by entering the following command –
[[email protected] ~]# tail -1 /etc/passwd
And, then you will receive the output as –
Add User using Custom Comments
For adding custom comments like user’s full name, phone number, etc. to /etc/passwd file while you add user Linux, you need to use the ‘-c‘ option. The comment will be appended in a single line without leaving any spaces. For instance, entering the following command will enable you to add a user ‘jason’ and its full name, Jason Stan, into the comment field –
[[email protected] ~]# useradd -c “Jason Stan” jason
You can view your comments in the ‘/etc/passwd‘ file in the comments section by entering the command –
[[email protected] ~]# tail -1 /etc/passwd
The above command will display the following output –
How to Switch Users
You can test your new user’s account by entering the following command into a terminal window:
After entering this command, the user will be switched to the test account. And, it will be assumed that you have created a home folder. So, further, you will get located in the home folder for that user.
How to Add a User to a Group in Linux
You might be required to assign specific groups to the user who joins your company. This is done to let that user have access to the files and folders that the other members of his/her team use. For instance, Joe joins as an accountant in your company, then you can execute the following command to let him have access to the accounts group –
sudo useradd -g accounts joe
How to Change Passwords in Linux
Once you add a new user with the useradd command, it automatically gets created in a locked state. For unlocking the user account, you need to keep a password for its account. The password can be set with the help of ‘passwd‘ command as –
[[email protected] ~]# passwd technet
After entering this command, the system will prompt you to enter a new password and further confirm it.
When a new user gets created, the ‘/etc/passwd‘ file adds its entry automatically. This file stores all the user’s information.
How to Delete a User from Linux
For deleting a user from Linux, enter the following command –
userdel “name of the user”
For instance –
How to Analyze the /etc/passwd File
Once a new user is created, the /etc/passwd file adds all its details. You can see the information about a specific user by employing the grep command as –
grep smithj /etc/passwd
After entering the above command, you will get a colon-separated list of fields about every user. These fields will tell you about the following aspects –
- Encrypted password (which will always display as x)
- User’s group id
- Full name of the user
- User’s home directory
- Login shell
You must have got insightful details by now about utilizing the useradd command to create a user in Linux and perform related functions. There are numerous desktop Linux distributions available that render a graphical tool for creating or adding users. However, it is always a great idea to learn to perform such functions by employing a command line. This will enable you to transfer your abilities from one distribution to another without requiring you to learn new user interfaces. We at Serverwala along with the technical experts, are always available to help you out in case you encounter any issue while executing the commands discussed in this guide. You can reach out to us via various communication modes and share your concern in order to obtain accessible solutions.