Linux is a system where multiple users can interact within a single system simultaneously. But how do you create users in Linux? In order to do this, Linux uses a useradd command. If you are a system administrator, you have the authority to manage users and the groups as you can create or remove the users as well. Here, we will discuss the ways to create several new accounts with the use of useradd command. Moreover, you will learn many more functions associated with the command.
Here is the general syntax to begin with:
sudo useradd test_user
useradd command here creates a new account. It runs on the basis of some default values mentioned in the /etc/default/useradd file. Along with this, the command also runs through the /etc/login.defs file. This file imbibes certain information like the range of user IDs, password expiration policy, and much more. Now, in order to log in as a new user, you have to set the user password first. How do you do that?
Simply, write this command:
Once you execute this command, enter the details. Always set a password with alphanumerics.
The output screen shows the following:
Changing password for user username.
Retype new password:
passwd: all authentication tokens updated successfully.
Now, have a look at the list of commands to fulfil several other requirements.
How to create a user with a specific group ID?
When we talk about groups in Linux, they are primarily used in the administration and the usage of several user accounts. Groups generally define certain privileges. For instance, read, write, and execute permissions. These permissions are given to a particular resource to be shared among other users. Linux add user to group
useradd command automatically creates a group with the name as that of the username with the exact copy of the GID as that of the UID. Now, -g (- -gid) helps to create a user that has a certain login group. You have to mention the GID number or the group name. This is how Linux adds users to a group.
useradd -g 1000 test_user
How to create a user with a specific home directory?
The useradd command automatically creates a home directory of the user in /home. In order to change the location, use -d (- - home). Now, look at the following command to create a user having /opt/username as the home directory.
sudo useradd -d /home/test_user test_user
How to create a user and assign multiple groups?
The Linux operating system inhibits two kinds of groups. Primary group and secondary group. A secondary group is also called a supplementary group. Every user can be allocated to only one primary group. Whereas in the case of secondary groups, the number is either zero or more. Have a look at the below-stated command mentioning both the primary group ‘users’ as well as a secondary group ‘apple’ and ‘kiwi’.
sudo useradd -g users -G apple,developers username
If you want to check the user groups, type the following command:
uid=1002(username) gid=100(users) groups=100(users),10(apple),993(kiwi)
How to create a user with a specific user ID?
As you know, the users are known by their respective UID and username. When we talk about User Identifier, UID, it is simply a positive integer that is allocated to every user in the Linux system. It helps to understand the different types of actions that a user is able to perform on the system.
In the login.defs file, a range of user IDs is mentioned. The system automatically allocates the available user identifier from this range. In order to execute the command, use the following syntax:
useradd -u 1234 test_user
If you further want the verification of the user UID, type this:
id -u username
How to create a user with a new login shell?
For your information, a new user’s login shell is always set according to the one depicted in the /etc/default/useradd file. You will notice that in some cases the default shell is /bin/sh whereas in some others it is specified in /bin/bash.
If you want to mention the login shell of a new user, kindly use the -s (- -shell) option.
Now here is a syntax to create a user with a changed login shell:
-s /bin/sh test_user
How to set an unencrypted password for the user?
If you are looking forward to setting a password, use the following command:
sudo useradd -p test_password test_user
How to create a user with an expiry date?
There may come a situation when you would want to know the expiry date of the user accounts. To do that use the following option:
-e (- - expiredate)
You can also create temporary accounts with this option. Here is the syntax to create a user with an expiry date:
useradd -e 2021-03-05 test_user
By using this command you can easily create a user with an expiry date of March 30, 2021. The user account expiry date can be verified by using the following command:
sudo chage -l username
How to create a user without mentioning the home directory
To fulfil this need, type the following command:
sudo useradd -M test_user
This command helps you to create a user without a specific home directory.
How to create a user with a comment?
In case there is a requirement to add a description for a new user use the -c (- - comment) option. For instance,
sudo useradd -c “This is a test user” test_user
How to create a system user?
Particularly there is no solid technical difference between a regular user and the system user. When you install the operating system, the system users are created. To create a system user account, follow the syntax.
sudo useradd -r username
For your information, the system users do not have an expiry date.
How to display help?
Using the following command, you can retrieve the help section of useradd command.
sudo useradd --help
How to change the default useradd values?
You can do that by using the -D, - - defaults option. Manually, just edit the values in the /etc/default/useradd file. Use the following command to view the default option type:
The article will go through the steps on how to create users in Linux. We will go through numerous ways how to use the useradd command to create and add users to different groups. We hope that the data provided helps you to clear your doubts.
People also read: