文章目录
前言
默认情况下,由于一个普通用户(tom)无法修改自己的文件的属主和属组,所以当tom想为好基友(jerry)分享自己的文件时,只能通过修改文件的o(ther)权限使jerry拥有访问文件的权限。但是,这样会使所有的other用户也同时拥有了访问这个文件的权限,这样一来,数据的安全性大大降低。
facl的出现,很好的解决了这个问题。
facl(file access control list)的作用
facl(文件访问控制列表)是文件的一种额外的赋权机制,它在文件的基本权限u,g,o外,添加了另一层让普通用户能够将文件的控制权限赋予另外的一些用户或者组的机制。
facl的相关命令
getfacl
- 常见用法:getfacl FILENAME
[jerry@centos7u6 tmp]$ getfacl tomfile
# file: tomfile
# owner: tom
# group: tom
user::rw-
group::rw-
other::---
- 默认情况下,一个文件的facl属性包括以下字段;
- #file: tomfile 文件名
- #owner:tom 文件属主
- #group:tom 文件属组
- user::rw- 用户的访问权限(中间字段为用户名,当中间字段为空时,则代表属主)
- group::rw- 组的访问权限(中间字段为组名,当中间字段为空时,则代表属组)
- other::— 其他的访问权限
setfacl
setfacl命令有很多选项,但是最常用的是-m和-x
- 给用户赋权:setfacl -m u:USER:MODE FILE
撤销赋权:setfacl -x g:GROUP:MODE FILE - 给组赋权:setfacl -m g:GROUP:MODE FILE
撤销赋权:setfacl -m g:GROUP FILE
实例讲解
对用户(user)
默认情况(不使用facl)
- 假设tom有一个人文件,文件权限如下:
[tom@centos7u6 tmp]$ ll tomfile
-rw-rw---- 1 tom tom 20 Mar 31 13:51 tomfile
- 这种权限下,jerry无法查看tomfile的内容,除非修改tomfile的o(ther)权限
[jerry@centos7u6 tmp]$ cat tomfile
cat: tomfile: Permission denied
使用facl
- tom赋予用户jerry对这个文件的facl的r(ead)权限和写权限,则jerry可以查看和修改tomfile,但是其他用户仍然无法查看和修改文件内容, 保证了数据的安全性
[tom@centos7u6 tmp]$ setfacl -m u:jerry:rw tomfile
[jerry@centos7u6 tmp]$ cat tomfile
This is tom's file.
[jerry@centos7u6 tmp]$ echo "jerry is coming." >> tomfile
[jerry@centos7u6 tmp]$ cat tomfile
This is tom's file.
jerry is coming.
[test@centos7u6 tmp]$ cat tomfile
cat: tomfile: Permission denied
- 使用ls查看tomfile的权限,会发现在9位权限位后,又增加了一个+号。
[tom@centos7u6 tmp]$ ls -l tomfile
-rw-rw----+ 1 tom tom 20 Mar 31 13:51 tomfile
- 使用getfacl查看tomfile的facl属性,会发现在原本的基本权限后,又添加了一个jerry用户的rw权限。
[tom@centos7u6 tmp]$ getfacl tomfile
# file: tomfile
# owner: tom
# group: tom
user::rw-
user:jerry:r--
group::rw-
mask::rw-
other::---
对组
默认情况(不使用facl)
- 现在,有一个testfile文件,文件权限如下:
[test@centos7u6 tmp]$ ls -l testfile
-rw-rw-r-- 1 test test 5 Mar 31 15:04 testfile
- tom和jerry都将workgroup作为自己的附加组,此时,tom和jerry是无法修改test文件的内容的
[root@centos7u6 tmp]# id tom
uid=1004(tom) gid=1004(tom) groups=1004(tom),1006(workgroup)
[root@centos7u6 tmp]# id jerry
uid=1005(jerry) gid=1005(jerry) groups=1005(jerry),1006(workgroup)
[tom@centos7u6 tmp]$ echo "tom is coming." >> testfile
bash: testfile: Permission denied
[jerry@centos7u6 tmp]$ echo "jerry is coming." >> testfile
bash: testfile: Permission denied
使用facl
- 此时,使用setfacl命令赋予workgroup写权限,则tom和jerry都可以修改testfile文件了,而不属于workgroup组的其他用户是无法修改testfile文件的。
[test@centos7u6 tmp]$ setfacl -m g:workgroup:rw testfile
#用户tom可以修改testfile
[tom@centos7u6 tmp]$ echo "$(whoami) is coming." >> testfile
[tom@centos7u6 tmp]$ cat testfile
test
tom is coming.
#jerry可以修改testfile
[jerry@centos7u6 tmp]$ echo "$(whoami) is coming." >> testfile
[jerry@centos7u6 tmp]$ cat testfile
test
tom is coming.
jerry is coming.
#由于用户user5不在workgroup内,所以无法修改testfile
[user5@centos7u6 tmp]$ id user5
uid=1003(user5) gid=1003(user5) groups=1003(user5)
[user5@centos7u6 tmp]$ echo "$(whoami) is coming." >> testfile
bash: testfile: Permission denied
- 使用ls查看testfile的权限,会发现在9位权限位后,又增加了一个+号。
[root@centos7u6 tmp]# ls -l testfile
-rw-rw-r--+ 1 test test 37 Mar 31 15:14 testfile
- 使用setfacl命令查看testfile的facl属性,会发现在group后,多了一个关于workgroup组的权限。
[root@centos7u6 tmp]# getfacl testfile
# file: testfile
# owner: test
# group: test
user::rw-
group::rw-
group:workgroup:rw-
mask::rw-
other::r--
进程的访问权限匹配模型
默认情况下
- 检查进程的发起者和被访问文件的属主是否相同
如果相 同,则使用文件属主的权限访问
如果不同,转到步骤2 - 检查进程的发起者和被访问文件的属组是否相同
如果相同,则使用文件属组的权限访问
如果不同,转到步骤3 - 使用other权限
使用facl的情况下
- 检查进程的发起者和被访问文件的属主是否相同
如果相同,则使用文件属主的权限访问
如果不同,转到步骤2 - 检查被访问的文件是否有关于进程发起者这个特定用户的facl
如果有,则使用facl中这个特定用户的facl权限访问
如果没有,转到步骤3 - 检查进程的发起者和被访问文件的属组是否相同
如果相同,则使用文件属组的权限访问
如果不同,转到步骤4 - 检查被访问的文件是否有关于进程发起者这个特定用户的所属组的facl
如果有,则使用facl中这个特定用户的所属组的facl权限访问
如果没有,转到步骤5 - 使用other权限