# 16.11.访问控制列表（ACL）

访问控制列表（ACL）扩展了传统的 UNIX® 文件权限，允许在每个文件或目录的基础上对用户和组提供细粒度的访问控制。每个 ACL 条目定义一个用户或组以及相关的权限，如读取、写入和执行。FreeBSD 提供了命令 [getfacl(1)](https://man.freebsd.org/cgi/man.cgi?query=getfacl\&sektion=1\&format=html) 和 [setfacl(1)](https://man.freebsd.org/cgi/man.cgi?query=setfacl\&sektion=1\&format=html) 来管理 ACL。

ACL 在需要比标准权限更具体访问控制的场景中非常有用，通常用于多用户环境或共享托管。然而，复杂性可能是不可避免的，因此需要仔细规划，以确保提供所需的安全性。

> **注意**
>
> FreeBSD 支持在 UFS 和 OpenZFS 中实现 NFSv4 ACL。请注意，某些参数在 [setfacl(1)](https://man.freebsd.org/cgi/man.cgi?query=setfacl\&sektion=1\&format=html) 命令中仅适用于 POSIX ACL，其他则适用于 NFSv4 ACL。

## 16.11.1. 启用 UFS 中的 ACL 支持

ACL 通过挂载时的管理标志 `acls` 启用，可以在 **/etc/fstab** 中添加此标志。

因此，需要访问 **/etc/fstab**，并在选项部分添加 `acls` 标志，如下所示：

```ini
# 设备        挂载点          文件系统类型    选项       Dump      Pass#
/dev/ada0s1a    /                 ufs       rw,acls     1         1
```

## 16.11.2. 获取 ACL 信息

可以使用 [getfacl(1)](https://man.freebsd.org/cgi/man.cgi?query=getfacl\&sektion=1\&format=html) 检查文件或目录的 ACL。

例如，要查看 **\~/test** 文件的 ACL 设置，请执行以下命令：

```sh
% getfacl test
```

如果使用 NFSv4 ACL，输出应类似于以下内容：

```sh
# 文件: test
# 所有者: freebsduser
# 组: freebsduser
            owner@:rw-p--aARWcCos:-------:allow
            group@:r-----a-R-c--s:-------:allow
         everyone@:r-----a-R-c--s:-------:allow
```

如果使用 POSIX.1e ACL，输出应类似于以下内容：

```sh
# 文件: test
# 所有者: freebsduser
# 组: freebsduser
user::rw-
group::r--
other::r--
```

## 16.11.3. 操作 ACL

[setfacl(1)](https://man.freebsd.org/cgi/man.cgi?query=setfacl\&sektion=1\&format=html) 可用于向文件或目录添加、修改或删除 ACL。

如上所述，某些 [setfacl(1)](https://man.freebsd.org/cgi/man.cgi?query=setfacl\&sektion=1\&format=html) 的参数不适用于 NFSv4 ACL，反之亦然。本节介绍如何执行适用于 POSIX ACL 和 NFSv4 ACL 的命令，并展示两者的示例。

例如，要设置 POSIX.1e 默认 ACL 的强制元素：

```sh
% setfacl -d -m u::rwx,g::rx,o::rx,mask::rwx directory
```

此示例为文件所有者的 POSIX.1e ACL 条目设置读、写和执行权限，并为文件上的邮件组设置读写权限：

```sh
% setfacl -m u::rwx,g:mail:rw file
```

要在 NFSv4 ACL 中执行与上一个示例相同的操作：

```sh
% setfacl -m owner@:rwxp::allow,g:mail:rwp::allow file
```

要从 POSIX.1e ACL 中删除所有 ACL 条目，但保留文件的三个必需条目：

```sh
% setfacl -bn file
```

要从 NFSv4 ACL 中删除所有 ACL 条目：

```sh
% setfacl -b file
```

有关这些命令的选项，请参考 [getfacl(1)](https://man.freebsd.org/cgi/man.cgi?query=getfacl\&sektion=1\&format=html) 和 [setfacl(1)](https://man.freebsd.org/cgi/man.cgi?query=setfacl\&sektion=1\&format=html)。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/hanbook/di-16-zhang-an-quan/16.11.-fang-wen-kong-zhi-acl.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
