Linux下使用者添加至組的方法介紹

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.