欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

如何在Linux中将用户添加到组

程序员文章站 2022-06-26 16:14:12
...

Linux uses groups in order to share resources in a easy way. In general a resource will have a user, group and other access rights. In this tutorial we will learn how to add user to a Linux group and its variations like creating group, deleting group, listing group members etc.

Linux使用组以便以简单的方式共享资源。 通常,资源将具有用户,组和其他访问权限。 在本教程中,我们将学习如何将用户添加到Linux组及其变体,例如创建组,删除组,列出组成员等。

Linux组 (Linux Groups)

Linux groups are organizational units used to organize and administrator user account and rights. Especially for privilege and access management like reading, writing, executing permissions for a given resource or login into a system can be managed with groups by allowing some group and denying some group.

Linux组是用于组织和管理用户帐户和权限的组织单位。 尤其是对于特权和访问权限管理(例如,读取,写入,执行给定资源的执行权限或登录系统),可以通过允许某些组和拒绝某个组来按组进行管理。

There are two type of groups in Linux operating system like below.

如下所示,Linux操作系统中有两种类型的组。

初级组 (Primary Group)

Primary Group is the user first chose group which means if a user creates a file the file group will be assigned to the user primary group. If a user try to use some resources first the primary group will be checked. Primary group information can be viewed in the /etc/passwd file like below.

Primary Group是用户首先选择的组,这意味着如果用户创建文件,则文件组将分配给用户主组。 如果用户尝试使用某些资源,则将首先检查主要组。 可以在/etc/passwd文件中查看主要组信息,如下所示。

$ cat /etc/passwd
如何在Linux中将用户添加到组
Primary Group
初级组

The 5th column provides the primary group information. As we can see some users do not have primary group because they are nor operational and used by operating system so they do not needs primary group.

第5列提供主要组信息。 如我们所见,某些用户没有主要组,因为它们既不可操作也不被操作系统使用,因此他们不需要主要组。

次要或补充组 (Secondary or Supplementary Group)

Secondary group is used to provide access rights for other resources which is not the user primary group. For example a file created by other user with the different primary group can be shared by using secondary group. A user may have multiple secondary groups.

次要组用于为不是用户主要组的其他资源提供访问权限。 例如,可以使用辅助组来共享由具有不同主要组的其他用户创建的文件。 一个用户可能有多个辅助组。

更改现有用户或将其添加到主要组 (Change or Add Existing User To A Primary Group)

We will start by adding an existing user to ta primary group. Caution because this will change the primary group of the user which can create problems because of the file and folder ownership and access restrictions. We will use usermod command with -g option with a primary group name and -a to append .

我们将从将现有用户添加到主要组开始。 小心,因为这将更改用户的主要组,这可能会由于文件和文件夹的所有权以及访问限制而造成问题。 我们将使用带-g选项的usermod命令(带有主组名)和-a来附加。

了解更多什么是计算,Windows,Linux,Google中的用户?

In this example we will add user ismail to the group named ali as primary group.

在此示例中,我们将用户ismail添加到名为ali的组作为主要组。

$ sudo usermod -a -g ali ismail

将现有用户添加到辅助组 (Add Existing User To A Secondary Group)

We will start by adding existing user to a existing group. We will use usermod command. usermod is a command used to manage users and groups. We will also provide -a which means append and -G where we will specify the group we want to add the user.

我们将从将现有用户添加到现有组开始。 我们将使用usermod命令。 usermod是用于管理用户和组的命令。 我们还将提供-a表示添加和-G ,其中我们将指定要添加用户的组。

In this exmaple we will add user ismail to the group syslog as secondary group.

在此示例中,我们将用户ismail添加为组syslog作为辅助组。

$ sudo usermod -G syslog ismail

将现有用户添加到多个组 (Add Existing User To Multiple Groups)

In some cases we may need to add a user into multiple groups with a single command. Actually we will use the same command and parameters used for single group. We will delimit the group names with , .

在某些情况下,我们可能需要使用一个命令将用户添加到多个组中。 实际上,我们将使用与单个组相同的命令和参数。 我们将划定与组名,

In this example we will add user ismail into multiple secondary groups root, sudo , syslog

在此示例中,我们将用户ismail添加到多个辅助组rootsudosyslog

$ sudo usermod -a -G root,sudo,syslog ismail

从组中删除用户 (Remove User From Group)

We can remote a user from a group. This group can be primary or secondary group. We will use gpasswd with the -d option. In this example we will remote user ismail from group syslog.

我们可以从组中远程用户。 该组可以是主要或次要组。 我们将使用带有-d选项的gpasswd 。 在此示例中,我们将从组syslog远程用户ismail

$ sudo gpasswd -d ismail syslog

列出现有组 (List Existing Groups)

Linux group information is stored in the file /etc/group . We can list this file like below which will provide the groups.

Linux组信息存储在文件/etc/group 。 我们可以像下面这样列出该文件,它将提供组。

$ cat /etc/group
如何在Linux中将用户添加到组
List Existing Groups
列出现有组

使用getent命令列出现有组(List Existing Groups with getent Command)

Another good command to list existing groups is getent command by providing the group option.

列出现有组的另一个好命令是通过提供group选项的getent命令。

$ getent group
如何在Linux中将用户添加到组
List Existing Groups with getent Command
使用getent命令列出现有组

创建组(Create Group)

If we need to add a new group before adding a user to this group we need to create it. We will create new group with the groupadd . In this example we will create a group named test.

如果需要在将用户添加到该组之前添加新组,则需要创建它。 我们将使用groupadd创建新组。 在此示例中,我们将创建一个名为test的组。

$ sudo groupadd test

删除群组 (Delete Group)

We can delete existing user group with the groupdel command. In this example we will delete the group named test.

我们可以使用groupdel命令删除现有的用户组。 在此示例中,我们将删除名为test的组。

$ sudo groupdel test

使用单个命令创建新用户并分配组 (Create New User and Assign A Group with Single Command)

There may be some scenarios where we need to create a user and assign groups to this user multiple groups. We will use useradd we will specify the primary user group with -g parameter and secondary groups with -G parameter.

在某些情况下,我们需要创建一个用户并将该组分配给该用户多个组。 我们将使用useradd使用-g参数指定主要用户组,并使用-G参数指定辅助用户组。

$ sudo useradd -g ismail -G wheel,developers ismail

使用id命令显示用户组 (Display User Group with id Command)

We can display existing user groups with different commands. id command is a basic command which will list user name, user primary group, user secondary group and related id.

我们可以使用不同的命令显示现有的用户组。 id命令是一个基本命令,它将列出用户名,用户主要组,用户辅助组以及相关的ID。

$ id
如何在Linux中将用户添加到组
Display User Group with id Command
使用id命令显示用户组

使用组显示用户组命令(Display User Group with groups Command)

Another command which can list current user groups is groups command.

可以列出当前用户组的另一个命令是groups命令。

$ group
如何在Linux中将用户添加到组
Display User Group with groups Command
使用组显示用户组命令

翻译自: https://www.poftut.com/how-to-add-user-to-a-group-in-linux/