FACL 访问控制(file access control list)
意义:一般权限只针对某一类用户设置,如果希望对某个指定的用户进行单独的权限控制,就需要用到文件的访问控制列表(ACL),设置ACL只能是管理员用户,相关命令:getfacl,setfacl。
ACL的基本用法
//环境的搭建
[root@hjh test]# ll test.txt
---------- 1 root root 10 Sep 16 22:10 test.txt
[root@hjh test]# chmod 644 test.txt
[root@hjh test]# ll test.txt
-rw-r--r-- 1 root root 10 Sep 16 22:10 test.txt
// 使用getfacl查看权限
[root@hjh test]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
group::r--
other::r--
//设定acl案例如下
要求:
hjh 拥有读写权限 rw
cyy 没有任何权限 -
fgf 组拥有读权限 r
其他用户拥有读写权限 rw
//建立相关的用户组
[root@hjh test]# useradd hjh
useradd: user 'hjh' already exists
[root@hjh test]# useradd fgf
[root@hjh test]# useradd cyy
//增加用户hjh的权限
[root@hjh test]# setfacl -m u:hjh:rw test.txt
-m 更改访问列表
[root@hjh test]# setfacl -m u:cyy:- test.txt
[root@hjh test]# setfacl -m g:fgf:r test.txt
[root@hjh test]# setfacl -m o::rw test.txt
//再次查看权限
[root@hjh test]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:hjh:rw-
user:cyy:---
group::r--
group:fgf:r--
mask::rw-
other::rw-
//我们再次用ll 查看文件权限
[root@hjh test]# ll test.txt
-rw-rw-rw-+ 1 root root 10 Sep 16 22:10 test.txt
//你会发现权限莫名其妙的变了一些,但是你并没有操作过root
并在最后多了个 + ,这是在提示你该使用geifacl命令
去查看详细的文件权限。
//我们将hjh和cyy加入到fgf组中看看这是他们的权限会有怎样的效果
[root@hjh test]# usermod -G fgf cyy
[root@hjh test]# id cyy
uid=5011(cyy) gid=5019(cyy) groups=5019(cyy),5018(fgf)
[root@hjh test]# gpasswd -a hjh fgf
Adding user hjh to group fgf
[root@hjh test]# id hjh
uid=1000(hjh) gid=1000(hjh) groups=1000(hjh),5018(fgf)
//开始验证
[hjh@hjh test]$ cat test.txt
test
test
[hjh@hjh test]$ echo test > test.txt
[hjh@hjh test]$ cat test.txt
test
`
[cyy@hjh test]$ cat test.txt
cat: test.txt: Permission denied
[cyy@hjh test]$ echo test > test.txt
-bash: test.txt: Permission denied
//从上面的实验可以看出当用户权限和组权限冲突时,
首先看用户权限。
//我们再来做个实验,将用户hjh的权限置空,
将hjh加入两个权限不同组中,看结果如何
[root@hjh test]# setfacl -x u:hjh test.txt
[root@hjh test]# setfacl -x u:cyy test.txt
[root@hjh test]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:fgf:rw-
group::r--
group:fgf:r--
group:sgfs:rw-
mask::rw-
other::rw-
//将hjh再加入到sgfs的组中
[root@hjh test]# gpasswd -a hjh sgfs
Adding user hjh to group sgfs
//hjh
[hjh@hjh test]$ cat test.txt
test
[hjh@hjh test]$ echo test > test.txt
-bash: test.txt: Permission denied
//从上面看似乎是执行权限较小的那个 。
(我觉得大家是使用这种冲突权限的时候最好是以实际操作结果为准,不要想当然)
ACL 高级特性 MASK
mask:用于临时降低用户或组(除属主和其他人)的权限
mask:决定了他们最高权限 建议:我们一般都把其他人权限置空。
//搭建环境
[root@hjh test]# setfacl o::- test.txt
[root@hjh test]# setfacl -m m::- test.txt
[root@hjh test]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:fgf:rw- #effective:---
group::r-- #effective:---
group:fgf:r-- #effective:---
group:sgfs:rw- #effective:---
mask::---
other::---
//我们切换到hjh我来体验一下
[hjh@hjh test]$ cat test.txt
cat: test.txt: Permission denied
[hjh@hjh test]$ echo test > test.txt
-bash: test.txt: Permission denied
//我们把 O 的权限还回去看会出现什么情况
[root@hjh test]# setfacl -m o::rw test.txt
[root@hjh test]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:fgf:rw-
group::r--
group:fgf:r--
group:sgfs:rw-
mask::rw-
other::rw-
//这里我们看到当我们改变other mask也会随着改变,
我们再次把mask置空。
[root@hjh test]# setfacl -m m::- test.txt
[root@hjh test]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:fgf:rw- #effective:---
group::r-- #effective:---
group:fgf:r-- #effective:---
group:sgfs:rw- #effective:---
mask::---
other::rw-
//此时再来hjh 你会发现 hjh已经不受约束了。
[hjh@hjh test]$ cat test.txt
test
[hjh@hjh test]$ echo test > test.txt
ACL高级特性Default
defalut : 继承(默认)
[root@hjh tmp]# ll
drwxr-x--- 2 root root 6 Sep 25 19:10 test
[root@hjh tmp]# getfacl test/
# file: test/
# owner: root
# group: root
user::rwx
group::r-x
other::---
[root@hjh tmp]# setfacl -m d:u:hjh:7 test/
[root@hjh tmp]# getfacl test/
# file: test/
# owner: root
# group: root
user::rwx
group::r-x
other::---
default:user::rwx
default:user:hjh:rwx
default:group::r-x
default:mask::rwx
default:other::---
[root@hjh tmp]# cd test/
[root@hjh test]# touch test.txt
[root@hjh test]# ll
total 0
-rw-rw----+ 1 root root 0 Sep 25 19:20 test.txt
[root@hjh test]# getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:hjh:rwx #effective:rw-
group::r-x #effective:r--
mask::rw-
other::---
//我们切换到用户`hjh`
[hjh@hjh tmp]$ cd test/
-bash: cd: test/: Permission denied
[root@hjh tmp]# setfacl -m u:hjh:1 test/
[root@hjh tmp]# getfacl test/
# file: test/
# owner: root
# group: root
user::rwx
user:hjh:--x
group::r-x
mask::r-x
other::---
default:user::rwx
default:user:hjh:rwx
default:group::r-x
default:mask::rwx
default:other::---
//再次切换到`hjh`
[hjh@hjh tmp]$ cd test/
[hjh@hjh test]$ touch /test
touch: setting times of ‘/test’: Permission denied
[hjh@hjh test]$ ll
ls: cannot open directory .: Permission denied
[hjh@hjh test]$ echo test > test.txt
[hjh@hjh test]$ cat test.txt
test
//从上面的命令我们可以得出一个结论,default新生成下面的文件生效,对目录本身不生效。