访问控制列表

来自 Arch Linux 中文维基

访问控制列表 (ACL) 为文件系统提供了一种额外的、更加灵活的权限机制,用于完善 UNIX 的文件权限机制。ACL 允许您将任意磁盘资源授权给任意用户组或用户。

安装[编辑 | 编辑源代码]

aclsystemd 的依赖之一,应该已经安装好了。

启用 ACL[编辑 | 编辑源代码]

要启用 ACL,文件系统在挂载时必须要加入 acl 参数。要开机时自动启用 ACL,你可以通过编辑 fstab 文件来实现。

默认情况下 Btrfs、Ext2/3/4 已经启用了 acl 参数,因此无需额外指定。对于 Ext* 文件系统你可以使用以下命令检查 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 选项在创建 ext2/3/4 文件系统时将被默认启用,该行为由 /etc/mke2fs.conf 控制。
  • /proc/mounts 文件中不会列出默认挂载选项。

使用[编辑 | 编辑源代码]

设置 ACL[编辑 | 编辑源代码]

ACL 可以通过 setfacl 命令被设置。

提示:
  • 你可以通过添加 --test 参数来测试该命令的结果
  • 要递归地将操作应用到所有的文件和目录上,使用 -R/--recursive 参数。

为用户设定权限(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>

本文或本章节的事实准确性存在争议。

原因: The original note about the --mask option (which was taken from setfacl(1)) was determined as inaccurate, but the new note does not seem correct either. See the talk page for details.(在 Talk:访问控制列表#ACL mask entry 中讨论)


注意: The default behavior of setfacl is to recalculate the ACL mask entry, unless a --mask entry was explicitly given. The mask entry indicates the maximum permissions allowed for users (other than the owner) and for groups. Unless explicitly set, this will match the permissions of the default group. To clarify what this means, suppose the group owning a directory has r-x permissions. If you add an ACL user or group with rwx permissions, the effective permissions of this user or group will be r-x. The reason for this is so that there are no surprises when a file from a system which does not support ACLs is made available on a system which does..

查看 ACL[编辑 | 编辑源代码]

要显示 ACL 权限,使用:

# getfacl <file/dir>

例子[编辑 | 编辑源代码]

给予 johnnyabc 文件的所有访问权:

# 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 服务器进程的用户为 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

参阅[编辑 | 编辑源代码]