How to add users in linux

青灯夜游
Release: 2021-12-21 15:27:59
Original
16120 people have browsed it

In Linux, you can use the useradd command to add users. The function of this command is to create a new user and add a new system user. The basic syntax format is "useradd [option] username".

How to add users in linux

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

In Linux, you can use the useradd command to create a new user and add a new system user.

The basic format of this command is as follows:

[root@localhost ~]#useradd [选项] 用户名
Copy after login

The commonly used options of this command and their respective meanings are shown in Table 1.

Table 1 Useradd command common options
Option Meaning
-u UID Manually specify the user's UID, pay attention to the UID range (not less than 500).
-d Home directory Manually specify the user's home directory. The home directory must be an absolute path, and if you need to manually specify the home directory, you must pay attention to the permissions;
-c User instructions Manually specify the /etc/passwd file The descriptive content of the fifth field in each user information can be configured at will;
-g Group name Manually specify the user's initial group. Generally, the group with the same name as the user is used as the user's initial group. When a user is created, the initial group will be established by default. Once specified manually, the system will not create this default initial group directory.
-G Group name Specifies the user's additional group. When we add users to other groups, we generally use additional groups;
-s shell Manually specify the user's login shell, the default is /bin/bash;
-e Date Specifies the expiration date of the user, in the format of "YYYY-MM-DD". That is, the eighth field of the /etc/shadow file;
-o allows the created users to have the same UID. For example, execute the "useradd -u 0 -o usertest" command to create user usertest. Its UID is the same as the root user's UID, both are 0;
-m When creating a user, it is mandatory to create the user's home directory. When creating a system user, this option is the default;
-r Create a system user, that is, the UID is between 1 and 499, for use by system programs. User. Since system users are mainly used to configure permissions for services required to run the system, the creation of system users does not create a home directory by default.

In fact, the system has provided us with a lot of default values. Without special requirements, users can be successfully created without using any options. For example:

[root@localhost ~]# useradd lamp
Copy after login

This command line means creating a normal user lamp.

Don’t underestimate this simple command, it will complete the following operations:

  • Create a row of data related to the lamp user in the /etc/passwd file :

[root@localhost ~]# grep "lamp" /etc/passwd lamp:x:500:500::/home/lamp:/bin/bash
Copy after login

You can see that the user's UID is calculated starting from 500. At the same time, the user's home directory is specified as /home/lamp/ by default, and the user's login shell is /bin/bash.

  • A new line of data related to the lamp user password has been added to the /etc/shadow file:

[root@localhost ~]# grep "lamp" /etc/shadow lamp:!!:15710:0:99999:7:::
Copy after login

Of course, this user has not yet Set the password, so the password field is "!!", which means that the user does not have a reasonable password and cannot log in normally. At the same time, the time field will be set according to the default value. For example, the password is valid for 99999 days, and the system will prompt the user that "the password is about to expire" 7 days before the password expires.

  • Create a group in the /etc/group file with the same line as the user name:

[root@localhost ~]# grep "lamp" /etc/group lamp:x:500:
Copy after login

This group will be used as the name of the new user Initial group.

  • Add a new line of password information related to the new group in the /etc/gshadow file:

[root@localhost ~]# grep "lamp" /etc/gshadow lamp:!::
Copy after login

Of course, we did not set The group password is set, so there is no password and no group administrator.

  • The user's home directory and mailbox are created by default:

[root@localhost ~]#ll -d /home/lamp/ drwx------ 3 lamp lamp 4096 1月6 00:19 /home/lamp/ [root@localhost ~]#ll /var/spod/mail/lamp -rw-rw---- 1 lamp mail 0 1月6 00:19 /var/spool/mail/lamp
Copy after login

Note that the permissions of these two files must be given to the lamp user. permissions.

  • Copy the configuration files in the/etc/skeldirectory to the new user’s home directory.

As you can see, the process of creating a user with the useradd command actually modifies several files or directories related to the user. These files have been introduced in detail in the previous chapter.

In addition to creating users by default, we can also use various options of the useradd command to customize the users to be created, for example:

[root@localhost ~]# groupadd lamp1 #先手工添加lamp1用户组,因为我一会儿要把lamp1用户的初始迎指定过来,如果不事先建立,则会报告用户组不存在 [root@localhost ~]# useradd -u 550 -g lamp1 -G root -d /home/lamp1 -c "test user" -s /bin/bash lamp1 #在建立用户lamp1的同时,指定了UID(550)、初始组(lamp1)、附加组(root)、家目录(/home/lamp1/)、用户说明(test user)和用户登录Shell(/bin/bash) [root@localhost ~]# grep "lamp1" /etc/passwd /etc/shadow /etc/group #同时查看三个文件 /etc/passwd:lamp1:x:550:502:test user:/home/lamp1:/bin/bash #用户的UID、初始组、用户说明、家目录和登录Shell都和命令手工指定的一致 /etc/shadow:lamp1:!!:15710:0:99999:7::: #lamp1用户还没有设定密码 /etc/group:root:x:0:lamp1 #lamp1用户加入了root组,root组是lamp1用户的附加组 /etc/group:lampl:x:502: #GID为502的组是lamp1组 [root@localhost ~]#ll -d /home/lamp1/ drwx------ 3 lamp1 lamp1 4096 1月6 01:13 /home/lamp1/ #家目录也建立了,不需要手工建立
Copy after login

Using the above 2 methods, users can be successfully created. Often, there is no need to manually specify anything at all, since using the default values will satisfy our requirements. Have you ever thought about where the default values of the useradd command are stored and whether they can be modified manually?

The answer is yes. There are two main default value files that the useradd command refers to when adding users, namely /etc/default/useradd and /etc/login.defs.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of How to add users in linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!