Management of Linux User Groups - Techniques for Switching Between User Groups

Management of Linux User Groups - Techniques for Switching Between User Groups

In the Linux system, user groups are a very important way to manage user rights. Each user can belong to an initial group or to multiple additional groups. When a user needs to access certain resources, it is necessary to check whether the group to which the user belongs has access rights. Therefore, in Linux server management, familiarity with the use and switching methods of user groups is very important for ensuring system security and improving management efficiency.

This article will introduce the method of switching between user groups in the Linux system, including syntax, practical operation, and the differences between switching between various user groups. It is limited to the switching between user groups, and does not explain the addition and deletion of user groups.

Getting to Know Linux User Groups

In a Linux system, each user must belong to one or more user groups, and each user group has a unique numeric group ID (GID) and a group name. Users can switch between their initial group and any number of additional groups. When a user belongs to a group, he can access files and directories owned by that group.

The following commands can be used to manage Linux user groups:

Order describe
groupadd Add a new user group
groupdel Delete an existing user group
usermod -aG Add the user to a specified additional group
groups Display the groups the user belongs to

How to switch user groups

In the Linux system, the commonly used command to switch between user groups is newgrp. This command allows users to choose one of the additional groups as their new initial group. newgrpThe syntax and practical operation of the command are introduced below .

newgrp Command Syntax

newgrpThe basic syntax of the command is as follows:

newgrp [选项] [组名]

Where, 组名is the name of the user group to switch to. If no group name is specified on the command line, the default will be the primary group of the currently logged in user.

Practical method of newgrp command

The following describes newgrpthe specific steps of using the command to switch user groups.

  1. Create several user groups:
$ sudo groupadd group1
$ sudo groupadd group2
$ sudo groupadd group3
  1. Create a user and assign it to multiple groups:
$ sudo useradd -g group1 -G group2,group3 user1

The above command will create a user1new user with username and add it to group1the group as the initial group, and add it to the group2and group3groups as additional groups.

  1. Switch to a new user and create a file:
$ su - user1
$ touch file1
$ newgrp group2
$ touch file2
$ newgrp group3
$ touch file3

After switching to the new user, touchthree files were created using the command, stored in different user groups.

  1. View the user group to which the file belongs:
$ ls -l file*
-rw-r--r-- 1 user1 group1 0 May 22 10:02 file1
-rw-r--r-- 1 user1 group2 0 May 22 10:05 file2
-rw-r--r-- 1 user1 group3 0 May 22 10:08 file3

As you can see, file1the files belong to the initial group group1, and file2and belong to the additional groups and file3respectively .group2group3

The difference between switching between user groups

Using newgrpthe command to switch the user's initial group will not permanently change the user's initial group. When the user exits the current shell, the original initial group will be restored. If you want to permanently modify the user's initial groups, you need to modify the /etc/passwd file.

In addition, when using newgrpthe command, there is another problem that needs attention: every time the initial group is switched, a new subshell process will be started. Therefore, when switching user groups frequently, a large number of subshell processes may be generated, resulting in high system load. Therefore, you should try to avoid using the command too frequently newgrp.

conclusion

Through the introduction of this article, I believe that everyone has a certain understanding of switching between user groups. In actual use, care should be taken to avoid excessive system load caused by too frequent user group switching. At the same time, you need to be cautious when switching user groups to avoid accidentally deleting or modifying some important files.

Guess you like

Origin blog.csdn.net/m0_67268191/article/details/130854252