Detailed explanation of Linux user and user group commands

I. Overview

In Linux, the user's operation is also crucial.

For the security of the system, it is necessary to know how to use Linuxthe relevant commands of the user and user group.

2. User management commands

2.1 useradd is used to add new users

1) Basic syntax:

# 直接添加用户
useradd 选项 用户名

# 添加新用户的同时把用户分配到到某个组
useradd -g 组名 用户名

2) Description of common options

option name significance
-c Specify an annotative description
-d Specify the user's home directory. If this directory does not exist, use the -m option at the same time to create the home directory
-g Specifies the user group to which the user belongs
-G Specifies additional groups the user belongs to
-s Specifies the user's login shell
-u Specify the user ID of the user. If there is the -o option at the same time, the ID number of other users can be reused
-g Set the group name for the new user

2.2 passwd set user password

1) Basic syntax:

passwd 选项 用户名

2) Description of common options

option name significance
-l i.e. disable the account
-u password unlock
-d make account passwordless
-f Force user to change password at next login

2.3 id View the user's uid, gid, group

id 用户名

2.4 View which users have been created

cat  /etc/passwd

Created users will exist in the /etc/passwd file

2.5 userdel delete user

1) Basic syntax:

# 删除用户但保存用户主目录
userdel  用户名

# 用户和用户主目录,都删除
userdel -r 用户名

2) Description of common options

options Function
-r When deleting a user, all files related to the user are deleted.
-f Force delete, even if the user is currently logged in;

2.6 usermod modify user

1) Basic syntax:

# 删除用户但保存用户主目录
usermod -g 用户组 用户名

# 用户和用户主目录,都删除
userdel -r 用户名

2) Description of common options

options Function
-g Modify the user's initial login group, the given group must exist

2.7 who View current user information

# 显示登录用户的用户名
who am i

2.8 su switch user

# 切换用户,只能获得用户的执行权限,不能获得当前用户环境变量,而是获取原用户的环境变量
su 用户名称
# 切换到用户并获得该用户的环境变量及执行权限
su - 用户名称

Remember to log out after switching users, instead of switching to the next user directly, otherwise it will cause a lot of processes.

Exit command:

exit

2.9 sudo set ordinary users to have root authority

# 在/root目录切换成test用户
su test

# 现在test用户想冒充root用户在/root文件夹下创建文件夹
sudo mkdir module

It is found that even if sudo is used above, root privileges cannot be obtained.

This is because there is a file that records who can obtain rootpermissions, and those who are not recorded in the file sudocannot obtain permissions even if they use it.

So you need to switch back to rootthe user at this time to modify this file:

vim /etc/sudoers

insert image description here

rootConfigure rootthe users who can obtain permissions on the next line.

At this point a save and exit command must be enforced::wq

3. User group management commands

Each user has a user group, and the system can centrally manage all users in a user group.

Different Linuxsystems have different regulations on user groups.

The followingLinux users belong to the user group with the same name, and this user group is created at the same time when the user is created.

The management of user groups involves adding, deleting and modifying user groups. The addition, deletion, and modification of groups are actually /etc/groupupdates to files.

3.1 groupadd add new group

1) Basic syntax:

groupadd 选项 用户组名

2) Description of common options

options Function
-g Modify the user's initial login group identification number (GID), the given group must exist
-o It is generally used together with the -g option, indicating that the GID of the new user group can be the same as the GID of the existing user group in the system

3.2 groupdel delete group

groupdel 组名

3.3 groupmod modify group

1) Basic syntax:

groupmod -n 新组名 老组名

2) Description of common options

options Functional description
-n Specifies the new group name for the workgroup
-g Modify the user's initial login group identification number (GID), the given group must exist
-o It is generally used together with the -g option, indicating that the GID of the new user group can be the same as the GID of the existing user group in the system

3.4 View which groups have been created

cat /etc/group

3.5 Check which group the user belongs to

groups 用户名称

3.6 gpasswd add user to group

gpasswdYes Linuxunder Workgroup Files /etc/groupand /etc/gshadowAdministrative Tools.

Used to add or remove a user from a group.

1) Basic syntax:

gpasswd 选项 组名

2) Description of common options

options Functional description
-a Add user USER to group
-d Add or remove users from group GROUP

Guess you like

Origin blog.csdn.net/qq_44749491/article/details/127759086