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. newgrp
The syntax and practical operation of the command are introduced below .
newgrp Command Syntax
newgrp
The 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 newgrp
the specific steps of using the command to switch user groups.
- Create several user groups:
$ sudo groupadd group1
$ sudo groupadd group2
$ sudo groupadd group3
- Create a user and assign it to multiple groups:
$ sudo useradd -g group1 -G group2,group3 user1
The above command will create a user1
new user with username and add it to group1
the group as the initial group, and add it to the group2
and group3
groups as additional groups.
- 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, touch
three files were created using the command, stored in different user groups.
- 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, file1
the files belong to the initial group group1
, and file2
and belong to the additional groups and file3
respectively .group2
group3
The difference between switching between user groups
Using newgrp
the 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 newgrp
the 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.