linux访问控制列表 ACL实现文件权限设置

ACL:Access Control List,实现灵活的文件权限管理
除了文件的所有者,所属组和其它人,可以对更多的用户设置权限
CentOS7 默认创建的xfs和ext4文件系统具有ACL功能

ACL相关命令

setfacl 可设置ACL权限
getfacl 可查看设置的ACL权限

实例

[root@localhost ~]# ll test.txt 
-rw-r--r--. 1 root root 0 Oct 21 12:01 test.txt
设置用户ACL权限
[root@localhost ~]# setfacl -m u:yc:rw  test.txt
[root@localhost ~]# getfacl test.txt 
# file: test.txt
# owner: root
# group: root
user::rw-
user:yc:rw-
group::r--
mask::rw-
other::r--
[root@localhost ~]# ll test.txt 
-rw-rw-r--+ 1 root root 0 Oct 21 12:01 test.txt
注:此时rw-不再代表所属组的权限,而是指mask,mask指除所有者和other的之外的人和组的最大权限
修改mask方法:
[root@localhost ~]# chmod g=r test.txt 
[root@localhost ~]# getfacl test.txt 
# file: test.txt
# owner: root
# group: root
user::rw-
user:yc:rw-			#effective:r--
group::r--
mask::r--
other::r--
[root@localhost ~]# ll test.txt 
-rw-r--r--+ 1 root root 0 Oct 21 12:01 test.txt
注:此时用户yc对文件只有读的权限 mask使用户yc对文件的权限降级了,用户或组的设置必须存在于mask权限设定范围内才会生效

设置组ACL权限
[root@localhost ~]# setfacl -m g:yc:w  test.txt
[root@localhost ~]# getfacl test.txt 
# file: test.txt
# owner: root
# group: root
user::rw-
user:yc:rw-
group::r--
group:yc:-w-
mask::rw-
other::r--

将用户加入到组中
groupmems -g root -a yc
[root@localhost ~]# groupmems -g root -a yc
[root@localhost ~]# id yc
uid=1000(yc) gid=1000(yc) groups=1000(yc),0(root)

清除ACL权限
[root@localhost ~]# setfacl -x u:yc test.txt 

清除所有ACL权限
setfacl -b test.txt

复制test1的acl权限给test2
getfacl test1 | setfacl --set-file=- test2

复制文件时保留ACL属性
[root@localhost ~]# cp -a test.txt test3.txt
注:文件操作命令cp和mv都支持ACL,但是tar等常见的备份工具是不会保留目录和文件的ACL信息
posted @ 2021-10-20 23:08  咚咚小孩  阅读(135)  评论(0)    收藏  举报