usermod有关-g和-G的部分区别

来源:3-11 群组的管理

慕的地8023689

2022-01-10

usermod修改用户组的权限,-g会把用户从原来的组里剔除,然后加入新的群组。而-G只会是修改附加组,通过-g添加的群组是不受-G影响的,对吗?

写回答

1回答

Oscar

2022-01-11

usermod 的命令手册见 https://linux.die.net/man/8/usermod

或者你用

man usermod

来查看命令手册,可以看到 

-g, --gid GROUP
           The group name or number of the user's new initial login group. The group must exist.

           Any file from the user's home directory owned by the previous primary group of the user will be owned by this new group.

           The group ownership of files outside of the user's home directory must be fixed manually.

-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.

说明得比较清楚了。

-g 参数是用于设定用户的 initial login group(初始登录群组),并不会受之后用 -G 参数添加或修改的附加群组的影响。

-G 参数是用于设定用户除了 initial login group(初始登录群组)之外的其他附加群组(A list of supplementary groups which the user is also a member of. 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.)。

-aG 用于添加新的附加群组,不会删掉目前的附加群组。a 是 append(附加)的缩写。


我实际测试了。比如我这里的 thomas 用户:

//img.mukewang.com/szimg/5dd747f3099c956511980254.jpg

//img1.sycdn.imooc.com/szimg/5dd7480009235aea21560798.jpg

跟你的情况类似,thomas 的群组里面还是有 friends 这个群组。因为这个 friends 群组是最初通过 -g 参数来设定的,是 thomas 这个用户的 initial login group(初始登录群组),并不会受之后用 -G 参数添加或修改的附加群组的影响。

我查看了 /etc/group 这个包含群组的信息的文件,发现:

//img1.sycdn.imooc.com/szimg/5dd74841094fbe2506620464.jpg

我又查看了 /etc/passwd 文件,里面有用户及用户ID,组ID的信息,等等:

//img.mukewang.com/szimg/5dd7489009dc6f8513760076.jpg

可以看到用户 thomas 的用户 ID 是 1001,而他的 组 ID 是 1002(1002 就是 friends 这个群组的 组 ID)。 thomas 用户的组 ID 是 1002,所以他目前一直属于 friends 这个初始登录群组。


参考文章:

linux下查看所有用户及所有用户组

Linux /etc/passwd内容解释(超详细)

0
0

Linux核心技能与应用

一网打尽Linux必备核心技能,面试、升职必备的“敲门砖”。

2089 学习 · 692 问题

查看课程