I have heard that Linux have groups. I wonder that how can use groups in Linux ? Linux user groups are used to groups users so for privilege and permission issues can be solved for multiple users easily. For example if we have a file and want to read access for 47 Linux users we can use groups mechanism to group these users and give access to the newly created group.
我听说Linux有小组。 我不知道如何在Linux中使用组? Linux用户组用于对用户进行分组,因此可以轻松地为多个用户解决特权和权限问题。 例如,如果我们有一个文件并想要读取47个Linux用户的访问权限,则可以使用组机制对这些用户进行分组,并授予对新创建的组的访问权限。
列出当前用户组 (List Current User Groups)
First thing we will do is listing user groups in a Linux system. We can list current user group like below
我们要做的第一件事是列出Linux系统中的用户组。 我们可以像下面列出当前用户组
$ groups
![List Current User Groups](https://i-blog.csdnimg.cn/blog_migrate/1ef498195c9800c117f27dd6c49c4a86.png)
We can see that current user is joined groups like ismail
adm
sudo
etc.
我们可以看到当前用户已加入ismail
adm
sudo
等组。
列出所有组 (List All Groups)
But if we want to list all groups those exists in Linux system we can use following command.
但是,如果要列出Linux系统中存在的所有组,则可以使用以下命令。
$ sudo cut -d: -f1 /etc/group
![List All Groups](https://i-blog.csdnimg.cn/blog_migrate/31c658774370a3f53b6d8f2948a3ded9.png)
创建组(Create Group)
We can create new group to add user. New group creation can be done like below.
我们可以创建新组以添加用户。 可以像下面这样完成新的组创建。
$ sudo groupadd test
We have added group named test
我们添加了名为test的组
移除群组(Remove Group)
Removing an existing group can be done with groupdel method.
可以使用groupdel方法删除现有的组。
$ sudo groupdel test
Remove group named test
删除名为test的组
组文件(Group File)
Group file is the database used by groups. Actually it is a text file like below.
组文件是组使用的数据库。 实际上,它是一个文本文件,如下所示。
$ cat /etc/group
![Group File](https://i-blog.csdnimg.cn/blog_migrate/971ada6e9d5a2436de4a6f12ffdb979c.png)
And it is generally resides in /etc/group
它通常位于/ etc / group中
For the first like root is group name
第一个like root是组名
x is the password where it is stored in /etc/gshadow
x是密码存储在/ etc / gshadow中的密码
0 is the group id
组ID为0
将用户添加到组 (Add User To the Group)
We can add existing user to the existing group by using usermod command
我们可以使用usermod命令将现有用户添加到现有组中
$ sudo usermod -a -G root ismail
usermod -a is command to add user to group
usermod -a是用于将用户添加到组的命令
-G root is used to specify group
-G root用于指定组
ismail is the user name
ismail是用户名
从组中删除用户(Remove User From Group)
We can remove existing user to the existing group by using usermod command
我们可以使用usermod命令将现有用户删除到现有组中
$ sudo usermod -G root ismail
usermod is command to remove user from group
usermod是从组中删除用户的命令
-G root is used to specify group
-G root用于指定组
ismail is the user name
ismail是用户名