linux系统中的文件访问控制列表ACL

1、linux系统中普通权限的设置主要是针对一类情形,比如针对所有者、所属组或者其他人的设置。

    linux系统中的文件访问控制列表是针对特定具体的用户或者用户组来进行权限设置。

直接测试:

## 为了对比,先测试未分配ACL之前的情况
[root@linuxprobe test]# ls [root@linuxprobe test]# whoami root [root@linuxprobe test]# mkdir test01 [root@linuxprobe test]# ll
-d test01/ drwxr-xr-x. 2 root root 6 Oct 21 14:09 test01/ [root@linuxprobe test]# chmod 770 test01/ ## 将其他人的权限设定为0 [root@linuxprobe test]# ll -d test01/ drwxrwx---. 2 root root 6 Oct 21 14:09 test01/ [root@linuxprobe test]# su - linuxprobe ## 切换至普通用户 Last login: Wed Oct 21 13:15:32 CST 2020 from 192.168.3.4 on pts/2 [linuxprobe@linuxprobe ~]$ cd /home/test/ [linuxprobe@linuxprobe test]$ whoami linuxprobe [linuxprobe@linuxprobe test]$ ls test01 [linuxprobe@linuxprobe test]$ cd test01/ ## 其他人没有访问的权限 -bash: cd: test01/: Permission denied [linuxprobe@linuxprobe test]$ su - liujiaxin01 ## 切换至另一个普通用户 Password: Last login: Wed Oct 21 13:01:47 CST 2020 from 192.168.3.4 on pts/1 [liujiaxin01@linuxprobe ~]$ cd /home/test/ [liujiaxin01@linuxprobe test]$ ls test01 [liujiaxin01@linuxprobe test]$ whoami liujiaxin01 [liujiaxin01@linuxprobe test]$ cd test01/ ## 仍然没有访问权限 -bash: cd: test01/: Permission denied
## 测试给linuxprobe分配权限,未给liujiaxin01分配权限的情况
[liujiaxin01@linuxprobe test]$ exit logout [linuxprobe@linuxprobe test]$ exit logout [root@linuxprobe test]# whoami root [root@linuxprobe test]# ls test01 [root@linuxprobe test]# setfacl u:linuxprobe:rwx test01
/ Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ... Try `setfacl --help' for more information. [root@linuxprobe test]# setfacl -R u:linuxprobe:rwx test01/ Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ... Try `setfacl --help' for more information. [root@linuxprobe test]# setfacl -Rm u:linuxprobe:rwx test01/ ##可以不加R
## 针对目录设置ACL的基本用法,-R 表示递归 ,-m modify?,u指的是user,linuxprobe指用户,rwx指赋予用户的权限
## 上句命令的意思就是赋予给linuxprobe用户针对目录test01的读、写、执行的权限
[root@linuxprobe test]# su - linuxprobe ## 切换至linuxProbe ,查看权限设置效果 Last login: Wed Oct 21 14:10:45 CST 2020 on pts/0 [linuxprobe@linuxprobe ~]$ cd /home/test/ [linuxprobe@linuxprobe test]$ whoami linuxprobe [linuxprobe@linuxprobe test]$ ls test01 [linuxprobe@linuxprobe test]$ cd test01/ ## 设置后可以正常访问了 [linuxprobe@linuxprobe test01]$ su - liujiaxin01 ## 为了对比,切换至另一普通用户 Password: Last login: Wed Oct 21 14:13:00 CST 2020 on pts/0 [liujiaxin01@linuxprobe ~]$ cd /home/test/ [liujiaxin01@linuxprobe test]$ whoami liujiaxin01 [liujiaxin01@linuxprobe test]$ ls test01 [liujiaxin01@linuxprobe test]$ cd test01/ ## 不能正常访问 -bash: cd: test01/: Permission denied

 2、如何查看设置了ACL

## 通过普通权限查看方法查看
[liujiaxin01@linuxprobe test]$ exit logout [linuxprobe@linuxprobe test01]$ exit logout [root@linuxprobe test]# whoami root [root@linuxprobe test]# ls test01 [root@linuxprobe test]# mkdir test02 [root@linuxprobe test]# ll ## 设置过 ACL 权限字符后面多出了 “+”,test01设置了ACL,test02未设置ACL,如下图 total
4 drwxrwx---+ 2 root root 6 Oct 21 14:09 test01 drwxr-xr-x. 2 root root 6 Oct 21 14:30 test02

## 通过getfacl 命令查看
[root@linuxprobe test]# getfacl test01 # file: test01 # owner: root # group: root user::rwx user:linuxprobe:rwx ## 显示了linuxprobe权限 group::rwx mask::rwx other::
--- [root@linuxprobe test]# getfacl test02 ## 没有其他用户的权限 # file: test02 # owner: root # group: root user::rwx group::r-x other::r-x

