> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/di-25-zhang-ufs-wen-jian-xi-tong/di-25.5-jie-ufs-ci-pan-pei-e.md).

# 25.5 UFS 磁盘配额

磁盘配额可用于限制用户或用户组在每个 UFS 文件系统中可分配的磁盘空间或文件数量，从而防止某个用户或用户组消耗所有可用的磁盘空间。

## 验证内核支持

FreeBSD 默认内核提供了对磁盘配额的支持，可运行以下命令验证：

```sh
$ sysctl kern.features.ufs_quota
kern.features.ufs_quota: 1
```

如果输出为 0，表示当前可能正在使用自定义内核，且该内核不支持磁盘配额模块。应在内核配置文件中加入内核选项 `options QUOTA`，随后重新编译内核。

## 启用磁盘配额

设置开机启用磁盘配额：

```sh
# service quota enable
```

立即启用磁盘配额：

```sh
# service quota start
```

系统在启动时会通过 quotacheck(8) 检查每个文件系统的配额完整性。此程序确保配额数据库中的数据正确反映文件系统中的数据。此过程耗时较长，会显著影响系统的启动时间。要跳过此步骤，可在 **/etc/rc.conf** 中添加以下变量：

```ini
check_quotas="NO"
```

必须在 **/etc/fstab** 中加入配额选项，以启用每个用户或每个用户组的磁盘配额。如果要启用每个用户的配额，可在文件系统的 **/etc/fstab** 配置行的选项字段中添加 `userquota`。例如：

```ini
/dev/nda0p2     /               ufs     rw,userquota      1       1
```

如果要启用组配额，可使用 `groupquota` 替代。如果同时启用用户和组配额，可用逗号分隔选项：

```ini
/dev/nda0p2     /               ufs     rw,userquota,groupquota      1       1
```

配置完成后，重新启动系统，**/etc/rc** 将自动运行相应的命令，为 **/etc/fstab** 中启用的所有配额创建初始配额文件。

默认情况下，配额文件存储在文件系统的根目录中，名为 **quota.user** 和 **quota.group**。

```sh
-rw-r-----   1 root operator -                             64192 May  1 02:34 quota.group
-rw-r-----   1 root operator -                             64192 May  1 02:34 quota.user
```

## 设置配额限制

要验证配额是否已启用，可运行以下命令：

```sh
$ quota -v
Disk quotas for user ykla (uid 1001):
Filesystem        usage    quota   limit   grace  files   quota  limit   grace
/                    32        0       0              9       0      0
```

至此，系统已可使用 `edquota` 设置配额限制。

配额限制可基于磁盘空间（块配额）、文件数量（inode 配额）或两者的组合设定。每种限制进一步细分为两个类别：硬限制和软限制。

硬限制不得超出。用户达到硬限制时，便无法再向该文件系统分配任何空间。例如，用户的文件系统硬限制为 500 KB，并且当前使用了 490 KB，则该用户只能分配额外的 10 KB。尝试分配额外的 11 KB 将失败。

软限制可在一定时间内超出，该时间段称为宽限期，默认为一周。如果用户超出软限制且宽限期已过，软限制将转变为硬限制，不再允许进一步分配空间。用户降至软限制以下时，宽限期将重置。

UFS 磁盘配额限制状态转换如下所示：

![UFS 磁盘配额限制状态转换](/files/FgmRSqutkd9DYtculv15)

在以下示例中，编辑 `ykla` 账户的配额。调用 `edquota` 时，将打开由环境变量 `EDITOR` 指定的编辑器编辑配额限制，默认编辑器为 vi(1)。

```sh
# edquota -u ykla
Quotas for user ykla:
/: in use: 32k, limits (soft = 0k, hard = 0k)	# 块配额限制
        inodes in use: 9, limits (soft = 0, hard = 0)	# inode 配额限制
```

可更改块配额和 inode 配额以配置配额限制。例如，将 **/** 的块限制提高到软限制 `100` 和硬限制 `120`，将该行的值更改为：

```sh
/: in use: 32k, limits (soft = 100k, hard = 120k)
```

退出编辑器后，新配额限制将生效。用户只能检查自己的配额和自己所属组的配额。只有超级用户才能查看所有用户和组的配额。

```sh
# quota -v ykla
Disk quotas for user ykla (uid 1001):
Filesystem        usage    quota   limit   grace  files   quota  limit   grace
/                    32      100     120              9       0      0
```

通常，如果用户在某文件系统中未使用任何磁盘空间，即使对该文件系统设置了配额，该文件系统也不会出现在 quota(1) 的输出中。

某些场景下需要为一批用户设置相同的配额限制。可先为某个用户分配所需的配额限制，再使用 `-p` 选项将该配额复制给指定 UID 范围的用户。以下命令将配额限制复制给 UID 为 `10000` 至 `19999` 的用户：

```sh
# edquota -p ykla 10000-19999
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/di-25-zhang-ufs-wen-jian-xi-tong/di-25.5-jie-ufs-ci-pan-pei-e.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
