[Linux] Detailed introduction to the operation of users and groups

The Linux operating system is a multi-user and multi-tasking operating system that allows multiple users to log in to the system at the same time and use system resources. There are two types of user accounts under the Linux operating system, ordinary user accounts and super user accounts (root), and ordinary users They can only do ordinary work in the system, and can only access the files they own or have permission to execute. The super user account is also called an administrator account. Its task is to manage ordinary accounts and have absolute control over the system.

1. Introduction to Linux user account files

The user's account information and group information are stored in the user's account file and group file respectively, which are
/etc/passwd, /etc/shadow, /etc/group respectively:

/etc/passwd, we can use vim or cat command to view this The file, the created user account and its related information are all stored in this file,
insert image description here
insert image description here
as shown in the figure, each line represents a user's information, the first user is root, and then some standard accounts, the shell of this type of account is /sbin/ nologin means no local login permission, and the last line is a common account created by the system administrator: test

Each line in the passwd file is divided into 7 fields with ":", and the contents of each field are as follows:

用户名:加密口令:UID:GID:用户的描述信息:主目录:命令解释器(登录shell)

2. useradd command

To create a new user, you can use the ueradd or adduser command, the format is:

useradd [-选项] test #新建一个名为test的用户

Some common options for useradd:
-c user's commentary information
-d specifies the user's home directory
-e disables the date of the account, in the format of YYYY-MM-DD
-g the group name or GID to which the user belongs
-G the subgroup to which the user belongs List, multiple groups are separated by ","
-m If the user's home directory does not exist, create it
-s Specifies the user's login shell, the default is /bin/bash
-u Specifies the user's UID, which must be unique, And greater than 999
-p encrypted password

example:

groupadd -g 1088 group1 #新建组group1,组的GID为1088
useradd -u 1088 -d /home/user3 -s /bin/bash -p 123456

3. passwd command

The command to set a user account password is passwd. Super users can set passwords for themselves and other users, while ordinary users can only set passwords for themselves. The format is:

passwd [-选项] [指定的用户名]

Some common options of passwd:
-I lock (deactivate) user account
-u password unlock
-d set the user's password to empty, unlike accounts without passwords, accounts without passwords cannot log in to the system, and passwords are empty The account can
-f force the user to change the password at the next login
-n specifies the minimum lifetime of the password
-x specifies the maximum lifetime of the password
-w the number of days before the password expires in advance warning
-i the number of days after the password expires Use account
-S to display brief status information for account passwords

example:

passwd #用户修改自己的口令,直接输入passwd命令
passwd test #修改test的账户密码

4.chage command

The chage command is used to change user password expiration information.

Common options of the chage command:
-l lists the various values ​​​​of the account password
-m specifies the minimum password lifetime
-M specifies the maximum password lifetime
-I the number of days after the password expires to deactivate the account -E the date when the user account expires and
becomes invalid-
d Set the password last modified date

Example (set the minimum password lifetime of the test user to 5 days, the longest password lifetime to 30 days, and remind the user to change the password 5 days before the password expires):

chage -m 5 -M 30 -W 5 test #设置test用户的最短口令存活期为5天,最长口令存活期为30天,口令到期前5天提醒用户修改口令

5. usermod command

The usermod command is used to modify the attributes of user accounts, the format is:

usermod [-选项] 用户名

Common options of usermod:
-c Fill in the user's remark information
-d -m The option -m and -d are used together to specify the user's home directory and automatically migrate the old data to the past
-e Expiration time of the account, the format is YYYY-MM-DD
-g Change the user group to which the user belongs
-G Change the extended user group
-L Lock the user, prohibit logging into the system
-U Unlock the user, allow logging into the system
-u Modify the user's UID

We can view the user's default information in advance with the following command:

id test #查看test用户的默认信息

example:

usermod -u 8888 test #修改用户test的uid为8888
usermod -L test #锁定test用户,无法登录
usermod -U test #解锁test用户,允许登录

6. userdel command

To delete a user, you can directly delete the line corresponding to the user to be deleted in the /erc/passwd and /etc/shadow files, or use the userdel command to delete, the format is:

userdel [-r] 用户名

7. Create and delete group commands

We use the groupadd and grouopdel commands to create and delete groups

groupadd testgroup #新建一个名为testgroup的组
groupdel testgroup #删除名为testgroup的组

8. groupmod command

We use the groupmod command to modify the group

groupmod [-选项] 组名

Common options for the groupmod command:
-g gid Change the GID of the group to gid
-n test2 Change the name of the group to test2
-o Force acceptance of changing the GID of the group to a duplicate number

The above is the introduction to the operation of users and groups in this article. If this article is helpful to you, remember to like, collect and pay attention~

Guess you like

Origin blog.csdn.net/beixige/article/details/130440047