Linux用户组管理命令

Linux用户组管理的相关文件

/etc/group 文件

这个文件存放着用户组信息:

[root@wqh06 ~]# cat /etc/group | head -3
root:x:0:
bin:x:1:
daemon:x:2:
列数 作用
第一列 组名
第二列 组密码占位符
第三列 组ID(GID)
第四列 附属成员,主组非此组,但附加组为此组的成员

/etc/gshadow 文件

这个文件存放着组密码信息:

[root@wqh06 ~]# cat /etc/gshadow | tail -8
ssh4:!::
ssh5:!::
shanghai03:$6$q2Gc1/YE7pk/EW$xNDx5p3G4tnUZkCbYhDc26zyDuNYYuidcyFP0j7JByOFIq8B4r9CEH6NzHJF2DviVnau55Ltwzl2nPjwUBKg51::user03
列数 作用
第一列 组名
第二列 组密码,若是!或空,表示没有密码
第三列 用户组管理员
第四列 附属成员,主组非此组,但附加组为此组的成员

用户组管理命令

增加用户组

# groupadd

//创建基本组, 不指定gid
[root@wqh ~]# groupadd no_gid
[root@wqh ~]# tail -n1 /etc/group
no_gid:x:1000:

//创建基本组, 指定gid为1500
[root@wqh ~]# groupadd -g 1500 yes_gid
[root@wqh ~]# tail -1 /etc/group
yes_gid:x:1500:

//创建系统组,gid从201-999
[root@wqh ~]# groupadd -r sys_group
[root@wqh ~]# tail -1 /etc/group
sys_group:x:335:

修改用户组

# groupmod
//-g 修改组gid
[root@wqh ~]# groupmod -g 777 no_gid
[root@wqh ~]# tail -1 /etc/group
no_gid:x:777:

//-n 修改组名称
[root@wqh ~]# groupmod -n active_group yes_gid
[root@wqh ~]# tail -1 /etc/group
active_group:x:1500:

删除用户组

# groupdel
#删除组
[root@wqh ~]# groupdel active_group

#删除用户附加组
[root@wqh ~]# id wqh 
uid=1069(wqh) gid=5005(wqh) groups=5005(wqh),5004(devops)
[root@wqh ~]# groupdel devops
[root@wqh ~]# id wqh
uid=1069(wqh) gid=5005(wqh) groups=5005(wqh)

#无法删除用户基本组
[root@wqh ~]# groupdel wqh
groupdel: cannot remove the primary group of user 'wqh'
#只有删除用户或者用户变更基本后,方可删除该组

设置组密码

# gpasswd 交互式设置密码
[root@wqh ~]# groupadd devops
[root@wqh ~]# gpasswd devops
Changing the password for group devops
New Password:
Re-enter new password:

修改基本组

//检查账户信息
[root@wqh06 ~]# useradd test_g
[root@wqh06 ~]# id test_g
uid=5161(test_g) gid=5161(test_g) groups=5161(test_g)

//切换普通用户
[root@wqh06 ~]# su - test_g

//创建新文件,并验证权限
[test_g@wqh06 ~]$ touch test_g.txt
[test_g@wqh06 ~]$ ll test_g.txt 
-rw-rw-r-- 1 test_g test_g 0 Apr  2 00:39 test_g.txt


//切换组信息,这里需要注意,要切换到的目的组一定要设置过组密码,否则无法切换,需要在root下设置
[test_g@wqh06 ~]$ newgrp shanghai03
Password: 

//建立文件,检查权限
[test_g@wqh06 ~]$ touch test_g_newgrp.txt
[test_g@wqh06 ~]$ ll
total 0
-rw-r--r-- 1 test_g shanghai03 0 Apr  2 00:40 test_g_newgrp.txt
-rw-rw-r-- 1 test_g test_g     0 Apr  2 00:39 test_g.txt

猜你喜欢

转载自www.cnblogs.com/zzzwqh/p/12617043.html