![如何使用 usermod 在 suse linux 中向用户添加组而不删除其他组?](https://linux22.com/image/46726/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20usermod%20%E5%9C%A8%20suse%20linux%20%E4%B8%AD%E5%90%91%E7%94%A8%E6%88%B7%E6%B7%BB%E5%8A%A0%E7%BB%84%E8%80%8C%E4%B8%8D%E5%88%A0%E9%99%A4%E5%85%B6%E4%BB%96%E7%BB%84%EF%BC%9F.png)
我有一个用户,它有一个主要组和一个辅助组,例如:
[ testuser Welcome ~ ]$ id
uid=2000(testuser) gid=2000(testuser) groups=2000(testuser),27(sudo),2001(testgroup)
主要组是testuser
,次要组是testgroup
。
现在,我尝试使用以下命令向该用户添加另一个辅助组 testgroup2
usermod -G testgroup2 testuser
但它删除了之前的辅助组 ( testgroup
) 并添加了新的辅助组。我的需要是不应删除以前的辅助组,而应添加新的辅助组。
我在 ubuntu 中看到,有一个选项-a
可以将组附加到用户而不删除其他组。
请告诉我在 suse linux 上是否也可以...?
谢谢!
答案1
在 SLES11 SP3 中,usermod
命令(来自 pwdutils 3.2.15)支持一个-A
可以执行您想要的操作的选项:
-A, --add-to-group group,...
With this option a list of groups can be specified, which the user should
become a member of. Each group is separated from the next one only by a comma,
without whitespace.
在SLES12和OpenSUSE 13.1中,usermod
命令(来自更广泛使用的shadow-utils 4.1.5.1)支持-a
与选项结合使用的选项-G
:
-a, --append
Add the user to the supplemental group(s). Use only with -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.
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.
答案2
如果您usermod
无法追加(甚至无法追加-A
),请尝试添加当前的组集:
usermod -G "$(groups testuser | sed 's/.*: //;s/ /,/g'),testgroup2" testuser
答案3
此命令使您指定所有组的列表,它会替换旧的组。
-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.
而是使用adduser
:
adduser [options] user group
adduser
可以用来添加用户,也可以将用户添加到组中。
或者正如 @markplotnick 指出的那样,使用-a
以下选项usermod