在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) |