侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 747 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

Linux基础(18)之组密码设置及组用户管理

zze
zze
2019-10-22 / 0 评论 / 0 点赞 / 322 阅读 / 2824 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

组密码添加

组密码添加是通过 gpasswd 命令,使用格式如下:

gpasswd [OPTION...] <GROUP>

例:添加一个名为 myGroup 的组,修改其密码为 www.zze.xyz

[root@localhost ~]# groupadd myGroup
[root@localhost ~]# gpasswd myGroup
正在修改 myGroup 组的密码
新密码:
请重新输入新密码:

添加组用户

gpasswd 命令的 -a 选项可以向目标组添加指定用户。
例:将 user1 用户添加到 myGroup 组。

[root@localhost ~]# gpasswd myGroup -a user1
正在将用户“user1”加入到“myGroup”组中
[root@localhost ~]# id user1
uid=1000(user1) gid=1001(user1) 组=1001(user1),1000(myGroup)

从组中删除用户

从组中删除用户可使用 gpasswd 命令的 -d 选项。
例:从 myGroup 组中移除 user1 用户。

[root@localhost ~]# gpasswd myGroup -d user1
正在将用户“user1”从“myGroup”组中删除
[root@localhost ~]# id user1
uid=1000(user1) gid=1001(user1) 组=1001(user1)

设置组的管理用户

可通过 gpasswd 命令的 -A 选项设置对目标组有管理权限的用户列表。
例:让用户 user2user3 拥有组 myGroup 的管理权限。

[root@localhost ~]# gpasswd myGroup -A user2,user3
[root@localhost ~]# tail -1 /etc/gshadow
myGroup:$6$rS4sMN21eF/Z1$5E6auJmIlrVEGIj1VGcrCY8gvz0IfukdF68yphw7VwK2d8frRjJeKuNhP0.yv.8xtPvj9MRcG/Yz9nOneQ.7M0:user2,user3:

临时切换基本组

看如下示例:
新建一个用户 tom,登录切换到 tom,创建 test.tom 文件并查看:

[root@localhost ~]# useradd tom
[root@localhost ~]# su - tom
[tom@localhost ~]$ touch test.tom
[tom@localhost ~]$ ll
总用量 0
-rw-rw-r--. 1 tom tom 0 9月  10 16:58 test.tom

可以看到,test.tom 文件的属主是用户 tom,属组是用户 tom 的同名组即基本组 tom 组。
使用 newgrp 命令切换当前组为 myGroup

[tom@localhost ~]$ newgrp myGroup
密码:
[tom@localhost ~]$ id
uid=1000(tom) gid=1000(myGroup) 组=1000(myGroup),1001(tom) 

可以看到此时用户 tom 的基本组为 myGroup,并且此时临时属于 GID 为 1000 和 1001 的 2 个组。
再创建一个 test1.tom 文件:

[tom@localhost ~]$ touch test1.tom
[tom@localhost ~]$ ll
总用量 0
-rw-r--r--. 1 tom myGroup 0 9月  10 17:13 test1.tom
-rw-rw-r--. 1 tom tom     0 9月  10 16:58 test.tom

可以看到,此时 test1.tom 文件的属主依旧是用户 tom,而它的属组则是新的基本组 myGroup
输入 exit 退出,再次查看 tom 用户的信息:

[tom@localhost ~]$ exit
exit
[tom@localhost ~]$ id 
uid=1000(tom) gid=1001(tom) 组=1001(tom)

可以看到,tom 用户的基本组又恢复了就是原来的同名私有组。

newgrp 命令能够让用户临时切换基本组去执行命令,如果用户的附加组中本就包含要切换的目标基本组,那么就不需要输入组密码。

0

评论区