List of content you will read in this article:
As you know, Linux is a system where multiple people can interact with one system at one time. In order to reach this objective, Linux uses a useradd command. This command’s primary aim is to add the accounts of the new users to your Linux system.
If you are a system administrator, you have the authority to manage users and the groups of the system. You can create or remove the users as well. Here, we are going to talk about creating several new accounts with the use of useradd command. Along with that, you will see many more functions.
How to Create Users in Linux?
The useradd Command
Here is the general syntax to begin with:
sudo useradd test_user
The useradd command here creates a new account. It runs according to some of the 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 range of user IDs, password expiration policy, and much more.
Now, in order to login as a new user, you have to set the user password first. How do you do that?
Simply, write this command:
After you execute this command, you need to enter the details. Always set a password with alphanumerics.
Changing password for user username.
Retype new password:
passwd: all authentication tokens updated successfully.
Now, have a look at the list of commands to fulfill several other requirements.
How to create a user with a specific home directory?
The useradd command automatically creates a home directory of the user in /home. If you want 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 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.
The 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.
useradd -g 1000 test_user
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 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 users UID, type this:
id -u username
How to create a user and assign multiple groups?
The Linux operating system inhibits two kinds of groups. Primary group and secondary group. The secondary group is also called a supplementary group.
Every user can be assigned 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 secondary group ‘wheel’ and ‘docker’.
sudo useradd -g users -G wheel,developers username
If you want to check the user groups, type the following command:
uid=1002(username) gid=100(users) groups=100(users),10(wheel),993(docker)
How to create a user with a changed login shell?
For your information, a new user’s login shell is always set according to the one depicted in /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 -s (- -shell) option.
Now here is a syntax to create a user we change the login shell:
-s /bin/sh test_user
How to create a user with an expiry date?
There are certain times 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 will be able to create a user with the expiry date of March 30,2021.
If you want to verify the user account expiry date then use the following command:
sudo chage -l username
How to create a user with a comment?
In case you need to add a description for a new user use -c (- - comment) option.
sudo useradd -c “This is a test user” test_user
How to create a user without mentioning the home directory?
To full fill 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 set an unencrypted password for the user?
In order to set an unencrypted password use the following command:
sudo useradd -p test_password test_user
How to create a system user?
Particularly there is no solid technical difference between a regular user and the system user. When we talk about system users, they are created while the installation of the operating system.
To create a system user account use the -r (- - system) option.
sudo useradd -r username
For your information, the system users do not have an expiry date.
How to change the default useradd values?
You can do that by using -D, (- - defaults option).
Manually, just edit the values in /etc/default/useradd file.
Use the following command to view the default option type:
How to display help?
Using the following command, you can retrieve the help section of useradd command.
sudo useradd --help
We have presented information about using useradd command in order to create several user accounts. The above-stated article showcases numerous ways that will make your working easier in the Linux system.
We hope that the information adds value to your knowledge and widens your horizons.