【www.bbyears.com--linux】
在Linux系统把用户添加至组(Group)时,使用gpasswd -a或者usermod -aG命令。
将用户添加至组的几种方法
创建用户时,把用户添加至指定组的方法有以下几种。
代码如下(1) 创建用户时指定组
# useradd -g [组名 or gid] -G [附加组 or gid] [用户名]
(2) 使用usermod命令指定组
# usermod -g [组名 or gid] -G [附加组 or gid] [用户名]
(3) usermod命令使用-aG选项指定添加的组
# usermod -aG [组名] [用户名]
(4) gpasswd命令,把指定用户添加至指定组
# gpasswd -a [用户名] [组名]
使用usermod命令时的需注意
在使用usermod命令时仅使用-G选项指定组时需注意,该用户将从原来的所有组里退出,仅属于使用-G选项指定的组。
代码如下# man usermod
-a, --append
Add the user to the supplementary group(s). Use only with the -G option.
-G, --groups GROUP1[,GROUP2,...[,GROUPN]]]
A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option.
If the user is currently a member of a group which is not listed, the user will be removed from the group. This behaviour can be changed via the -a option, which appends the user to the current supplementary group list.
实际操作
以下是zabbix用户为例,执行gpasswd -a、usermod -G、usermod -aG命令的结果。
代码如下# id zabbix
uid=500(zabbix) gid=500(zabbix) groups=500(zabbix)
# gpasswd -a zabbix wheel
Adding user zabbix to group wheel
# id zabbix
uid=500(zabbix) gid=500(zabbix) groups=500(zabbix),10(wheel)
# usermod -G zabbix zabbix
# id zabbix
uid=500(zabbix) gid=500(zabbix) groups=500(zabbix)
# usermod -aG wheel zabbix
# id zabbix
uid=500(zabbix) gid=500(zabbix) groups=500(zabbix),10(wheel)