Access Control Lists (简体中文)
访问控制列表 (ACL) 为文件系统提供了一种附加的、更灵活的权限机制。它的设计目的是帮助 UNIX 文件权限。ACL 允许您将任何用户或组的权限授予任何磁盘资源
安装
acl 是 systemd 的依赖之一, 它应该已经被安装了.
启用 ACL
要启用 ACL,文件系统在挂载时必须要加入 acl
参数。要开机时自动启用 ACL,你可以通过编辑 fstab 文件来实现。
需要注意的是对于 Btrfs、Ext2/3/4 文件系统已经默认启用了 acl
参数。对于 Ext2/3/4 文件系统,你可以通过以下方式检查 acl
参数是否在默认情况已经启用:
# tune2fs -l /dev/sdXY | grep "Default mount options:"
Default mount options: user_xattr acl
在某些情况下默认挂载选项可能会被覆盖,此时你可以在 /proc/mounts
文件中的相关行中看到 noacl
。
你可以通过 tune2fs -o option partition
命令设置文件系统的默认挂载选项。例如:
# tune2fs -o acl /dev/sdXY
当磁盘被设置默认挂载方式后,无论以后它被哪一台 Linux 挂载,都将默认启用 acl
选项,而无需手动编辑 /etc/fstab
文件。
acl
选项在创建 ext2/3/4 文件系统时将被默认启用,该行为被/etc/mke2fs.conf
控制。/proc/mounts
文件中不会列出默认挂载选项。
使用
设置 ACL
ACL 可以通过 setfacl 命令被设置。
--test
参数来测试该命令的结果为用户设定权限(user
是用户名或 UID):
# setfacl -m "u:user:permissions" <file/dir>
为用户组设定权限 (group
是用户组名或 GID):
# setfacl -m "g:group:permissions" <file/dir>
为其他用户设定权限:
# setfacl -m "other:permissions" <file/dir>
允许所有新创建的文件或目录从父目录继承条目(这不会影响将复制到目录中的文件):
# setfacl -dm "条目" <目录>
删除权限:
# setfacl -x "用户/用户组" <文件/目录>
删除默认权限
# setfacl -k <文件/目录>
删除所有ACL权限(拥有者、用户组和其他用户的权限将被保留)
# setfacl -b <file/dir>
--mask
entry was explicitly given. The mask entry is set to the union of all permissions of the owning group, and all named user and group entries (These are exactly the entries affected by the mask entry).-R
参数。查看 ACL
要显示 ACL 权限,使用:
# getfacl <file/dir>
例子
给予 johnny
对 abc
文件的所有访问权:
# setfacl -m "u:johnny:rwx" abc
检查权限:
# getfacl abc
# file: abc # owner: someone # group: someone user::rw- user:johnny:rwx group::r-- mask::rwx other::r--
改变 johnny
的权限:
# setfacl -m "u:johnny:r-x" abc
检查权限:
# getfacl abc
# file: abc # owner: someone # group: someone user::rw- user:johnny:r-x group::r-- mask::r-x other::r--
删除所有已添加的 ACL 权限信息:
# setfacl -b abc
检查权限:
# getfacl abc
# file: abc # owner: someone # group: someone user::rw- group::r-- other::r--
ACL 对 ls 命令的影响
当使用 ls -l
命令时,含有ACL权限的文件将会显示一个 +
$ ls -l /dev/audio
crw-rw----+ 1 root audio 14, 4 nov. 9 12:49 /dev/audio
$ getfacl /dev/audio
getfacl: Removing leading '/' from absolute path names # file: dev/audio # owner: root # group: audio user::rw- user:solstice:rw- group::rw- mask::rw- other::---
授予 WEB 服务器私有文件的执行权限
下面的例子演示了如何对 WEB 服务器这样的进程赋予其访问用户目录的权限,以便防止其越权处理文件。
假设运行该 WEB 服务器进程的用户为 http
,授予其访问 /home/geoffrey
的权限。
首先授予 http
执行权限:
# setfacl -m "u:http:--x" /home/geoffrey
由于 http
现在已经对 /home/geoffrey
拥有执行权,下面移除 other 对 /home/geoffrey
的权限:
# chmod o-rx /home/geoffrey
使用getfacl
查看更改:
$ getfacl /home/geoffrey
getfacl: Removing leading '/' from absolute path names # file: home/geoffrey # owner: geoffrey # group: geoffrey user::rwx user:http:--x group::r-x mask::r-x other::---
正如上面的输出所示,other
不再具有任何权限,但是用户 http
仍然能够访问文件,因此可以认为安全性有所提高。
http
提供特定目录或文件的写访问权:
# setfacl -dm "u:http:rwx" /home/geoffrey/project1/cache
参阅
- getfacl(1)
- setfacl(1)
- 一个虽然旧但却全面的 ACL 指导
- 如何为文件和文件夹设置默认权限?