概述
- 本节课需要学习Linux的用户与用户安全管理
用户、用户组增删改查的基本命令
https://blog.youkuaiyun.com/lydms/article/details/101368670?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164212222416780271945494%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=164212222416780271945494&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_ulrmf~default-1-101368670.pc_search_insert_ulrmf&utm_term=linux%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4&spm=1018.2226.3001.4187
参考此文章,非常详细,希望大家自己下去一个命令一个命令的都去敲一下。
补充内容-文件权限
- r(read)读操作,阅读功能,也就是文档的查看对应的标志位为100即4
- w(write)写功能,编辑功能。对应的标志位为010即2
- x(execute)执行功能。类似与windows的.bat文件,linux里面的.sh文件。同样还适用于文件夹的打开功能。对应的标志位为001即1
我们来看一个例子
[zhuofai@localhost home]$ ll
total 4
drwx------. 3 newstudent newstudent 99 Jan 17 19:10 newstudent
drwxrwx---+ 5 sharedoc sharedoc 129 Jan 17 19:10 sharedoc
drwx------. 3 student1 sharedoc 78 Jan 17 18:23 student1
drwx------. 3 student2 sharedoc 78 Jan 17 18:23 student2
drwx------. 3 student3 sharedoc 78 Jan 17 18:23 student3
drwx------. 3 student4 sharedoc 78 Jan 17 18:27 student4
drwx------. 15 zhuofai zhuofai 4096 Jan 17 20:56 zhuofai
[zhuofai@localhost home]$ chmod 755 zhuofai#我们修改了其他用户进入该文件目录的权限我们现在来看下是否能进去
drwxr-xr-x. 15 zhuofai zhuofai 4096 Jan 17 20:56 zhuofai#可以看到有三个字段,第一个drwx,d代表的就是该文件是文件夹,当前用户具有该文件夹的读写执行权限。
#第二个为r-x即与该用户处于用户组的成员对此文件具有读和执行功能(即打开该文件夹功能)
#第三个为其他用户r-x
接下来我们尝试着用其他用户来进入此文件夹试试
su sharedoc
Password:
[sharedoc@localhost /]$
[sharedoc@localhost /]$ cd home
[sharedoc@localhost home]$
[sharedoc@localhost home]$ dir
newstudent sharedoc student1 student2 student3 student4 zhuofai
[sharedoc@localhost home]$
[sharedoc@localhost home]$ cd zhuofai
这时候我们就能够进去zhuofai的文件夹里面了,但是一般我们不这样操作,风险太大。
补充内容-ACL控制
在CentOS7往后的版本ACL是自动开启的,无需手动开始,所以我们直接开始用,书本上关于ACL控制的内容很少不够全面也不能够完全体现出ACL强大的功能。
所以老师自己在网上找了一个例子来说明ACL强大的控制作用
拿我们学校来讲,在linux多用户操作系统下超级用户是谁,肯定是校长对不对,那么假设有这样一种情况,针对于学校里面的我们班,有一个共享文件夹,文件夹有老师的课件,学生的作业。我们先来实现以下这个情况。这时候新入学了一个学生。OK我们来分析一下这个情况
首先我们要设立一个老师用户,同时创建主目录sharedoc,同时将权限改为770
第二我们要创建所有学生用户,同时将学生用户加入到老师用户组里面获得进入sharedoc目录的权限。
第三在sharedoc创建两个文件夹,第一个是老师自己使用的teacher文件夹,第二个是学生使用的student文件夹。老师对自己的文件夹具有rwx权限,但是不允许学生进入即700权限,学生文件夹的权限对老师来说也是rwx权限。
那么当新来了一个学生,他还不会使用我们的系统,这时候我们给他的权限是只能进入到student文件夹具有rx权限,而不具有w权限。这时候加入用户组也不合适,让他作为其他用户给权限也不合情理,因为这样所有的其他用户就都可以进入该文件里面查看内容了。所以要该用户具有不加入用户组也不是其他用户的时候想拥有该文件夹的权限这就要用到我们的ACL控制了。
```bash
useradd -m student1
useradd -m student2
useradd -m student3#创建三个学生用户
useradd -m newstudent#创建一个信赖的学生用户
usermod -g sharedoc student1
usermod -g sharedoc student2
usermod -g sharedoc student3#将三个学生用户添加到我们老师的用户组里面
[root@localhost home]# useradd -m sharedoc#建立老师的共享目录
[root@localhost home]# useradd -m student1
[root@localhost home]# useradd -m student2
[root@localhost home]# useradd -m student3#创建了三个学生用户
[root@localhost home]# useradd -m newstudent#创建新来学生的用户
[root@localhost home]# usermod -g sharedoc student1
[root@localhost home]#
[root@localhost home]# usermod -g sharedoc student2
[root@localhost home]#
[root@localhost home]# usermod -g sharedoc student3#将三个学生用户添加到老师的用户组里面
[root@localhost home]#
[root@localhost home]# ll
total 4
drwx------. 3 newstudent newstudent 78 Jan 17 18:23 newstudent
drwx------. 3 sharedoc sharedoc 78 Jan 17 18:19 sharedoc
drwx------. 3 student1 sharedoc 78 Jan 17 18:23 student1
drwx------. 3 student2 sharedoc 78 Jan 17 18:23 student2
drwx------. 3 student3 sharedoc 78 Jan 17 18:23 student3
drwx------. 3 student4 sharedoc 78 Jan 17 18:27 student4
drwx------. 15 zhuofai zhuofai 4096 Jan 17 04:43 zhuofai
[root@localhost home]#
[root@localhost home]# su sharedoc
[sharedoc@localhost home]$
[sharedoc@localhost home]$ dir
newstudent sharedoc student1 student2 student3 student4 zhuofai
[sharedoc@localhost home]$
[sharedoc@localhost home]$ ll
total 4
drwx------. 3 newstudent newstudent 78 Jan 17 18:23 newstudent
drwx------. 3 sharedoc sharedoc 78 Jan 17 18:19 sharedoc
drwx------. 3 student1 sharedoc 78 Jan 17 18:23 student1
drwx------. 3 student2 sharedoc 78 Jan 17 18:23 student2
drwx------. 3 student3 sharedoc 78 Jan 17 18:23 student3
drwx------. 3 student4 sharedoc 78 Jan 17 18:27 student4
drwx------. 15 zhuofai zhuofai 4096 Jan 17 04:43 zhuofai
[sharedoc@localhost home]$
[sharedoc@localhost home]$ chmod 770 sharedoc#不允许其他用户进去这个文件夹里面
[sharedoc@localhost home]$
[sharedoc@localhost home]$ ll
total 4
drwx------. 3 newstudent newstudent 78 Jan 17 18:23 newstudent
drwxrwx---. 3 sharedoc sharedoc 78 Jan 17 18:19 sharedoc
drwx------. 3 student1 sharedoc 78 Jan 17 18:23 student1
drwx------. 3 student2 sharedoc 78 Jan 17 18:23 student2
drwx------. 3 student3 sharedoc 78 Jan 17 18:23 student3
drwx------. 3 student4 sharedoc 78 Jan 17 18:27 student4
drwx------. 15 zhuofai zhuofai 4096 Jan 17 04:43 zhuofai
[sharedoc@localhost home]$
[sharedoc@localhost home]$ cd sharedoc
[sharedoc@localhost ~]$
[sharedoc@localhost ~]$ mkdir student#学生作业专门的文件夹
[sharedoc@localhost ~]$
[sharedoc@localhost ~]$ ll
total 0
drwxrwxr-x. 2 sharedoc sharedoc 6 Jan 17 18:34 student
[sharedoc@localhost ~]$
[sharedoc@localhost ~]$ mkdir teacher #老师专门的文件夹
[sharedoc@localhost ~]$
[sharedoc@localhost ~]$ ll
total 0
drwxrwxr-x. 2 sharedoc sharedoc 6 Jan 17 18:34 student
drwxrwxr-x. 2 sharedoc sharedoc 6 Jan 17 18:35 teacher
[sharedoc@localhost ~]$
[sharedoc@localhost ~]$ chmod 700 teacher#将老师专门的文件夹设置为仅自己可用
[sharedoc@localhost ~]$
[sharedoc@localhost ~]$ ll
total 0
drwxrwxr-x. 2 sharedoc sharedoc 6 Jan 17 18:34 student
drwx------. 2 sharedoc sharedoc 6 Jan 17 18:35 teacher
[sharedoc@localhost ~]$
[sharedoc@localhost ~]$ setfacl -m u: newstudent: rw ./student#多了空格所以命令执行不了
setfacl: Option -m incomplete
[sharedoc@localhost ~]$
[sharedoc@localhost ~]$ ll
total 0
drwxrwxr-x. 2 sharedoc sharedoc 6 Jan 17 18:34 student
drwx------. 2 sharedoc sharedoc 6 Jan 17 18:35 teacher
[sharedoc@localhost ~]$
[sharedoc@localhost ~]$ su
Password:
[root@localhost sharedoc]# setfacl -m u:newstudent:r-- /student#找不到在根目录下面的student文件
setfacl: /student: No such file or directory
[root@localhost sharedoc]#
[root@localhost sharedoc]# setfacl -m u:newstudent:r-- ./student
[root@localhost sharedoc]#
[root@localhost sharedoc]# ll
total 0
drwxrwxr-x+ 2 sharedoc sharedoc 6 Jan 17 18:34 student
drwx------. 2 sharedoc sharedoc 6 Jan 17 18:35 teacher
[root@localhost sharedoc]#
[root@localhost sharedoc]# su sharedoc
[sharedoc@localhost ~]$
[sharedoc@localhost ~]$ ll
total 0
drwxrwxr-x+ 2 sharedoc sharedoc 6 Jan 17 18:34 student
drwx------. 2 sharedoc sharedoc 6 Jan 17 18:35 teacher
[sharedoc@localhost ~]$
[sharedoc@localhost ~]$ chmod 770 student/#修改学生目录的权限,不允许其他人进入
[sharedoc@localhost ~]$
[sharedoc@localhost ~]$ ll
total 0
drwxrwx---+ 2 sharedoc sharedoc 6 Jan 17 18:34 student
drwx------. 2 sharedoc sharedoc 6 Jan 17 18:35 teacher
[sharedoc@localhost ~]$ su
Password:
[root@localhost sharedoc]#
[root@localhost sharedoc]# passwd newstudent
Changing password for user newstudent.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost sharedoc]#
[root@localhost sharedoc]# su newstudent
[newstudent@localhost sharedoc]$
[newstudent@localhost sharedoc]$ ll#还是进不去因为缺少了文件x执行权限
ls: cannot open directory '.': Permission denied
[newstudent@localhost home]$ su
Password:
[root@localhost home]#
[root@localhost home]# setfacl -m u:newstudent:rwx /home/sharedoc
[root@localhost home]#
[root@localhost home]# ll
total 4
drwx------. 3 newstudent newstudent 78 Jan 17 18:23 newstudent
drwxrwx---+ 5 sharedoc sharedoc 108 Jan 17 18:35 sharedoc
drwx------. 3 student1 sharedoc 78 Jan 17 18:23 student1
drwx------. 3 student2 sharedoc 78 Jan 17 18:23 student2
drwx------. 3 student3 sharedoc 78 Jan 17 18:23 student3
drwx------. 3 student4 sharedoc 78 Jan 17 18:27 student4
drwx------. 15 zhuofai zhuofai 4096 Jan 17 04:43 zhuofai
[root@localhost home]#
[root@localhost home]# su newstudent
[newstudent@localhost home]$
[newstudent@localhost home]$ dir
newstudent sharedoc student1 student2 student3 student4 zhuofai
[newstudent@localhost home]$
[newstudent@localhost home]$ cd sharedoc
[newstudent@localhost sharedoc]$
[newstudent@localhost sharedoc]$ ll
total 0
drwxrwx---+ 2 sharedoc sharedoc 6 Jan 17 18:34 student
drwx------. 2 sharedoc sharedoc 6 Jan 17 18:35 teacher
[newstudent@localhost sharedoc]$
[newstudent@localhost sharedoc]$ cd student
bash: cd: student: Permission denied
[newstudent@localhost sharedoc]$
[newstudent@localhost sharedoc]$ su
Password:
^X^H^H^Hsu: Authentication failure
[newstudent@localhost sharedoc]$
[newstudent@localhost sharedoc]$ su
Password:
[root@localhost sharedoc]#
[root@localhost sharedoc]# setfacl -m u:newstudent:r-x /home/sharedoc/student
[root@localhost sharedoc]#
[root@localhost sharedoc]# su newstudent
[newstudent@localhost sharedoc]$
[newstudent@localhost sharedoc]$ cd student
[newstudent@localhost student]$
[newstudent@localhost student]$ ll
total 0