如何在Linux上将用户添加到组
这是有关如何在Linux中将用户添加到组的文章。 我们将描述在创建用户以及现有用户时将用户添加到组中的快速方法。
Linux组类型:
- 主要组: 这是用户登录系统时的默认组。 在大多数情况下,它与用户名相同。 用户始终只是一个主要组的一部分。
- 次要小组(附加小组): 这是一个与主要用户不同的用户所属的组。 一个用户最多可以属于32个辅助组。
有关管理命令用户的更多信息,请单击 这里:
在以下位置将用户添加到群组的示例 Linux操作系统:
1.小学组
- 默认情况下,主要组为:
默认情况下,创建用户时,它属于同一组。
[[email protected] ~]# useradd andreyex_user
确认
[[email protected] ~]# id andreyex_user uid=508(andreyex_user) gid=508(andreyex_user) groups=508(andreyex_user) [[email protected] ~]#
在上面的输出中,默认情况下,用户“ grayex_user”属于具有相同名称的主组。
- Linux OS在创建用户时将用户添加到组中。
假设您要在创建用户时指定非默认的主组。 例如:您要创建一个用户主密钥,并且希望作为主组属于销售组。
[[email protected] ~]# useradd masterkey -g sales
再检查一遍
[[email protected] ~]# id masterkey uid=510(masterkey) gid=509(sales) groups=509(sales) [[email protected] ~]#
确保指定的组必须存在于系统中,否则您将看到以下来自系统的消息。
useradd: group 'sales' does not exist
如果出现上述错误,请先在useradd命令之前使用groupadd命令创建组。
[[email protected] ~]# groupadd sales
- Linux OS将用户添加到现有用户的组中。
如果您已经创建了一个用户,并且将来想更改该用户的主要组。 然后使用usermod命令。 例如,假设您要将主要组销售更改为用户主键的帐户。 为此使用以下命令。
[[email protected] ~]# usermod -g accounts masterkey
再检查一遍:
[[email protected] ~]# id masterkey uid=510(masterkey) gid=510(accounts) groups=510(accounts) [[email protected] ~]#
2.中学组(附加组)
- 在Linux OS中创建用户时,将用户添加到组中。
当直接使用-g选项创建用户时,可以指定辅助组。
[[email protected] ~]# useradd -G sales delphi
使用以下命令仔细检查:
[[email protected] ~]# id delphi uid=511(delphi) gid=511(delphi) groups=511(delphi),509(sales) [[email protected] ~]#
上面的输出确认销售是次要组,而主要组是德尔福。
- 当创建具有多个辅助组的用户时,Linux将用户添加到组中。
在useradd命令中创建用户本身时,可以使用逗号指定多个辅助组。
[[email protected] ~]# useradd -G sales,accounts,marketing bar
使用以下命令进行确认:
[[email protected] ~]# id bar uid=512(bar) gid=513(bar) groups=513(bar),509(sales),510(accounts),512(marketing) [[email protected] ~]#
此外,还有另一种方法可以确认:
[[email protected] ~]# cat /etc/group|grep bar sales:x:509:delphi,bar accounts:x:510:bar marketing:x:512:bar bar:x:513: [[email protected] ~]#
- 将用户添加到Linux上现有用户的组中。
您还可以使用usermod命令的-g选项为现有用户更改辅助组。 假设您要将delphi用户的次要组更改为sales。
[[email protected] ~]# usermod -G accounts delphi [[email protected] ~]# id delphi uid=511(delphi) gid=511(delphi) groups=511(delphi),510(accounts) [[email protected] ~]#
如果您要添加另一个辅助组而不是如上所述进行更改,则必须使用“ -a”(添加)选项。
[[email protected] ~]# usermod -a -G marketing delphi [[email protected] ~]# id delphi uid=511(delphi) gid=511(delphi) groups=511(delphi),510(accounts),512(marketing) [[email protected] ~]#
- 在Linux OS中,为具有多个辅助组的现有用户将用户添加到组中。
可以使用逗号为现有用户使用usermod命令指定多个辅助组。
[[email protected] ~]# usermod -a -G sales,hr delphi [[email protected] ~]# id delphi uid=511(delphi) gid=511(delphi) groups=511(delphi),509(sales),510(accounts),512(marketing),514(hr) [[email protected] ~]#
如果未指定-a选项,则将删除当前的辅助组,并用指定的组名替换。
[[email protected] ~]# id delphi uid=511(delphi) gid=511(delphi) groups=511(delphi),509(sales),510(accounts),512(marketing),514(hr) [[email protected] ~]# usermod -G sales,hr delphi [[email protected] ~]# id delphi uid=511(delphi) gid=511(delphi) groups=511(delphi),509(sales),514(hr) [[email protected] ~]#
注意: 因此,如果要添加到现有组而不是修改现有辅助组,请确保使用-a选项。