用户管理:
useradd
usermod
userdel
id
passwd
组管理:
groupadd ——添加组
groupmod ——修改组
groupdel——删除组
gpasswd——设置密码
添加组
[root@localhost ~]# groupadd grp1
[root@localhost ~]# tail -1 /etc/group
grp1:x:2016:
修改组:
[root@localhost ~]# groupmod -g 2116 grp1
[root@localhost ~]# tail -1 /etc/group
grp1:x:2116:
[root@localhost ~]# groupmod -n group1 grp1
[root@localhost ~]# tail -1 /etc/group
group1:x:2116:
删除组:
[root@localhost ~]# groupdel group1
[root@localhost ~]# tail -1 /etc/group看不到group1
usr6:x:2015:
注意:可以删除用户的附加组,不能删除用户的主组
假设有we1和we2两个用户,分别属于we1组和we2组
[root@localhost ~]# useradd we1
[root@localhost ~]# useradd we2
[root@localhost ~]# tail -2 /etc/passwd
we1:x:2021:2021::/home/we1:/bin/bash
we2:x:2022:2022::/home/we2:/bin/bash
[root@localhost ~]# tail -2 /etc/group
we1:x:2021:
we2:x:2022:
[root@localhost ~]# groupdel we2
groupdel: cannot remove the primary group of user 'we2'
[root@localhost ~]# usermod -g we1 we2
[root@localhost ~]# groupdel we2
[root@localhost ~]# groupadd we2
[root@localhost ~]# usermod -G we2 we2
[root@localhost ~]# id we2
uid=2022(we2) gid=2021(we1) groups=2021(we1),2022(we2)
[root@localhost ~]# groupdel we2
gpasswd 给组设置密码
[root@localhost ~]# gpasswd we1
Changing the password for group we1
New Password:
Re-enter new password:
newgrp 组名 用户输入密码后,可以获得相应组的资源,如:创建的文件属组为
[root@localhost ~]# gpasswd u7
Changing the password for group u7
New Password:
Re-enter new password:
[root@localhost ~]# su - u8
[u8@server254 ~]$ touch u8
[u8@server254 ~]$ ll
total 0
-rw-rw-r--. 1 u8 u8 0 Oct 14 14:10 u8
[u8@server254 ~]$ newgrp u7
Password:
[u8@server254 ~]$ touch u8_7
[u8@server254 ~]$ ll
total 0
-rw-rw-r--. 1 u8 u8 0 Oct 14 14:10 u8
-rw-r--r--. 1 u8 u7 0 Oct 14 14:11 u8_7
[u8@server254 ~]$ id
uid=2024(u8) gid=2024(u8) groups=2024(u8)
[u8@server254 ~]$ newgrp u7
Password:
[u8@server254 ~]$ id
uid=2024(u8) gid=2023(u7) groups=2024(u8),2023(u7) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
练习:
1、添加组g20,指定gid为3000
[root@localhost ~]# groupadd -g 3000 g20
[root@localhost ~]# tail -1 /etc/group
g20:x:3000:
2、修改g20组的gid为3002
[root@localhost ~]# groupmod -g 3002 g20
[root@localhost ~]# tail -1 /etc/group
g20:x:3002:
3、修改g20组的名字为grp20
[root@localhost ~]# groupmod -n grp20 g20
[root@localhost ~]# tail -1 /etc/group
grp20:x:3002:
4、给grp20组设置密码为group20
[root@localhost ~]# gpasswd grp20
Changing the password for group grp20
New Password:
Re-enter new password:
5、创建用户u20,使用id验证用户信息;切换u20的组为grp20,用id验证gid的变化
[root@localhost ~]# useradd u20
[root@localhost ~]# id u20
uid=2025(u20) gid=2025(u20) groups=2025(u20)
[root@localhost ~]# su - u20 #切换用户
[u20@server254 ~]$ whoami
u20
[u20@server254 ~]$ newgrp grp20#切换组
Password:
[u20@server254 ~]$ id
uid=2025(u20) gid=3002(grp20) groups=2025(u20),3002(grp20)