In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use the useradd command in the linux system, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Useradd is a command for Linux to add new users, which provides an easy way to create new user accounts and set up the user's HOME directory structure at once.
Function
The useradd command is used to create or update user information in Linux.
The useradd command is a difficult command to use (low level utility for adding users), so administrators are advised to use the adduser command in Debian distributions. In fact, the adduser command is just a script file that calls the useradd command.
This article will analyze the options related to groups and home directories in detail. And introduce the common writing method of useradd command in the way of an example.
Syntax and basic options
Note: this article is not a complete document, so only some commonly used options are listed for explanation.
Useradd [option] username [option]:-d specifies the directory where the user logs in. -g initial group. -G is not an initial group. -m automatically creates the user's home directory. -M do not create the user's home directory. -N do not create a group with a user name. -s specifies the shell that the user will use after logging in.
* * describe user groups in detail
**
First of all, we need to figure out, what is the initial group? In a nutshell, the fourth field of each line in the / etc/passwd file specifies the user's initial group. The user has permissions in the original group immediately after logging in.
Let's look at the usage of the group option through different commands:
$sudo useradd tester1
No group-related parameters are used. By default, a group with the same name will be created when the user tester1 is created. The initial group of the user tester1 is the new group.
$sudo useradd tester2-N
This time we used the-N option, that is, do not generate a group with the same name as the user. Looking at the / etc/passwd file, it is found that the initial group ID of tester2 users is 100. Where did this 100 come from? Is there a group with an ID of 100? In fact, 100 as the default value of-N is written in the configuration file. This value is the same regardless of whether there is a group with an ID of 100. Of course, we can also change this 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 this group. OK, now check the / etc/passwd and / etc/group files to see if any new groups have been created? Who is the initial group of tester3? No group with the same name as tester3 was created this time. The initial group of the 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 results are too different, please be sure to check the / etc/passwd and / etc/group files. Because not only is the group tester4 created this time, it is also the initial group of the user tester4. The only difference from tester1 is that tester4 is added to the sudo group.
In practical use, the scenarios of tester3 and tester4 are relatively common and need to be distinguished according to the actual situation.
Catalogue of detailed narrators
The handling of the user's home directory with the Useradd command is confusing. Let's learn how to use different options related to the home directory through experiments:
$sudo useradd tester1
Let's revisit the command to create the user tester1. It does not create a directory called tester1 as the home directory for the user tester1, but we open the / etc/passwd file and find that the tester1 record contains the home directory / home/tester1. Tester1 ❌ 1005:1005::/home/tester1: it's incredible, but that's how the command works.
$sudo useradd-m tester5
To create a user's home directory while creating a user, you must specify the-m option.
$sudo useradd-d / home/abc tester6
We want to specify the home directory ourselves, and do not generate the directory abc at this time
$sudo useradd-d / home/abcd-m tester7
The directory abcd is generated, and files exist by default in the directory
Common use cases
Case 1: create a user with a home directory that can log in to bash
$sudo useradd-m-s / bin/bash tester1
Case 2: specify the path to create the user's home directory
$sudo useradd-m-d / home/xxx tester2
The / home/xxx directory is created.
Case 3: create a user who does not have a home directory and cannot log in
$sudo useradd-s / sbin/nologin tester3
Case 4: add users to different user groups when creating
$sudo useradd-m-G xxx,sudo tester4
Note that group names are separated by commas and cannot have spaces.
Thank you for reading this article carefully. I hope the article "how to use useradd commands in linux system" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.