用户和组
**小述:在进行用户和组的管理之前,我们必须要知道何为用户,何为组,它们究竟有什么作用呢,又有何种关联。
-
关于用户那些事
系统上的每个进程(运行的程序)都是作为特定用户运行的
每个文件是由一个特定的用户拥有
访问文件和目录受到用户的限制 -
关于特权用户
UID 是 0
所有权力
大多数设备只能由 root 控制 -
组
用户管理
RHEL7系统设置:
- 特权用户 UID:0
- 系统用户 UID : 1~999
- 普通用户 UID :1000+
新建用户
- 格式:useradd [选项] 用户名
- 参数:-d 指定用户的家目录
-u 指定该用户的默认UID
-G 指定一个或多个扩展用户组
-s 指定该用户的默认Shell解释器 - 默认解释器:/bin/bash
- 默认存放目录: /home
示例
[root@tong ~]# useradd u01
[root@tong ~]# grep 'u01' /etc/passwd /etc/shadow /etc/group
/etc/passwd:u01:x:1001:1001::/home/u01:/bin/bash
/etc/shadow:u01:!!:17822:0:99999:7:::
/etc/group:u01:x:1001:
[root@tong ~]# id u01
uid=1001(u01) gid=1001(u01) groups=1001(u01)
[root@tong ~]# useradd u02 -u 666 //指定uid
[root@tong ~]# id u02
uid=666(u02) gid=2001(u02) groups=2001(u02)
[root@tong ~]# useradd user03 -d /abc //指定家目录
[root@tong ~]# useradd user04 -s /sbin/nologin //指定shell
[root@tong ~]# useradd user06 -G family //指定附加组
删除用户
- 格式:userdel [选项] 用户名
[root@tong ~]# userdel -r user05 //删除用户user05和所属的家目录
[root@tong ~]# ll -d /home/user05/
ls: cannot access /home/user05/: No such file or directory
[root@tong ~]# userdel user07 //删除用户user07但不删除其家目录
[root@tong ~]# ll -d /home/user07/
drwx------. 2 1004 1004 62 Oct 18 23:42 /home/user07/
组成员管理
- 格式:usermod [选项] 用户名
- 作用:修改用户的属性
- 参数:-g 变更所属用户组
- -G 变更扩展用户组
示例:
[root@tong ~]# id tong
uid=1008(tong) gid=1008(tong) groups=1008(tong)
[root@tong ~]# usermod -g student1 tong //指定所属用户组
[root@tong ~]# id tong
uid=1008(tong) gid=2003(student1) groups=2003(student1)
[root@tong ~]# groupadd girl
[root@tong ~]# usermod -G girl tong //变更附加组
[root@tong ~]# id tong
uid=1008(tong) gid=2003(student1) groups=2003(student1),2004(girl)
用户密码
[root@tong ~]# useradd tom
[root@tong ~]# passwd tom //给其他用户设置密码,root可以给所有用户设置密码,普通用户只给自己设置密码
Changing password for user tom.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@tong ~]# su - tom
[tom@tong ~]$
组管理
(我们在工作中会把一些用户放在一个组中,组方便我们后续对批量用户安排相关的权限)
创建组
- 格式:groupadd [选项] 群组名
- 参数:-g 指定一个初始的用户基本组(必须存在)
- 示例:
[root@tong ~]# groupadd student1 //新建组
[root@tong ~]# grep 'student1' /etc/group
student1:x:1001:
[root@tong ~]# groupadd student2 -g 1350 //指定gid
[root@tong ~]# grep 'student2' /etc/group //查看组信息
student2:x:1350:
删除组
- 格式:groupdel [选项] 群组名
- 示例:
[root@tong ~]# groupdel student1
[root@tong ~]# grep 'student1' /etc/group
noshell
用户解释器设为/sbin/nologin,用户不能登陆到该系统