facl 文件访问控制列表 实现屏蔽某个用户权限的功能 给分区加acl挂载选项

facl(文件访问控制列表)允许设置除属主、属组、其他用户之外的额外权限。当用户访问文件时,系统会按照属主权限 -> facl -> 属组权限 -> 其他用户权限的顺序判断权限。通过facl,可以实现屏蔽特定用户对文件的访问。如果在setfacl设置时遇到不支持的情况,可能是因为分区挂载不支持acl。解决办法包括修改分区配置使其挂载时带有acl选项,或者在挂载时手动添加acl选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

facl:file access control list  文件访问控制列表

facl用于实现除文件属主、文件属组、其它用户三者权限外的额外权限功能,所以当某个用户访问

文件时,系统是按如下顺序判定用户对此文件持有哪些权限的:

属主权限----facl----属组权限----其它用户   (当在facl中找到访问此文件用户的权限规则时,就以此权限
适用该用户,不再往后匹配权限)

于是出现了下面这种屏蔽某个用户的奇怪现象:

[nokk@centos6 tmp]$whoami       //当前用户是nokk
nokk
[nokk@centos6 tmp]$ll
total 4
-rw-rw-rwx+ 1 tom tom 0 May 13 04:15 test.tom    //其它用户对test.tom文件有可读可写可执行权限
[nokk@centos6 tmp]$cat test.tom    //nokk用户cat test.tom时却提示无权限
cat: test.tom: Permission denied
[nokk@centos6 tmp]$

原因是test.tom设置了facl,查看如下

[nokk@centos6 tmp]$getfacl test.tom 
# file: test.tom
# owner: tom
# group: tom
user::rw-
user:nokk:---     //facl规则里规定nokk用户对此文件无任何权限
group::rw-
mask::rw-
other::rwx

[nokk@centos6 tmp]$

根据上面权限判定顺序,当在facl中判定nokk用户对test.tom文件无任何权限时,就不会再去查看

“其它用户”的权限,所以,即使test.tom文件“其它用户”权限为可读可写可执行,但test.tom文件的属主

tom已经通过facl规则把nokk用户屏蔽了。

NAME
       getfacl - get file access control lists
        //getfacl命令可以查看某个文件的facl规则


NAME
       setfacl - set file access control lists
        //setfacl命令可以设置文件的facl规则
设置facl规则
[tom@centos6 tmp]$getfacl test.tom 
# file: test.tom
# owner: tom
# group: tom
user::rw-
group::rw-
mask::rw-
other::rwx

[tom@centos6 tmp]$setfacl -m u:nokk:--- test.tom //设置facl规则,屏蔽nokk用户
[tom@centos6 tmp]$getfacl test.tom               
# file: test.tom
# owner: tom
# group: tom
user::rw-
user:nokk:---
group::rw-
mask::rw-
other::rwx

[tom@centos6 tmp]$
*********************************************************************

移除facl规则
[tom@centos6 tmp]$getfacl test.tom 
# file: test.tom
# owner: tom
# group: tom
user::rw-
user:nokk:---
group::rw-
mask::rw-
other::rwx

[tom@centos6 tmp]$setfacl -x u:nokk test.tom //移除facl规则中对nokk用户的限制
[tom@centos6 tmp]$getfacl test.tom           
# file: test.tom
# owner: tom
# group: tom
user::rw-
group::rw-
mask::rw-
other::rwx

[tom@centos6 tmp]$

如遇如下setfacl设置facl时提示不支持,原因可能是当前目录所在分区不支持acl。

[root@centos6 data]#ll
total 16
drwx------. 2 root root 16384 May 14 00:49 lost+found
-rw-r--r--. 1 root root     0 May 14 00:50 test
[root@centos6 data]#getfacl test 
# file: test
# owner: root
# group: root
user::rw-
group::r--
other::r--

[root@centos6 data]#setfacl -m u:tom:--- test 
setfacl: test: Operation not supported
[root@centos6 data]#

如下有两种方法使其分区挂载后支持acl:

查看当前目录所在分区,并查看分区挂载选项是否有acl,没有的话说明分区被挂载后不支持acl

[root@centos6 data]#pwd    //当前所在目录
/data
[root@centos6 data]#lsblk  //查看块设备
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  3.7G  0 rom  
sda      8:0    0   50G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0 19.5G  0 part /
├─sda3   8:3    0 19.5G  0 part /data    //sda3分区挂载在/data目录下
├─sda4   8:4    0    1K  0 part 
├─sda5   8:5    0    2G  0 part [SWAP]
└─sda6   8:6    0 13.4M  0 part 
[root@centos6 data]#
[root@centos6 data]#tune2fs -l /dev/sda3   //查看sda3分区相关分区信息
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          /data
Filesystem UUID:          41a53e2e-d77f-4ada-8712-826ac7ae2aaa
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash 
Default mount options:    (none)    //显示分区挂载选项没有acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux

第一种方法:修改分区配置,使其挂载选项有acl,这样分区被挂载时就会自动带上acl选项挂载

[root@centos6 data]#tune2fs  -o acl /dev/sda3   //修改使/sda3分区挂载选项支持acl
tune2fs 1.41.12 (17-May-2010)
[root@centos6 data]#tune2fs -l /dev/sda3     //再次查看分区信息
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          /data
Filesystem UUID:          41a53e2e-d77f-4ada-8712-826ac7ae2aaa
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash 
Default mount options:    acl    //此时显示分区挂载选项支持acl
Filesystem state:         clean
Errors behavior:          Continue

卸载分区后重新挂载分区(因为只有重新挂载分区,才会使分区的挂载选项生效),再次

setfacl配置facl,验证是否能成功配置

[root@centos6 data]#cd
[root@centos6 ~]#umount /data            //卸载分区
[root@centos6 ~]#mount /dev/sda3 /data   //重新挂载分区
[root@centos6 ~]#cd /data
[root@centos6 data]#setfacl -m u:tom:--- test  //已能正常设置facl
[root@centos6 data]#getfacl test 
# file: test
# owner: root
# group: root
user::rw-
user:tom:---      //已设置成功
group::r--
mask::r--
other::r--

[root@centos6 data]#

第二种方法:不修改分区挂载选项,而是挂载分区时手动加挂载选项,如下

[root@centos6 data]#setfacl -m u:tom:--- test 
setfacl: test: Operation not supported
[root@centos6 data]#mount -o remount,acl /dev/sda3 /data   //-o acl指明挂载选项acl
[root@centos6 data]#setfacl -m u:tom:--- test     //setfacl设置成功      
[root@centos6 data]#getfacl test 
# file: test
# owner: root
# group: root
user::rw-
user:tom:---     //facl已生效
group::r--
mask::r--
other::r--

[root@centos6 data]#

通过第二种方法挂载后,可以如下查看:

[root@centos6 data]#mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sda3 on /data type ext4 (rw,acl)   //括号里已显示分区已acl选项挂载
[root@centos6 data]#

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值