3、给test01目录继续增加ACL

[root@linuxprobe test]# ll
total 4
drwxrwx---+ 2 root root 6 Oct 21 14:09 test01
drwxr-xr-x. 2 root root 6 Oct 21 14:30 test02
[root@linuxprobe test]# whoami
root
[root@linuxprobe test]# getfacl test01
# file: test01
# owner: root
# group: root
user::rwx
user:linuxprobe:rwx
group::rwx
mask::rwx
other::---

[root@linuxprobe test]# setfacl -Rm u:liujiaxin01:rwx test01  ## 给普通用户liujiaxin01增加rwx权限
[root@linuxprobe test]# getfacl test01  ## 查看增减效果
# file: test01
# owner: root
# group: root
user::rwx
user:linuxprobe:rwx
user:liujiaxin01:rwx
group::rwx
mask::rwx
other::---

[root@linuxprobe test]# su - liujiaxin01  ## 测试效果
Last login: Wed Oct 21 14:19:36 CST 2020 on pts/0
[liujiaxin01@linuxprobe ~]$ cd /home/test/
[liujiaxin01@linuxprobe test]$ whoami
liujiaxin01
[liujiaxin01@linuxprobe test]$ ls
test01  test02
[liujiaxin01@linuxprobe test]$ cd test01  ## 可以访问

4、如何移除ACL权限列表

## 使用 setfacl -x u:user 文件/目录,每次去除一个用户
[root@linuxprobe test]# whoami root [root@linuxprobe test]# getfacl test01 # file: test01 # owner: root # group: root user::rwx user:linuxprobe:rwx user:liujiaxin01:rwx group::rwx mask::rwx other::
--- [root@linuxprobe test]# setfacl -x u:liujiaxin01 test01 ## 去除liujiaxin01针对test01的权限 [root@linuxprobe test]# getfacl test01 # file: test01 # owner: root # group: root user::rwx user:linuxprobe:rwx group::rwx mask::rwx other::--- [root@linuxprobe test]# setfacl -x u:linuxprobe test01 ## 去除另一个用户的权限 [root@linuxprobe test]# getfacl test01 # file: test01 # owner: root # group: root user::rwx group::rwx mask::rwx other::---
##使用setfacl -b 文件/目录 一次性清空ACL
[root@linuxprobe test]# whoami root [root@linuxprobe test]# getfacl test01 # file: test01 # owner: root # group: root user::rwx group::rwx mask::rwx other::
--- [root@linuxprobe test]# setfacl -Rm u:linuxprobe:rwx test01 ## 增加一个用户的权限 [root@linuxprobe test]# getfacl test01 # file: test01 # owner: root # group: root user::rwx user:linuxprobe:rwx group::rwx mask::rwx other::--- [root@linuxprobe test]# setfacl -Rm u:liujiaxin01:rwx test01 ## 增加另一个用户的权限 [root@linuxprobe test]# getfacl test01 # file: test01 # owner: root # group: root user::rwx user:linuxprobe:rwx user:liujiaxin01:rwx group::rwx mask::rwx other::--- [root@linuxprobe test]# setfacl -b test01 ## 直接清空所有的ACL [root@linuxprobe test]# getfacl test01 # file: test01 # owner: root # group: root user::rwx group::rwx other::---

5、批量增加ACL用户

[root@linuxprobe test]# ls
a.txt  test01  test02
[root@linuxprobe test]# whoami
root
[root@linuxprobe test]# getfacl test01
# file: test01
# owner: root
# group: root
user::rwx
group::rwx
other::---

[root@linuxprobe test]# cat a.txt
u:linuxprobe:rwx
u:liujiaxin01:rwx
u:liujiaxin02:rwx
[root@linuxprobe test]# setfacl -M a.txt test01  ## -M 选项 接受列表文件,每行一个用户
[root@linuxprobe test]# getfacl test01
# file: test01
# owner: root
# group: root
user::rwx
user:linuxprobe:rwx
user:liujiaxin01:rwx
user:liujiaxin02:rwx
group::rwx
mask::rwx
other::---

6、批量删除ACL用户

[root@linuxprobe test]# getfacl test01/
# file: test01/
# owner: root
# group: root
user::rwx
user:linuxprobe:rwx
user:liujiaxin01:rwx
user:liujiaxin02:rwx
group::rwx
mask::rwx
other::---

[root@linuxprobe test]# cat b.txt
u:linuxprobe
u:liujiaxin02
[root@linuxprobe test]# setfacl -X b.txt test01  ## 大写X
[root@linuxprobe test]# getfacl test01/
# file: test01/
# owner: root
# group: root
user::rwx
user:liujiaxin01:rwx
group::rwx
mask::rwx
other::---
 
原文地址:https://www.cnblogs.com/liujiaxin2018/p/13852173.html