linux explains the basic usage of the useradd command

  • 2020-06-03 08:52:39
  • OfStack

linux explains the basic usage of the useradd command

Summary:

useradd is a basic command in Linux, but it's not intuitive to use. So much so that an adduser command was added to Ubuntu to simplify adding users. This article describes some of my test results while learning to use the useradd command.

Note: All experiments in this paper were performed on Ubuntu14.04.

function

In Linux, the useradd command is used to create or update user information.

The useradd command is one of the more difficult commands (low level utility for adding users), so administrators are recommended to use the adduser command in the Debian releases. The adduser command is just a script file that calls the useradd command.

This article examines in detail the options associated with groups and home directories. The common useradd command writing method is introduced as an example.

Syntax and basic options

Note: This article is not a complete document, so only some common options are listed to illustrate.


useradd [option] username

[option]:

-d< Login directory >  Specifies the directory where the user logged in. 

-g< group >  Initial group. 

-G< group >  Non-initial group. 

-m  Automatically creates the user's home directory. 

-M  Do not create a user's home directory. 

-N  Do not create groups with user names. 

-s  Specifies what the user will use after logging in shell . 

Detail user groups

The first thing we need to figure out is, what is an initial group? Simply put, in the /etc/passwd file, the fourth field of each line specifies the user's initial group. The user has rights in the original group immediately after logging in.

Let's look at the usage of the group option using different commands:


$ sudo useradd tester1

No group-related parameters are used, and by default a group with the same name is created when user tester1 is created. The initial group for user tester1 is this new group.


$ sudo useradd tester2 -N

This time we used the -N option, which does not generate groups with the same name as the user. Looking at the /etc/passwd file, the initial group ID for tester2 users is 100. Where did this 100 come from? Is there an ID group of 100? The default value of 100 for -N is written in the configuration file. This value is true with or without groups with ID of 100. Of course, we can change the default value by modifying the configuration file!


$ sudo useradd tester3 -g sudo

sudo is a very powerful group and I decided to add tester3 to it. Ok, now go to check 1 /etc/passwd and /etc/group files to see if any new groups have been created? Who was the original group for tester3? No group with the same name as tester3 was created this time. The initial group for user tester3 becomes sudo.


$ sudo useradd tester4 -G sudo

Compared to the previous command we just replaced the lowercase g with the uppercase G. But the result is too different, please make sure to check /etc/passwd and /etc/group files. Not only is group tester4 created this time, it is also the original group for user tester4. The only difference between tester4 and tester1 is that tester4 is added to sudo group.

In actual use, both tester3 and tester4 scenarios are fairly common and need to be distinguished according to the actual situation.

Go into the home catalog

The Useradd command's handling of the user's home directory is confusing, so let's experiment to see how different options related to the home directory can be used:


$ sudo useradd tester1

Let's revisit the command to create user tester1. It won't create a directory named tester1 for user tester1 as a home directory, but when we open the /etc/passwd file, we find that the record for tester1 contains the home directory /home/tester1.


tester1:x:1005:1005::/home/tester1:

This is incredible, but this is how this command works.


$ sudo useradd -m tester5

To create the user's home directory at the same time as creating the user, you must specify the -ES123en option.


$ sudo useradd -d /home/abc tester6

We want to specify the home directory ourselves, so abc is not generated at this point


$ sudo useradd -d /home/abcd -m tester7

At this point the directory abcd is generated and there are files in the directory by default

Common use case

Case 1: Create a user with a home directory and access to bash


$ sudo useradd tester1
0

Case 2: Specifies the path to create the user's home directory


$ sudo useradd tester1
1

The /home/xxx directory is created.

Case 3: Create a user that does not have a home directory and cannot log in


$ sudo useradd tester1
2

Case 4: Create users into different user groups


$ sudo useradd -m -G xxx,sudo tester4

Note that the group names are separated by commas and cannot have Spaces.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: