Skip to the content.

ACL的作用

ACL是Linux权限体系的补充,一般不是很常用。

ext2/ext3/ext4需要在mount的时候提供acl的选项才能正常使用。对于xfs是默认开启的。 检查ext文件系统是否开启了ACL特性:

[root@localhost ~]# dumpe2fs -h /dev/sda3 |grep "Default mount options"
Default mount options: user_xattr acl

开启ACL方法如下:

# 临时开启
[root@localhost ~]# mount -o remount, acl /
# 永久
[root@localhost ~]#vi /etc/fstab
UUID=c2ca6f57-b15c-43ea-bca0-f239083d8bd2    /ext4    defaults, acl    1 1
[root@localhost ~]# mount -o remount /

ACL权限设置

常见设置方法和选项:

[root@localhost ~]# getfacle 文件名
#查看ACL权限
[root@localhost ~]# setfacl 选项 文件名
#设定ACL权限
选项:
* -m:设定 ACL 权限。如果是给予用户 ACL 权限,则使用"u:用户名:权限"格式赋予;如果是给予组 ACL 权限,则使用"g:组名:权限" 格式赋予;
* -x:删除指定的 ACL 权限;
* -b:删除所有的 ACL 权限;
* -d:设定默认 ACL 权限。只对目录生效,指目录中新建立的文件拥有此默认权限;
* -k:删除默认 ACL 权限;
* -R:递归设定 ACL 权限。指设定的 ACL 权限会对目录下的所有子文件生效;

举个例子

[root@bogon ~]# mkdir /project
[root@bogon ~]# groupadd group1
[root@bogon ~]# groupadd group2
[root@bogon ~]# useradd -G group1 user1
[root@bogon ~]# useradd -G group1 user2
[root@bogon ~]# useradd -G group2 user3
[root@bogon ~]# useradd -G group2 user4
[root@bogon ~]# df -hT /
文件系统                类型  容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs    17G  3.9G   13G   24% /
[root@bogon ~]# 
[root@bogon ~]# getfacl /project/
getfacl: Removing leading '/' from absolute path names
# file: project/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x

[root@bogon ~]# 
[root@bogon ~]# setfacl --help 
setfacl 2.2.51 -- set file access control lists
Usage: setfacl [-bkndRLP] {  -m|-M|-x|-X ... } file ...
  -m, --modify=acl        modify the current ACL(s) of file(s)
  -M, --modify-file=file  read ACL entries to modify from file
  -x, --remove=acl        remove entries from the ACL(s) of file(s)
  -X, --remove-file=file  read ACL entries to remove from file
  -b, --remove-all        remove all extended ACL entries
  -k, --remove-default    remove the default ACL
      --set=acl           set the ACL of file(s), replacing the current ACL
      --set-file=file     read ACL entries to set from file
      --mask              do recalculate the effective rights mask
  -n, --no-mask           don't recalculate the effective rights mask
  -d, --default           operations apply to the default ACL
  -R, --recursive         recurse into subdirectories
  -L, --logical           logical walk, follow symbolic links
  -P, --physical          physical walk, do not follow symbolic links
      --restore=file      restore ACLs (inverse of `getfacl -R')
      --test              test mode (ACLs are not modified)
  -v, --version           print version and exit
  -h, --help              this help text
[root@bogon ~]# 
[root@bogon ~]# 
[root@bogon ~]# chown -R  :group1 /project
[root@bogon ~]# ls -ld /project/
drwxr-xr-x 2 root group1 6 10月 23 09:32 /project/
[root@bogon ~]# chmod -R 770 /project/
[root@bogon ~]# ls -ld /project/
drwxrwx--- 2 root group1 6 10月 23 09:32 /project/
[root@bogon ~]# getfacl /project/
getfacl: Removing leading '/' from absolute path names
# file: project/
# owner: root
# group: group1
user::rwx
group::rwx
other::---

# 为user3和group2修改ACL权限
[root@bogon ~]# setfacl -m u:user3:rx /project/
[root@bogon ~]# setfacl -m g:group2:rx /project/
[root@bogon ~]# getfacl /project/
getfacl: Removing leading '/' from absolute path names
# file: project/
# owner: root
# group: group1
user::rwx
user:user3:r-x
group::rwx
group:group2:r-x
mask::rwx
other::---

# 修改默认ACL权限
[root@bogon ~]# setfacl -m d:u:user4:rwx /project/
[root@bogon ~]# ls -ld /project/
drwxrwx---+ 2 root group1 6 10月 23 09:32 /project/
[root@bogon ~]# getfacl /project/
getfacl: Removing leading '/' from absolute path names
# file: project/
# owner: root
# group: group1
user::rwx
user:user3:r-x
group::rwx
group:group2:r-x
mask::rwx
other::---
default:user::rwx
default:user:user4:rwx
default:group::rwx
default:mask::rwx
default:other::---

[root@bogon ~]# setfacl -x u:user3 /project/
[root@bogon ~]# getfacl /project/
getfacl: Removing leading '/' from absolute path names
# file: project/
# owner: root
# group: group1
user::rwx
group::rwx
group:group2:r-x
mask::rwx
other::---
default:user::rwx
default:user:user4:rwx
default:group::rwx
default:mask::rwx
default:other::---

[root@bogon ~]# setfacl -b /project/
[root@bogon ~]# getfacl /project/
getfacl: Removing leading '/' from absolute path names
# file: project/
# owner: root
# group: group1
user::rwx
group::rwx
other::---

reference