> 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-23-zhang-cun-chu-guan-li/di-23.5-jie-jia-mi-jiao-huan-fen-qu.md).

# 23.5 加密交换分区

与加密磁盘分区类似，加密交换空间用于保护敏感信息。例如，处理密码的应用程序，只要密码保留在物理内存中，就不会写入磁盘，并在重启后被清除。然而，如果 FreeBSD 开始将内存页面换出以释放空间，密码可能会以未加密的形式写入磁盘。加密交换空间可以解决此问题。

本节演示如何使用 geli 配置加密交换分区。使用 `swapinfo` 命令查看当前的交换分区：

```sh
$ swapinfo
Device          1K-blocks     Used    Avail Capacity
/dev/nda0p2       8388608        0  8388608     0%
```

输出表明，当前交换分区为 **/dev/nda0p2**。

## 配置交换分区加密

默认未加密交换分区，在启用加密之前，应先清除交换分区中可能存在的敏感数据。执行以下命令，可以用随机垃圾覆盖当前的交换分区：

```sh
# dd if=/dev/urandom of=/dev/nda0p2 bs=1m
```

> **警告**
>
> 此命令存在风险，请谨慎确认磁盘分区后再操作！

如果要使用 geli(8) 加密交换分区，编辑 **/etc/fstab** 文件，使用 **.eli** 后缀，修改原有交换分区条目。以下示例配置了使用 AES-XTS 算法、256 位密钥长度和 4 千字节扇区大小的加密交换分区：

```ini
/dev/nda0p2.eli   none        swap    sw,ealgo=AES-XTS,keylen=256,sectorsize=4096    0       0
```

默认情况下，geli(8) 使用 AES-XTS 加密算法，密钥长度为 **128 位**。若需更高安全性可手动指定 `keylen=256`。默认设置即可满足需求。如果需要，可以在 **/etc/fstab** 中的选项字段更改这些默认设置。可用的标志如下：

| 标志             | 说明                                                                                                      |
| -------------- | ------------------------------------------------------------------------------------------------------- |
| **aalgo**      | 用于确保加密数据未被篡改的数据完整性验证算法。支持 HMAC/SHA1、HMAC/RIPEMD160、HMAC/SHA256、HMAC/SHA384、HMAC/SHA512，推荐使用 HMAC/SHA256 |
| **ealgo**      | 用于保护数据的加密算法。支持 AES-XTS、AES-CBC、Camellia-CBC（不推荐将 CBC 模式用于新部署），默认和推荐算法为 AES-XTS                          |
| **keylen**     | 用于加密算法的密钥长度（以位为单位）                                                                                      |
| **sectorsize** | 数据加密前拆分的块大小。较大的扇区大小会提高性能，但会增加存储开销。推荐大小为 4096 字节                                                         |

## 验证加密交换分区

重启系统后，可以使用 `swapinfo` 验证加密交换分区是否正常运行。

```sh
$ swapinfo
Device          1K-blocks     Used    Avail Capacity
/dev/nda0p2.eli   8388608        0  8388608     0%
```


---

# 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-23-zhang-cun-chu-guan-li/di-23.5-jie-jia-mi-jiao-huan-fen-qu.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.
