> 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/handbook/di-21-zhang-cun-chu/21.13.-jia-mi-jiao-huan-fen-qu.md).

# 21.13.加密交换分区

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

本节演示如何使用 gbde 或 geli 加密来配置加密交换分区。假设 **/dev/ada0s1b** 是交换分区。

## 21.13.1. 配置加密交换分区

交换分区默认不会加密，在继续之前，应清除其中的敏感数据。要用随机垃圾覆盖当前的交换分区，可执行以下命令：

```sh
# dd if=/dev/random of=/dev/ada0s1b bs=1m
```

要使用 [gbde(8)](https://man.freebsd.org/cgi/man.cgi?query=gbde\&sektion=8\&format=html) 加密交换分区，可为 **/etc/fstab** 中的交换行添加 `.bde` 后缀：

```sh
# Device		Mountpoint	FStype	Options		Dump	Pass#
/dev/ada0s1b.bde	none		swap	sw		0	0
```

如果要使用 [geli(8)](https://man.freebsd.org/cgi/man.cgi?query=geli\&sektion=8\&format=html) 加密交换分区，请使用一次性密钥初始化设备：

```sh
# geom eli onetime -d /dev/ada0s1b
```

之后，在 **/etc/fstab** 中为该设备添加带有 `.eli` 后缀的行：

```sh
# Device		Mountpoint	FStype	Options		Dump	Pass#
/dev/ada0s1b.eli	none		swap	sw		0	0
```

默认情况下，[geli(8)](https://man.freebsd.org/cgi/man.cgi?query=geli\&sektion=8\&format=html) 使用 AES 算法，密钥长度为 128 位。通常默认设置即可满足需求。如果需要，可在 **/etc/fstab** 中的选项字段更改这些默认设置。可用的标志有：

* **aalgo**：用于确保加密数据未被篡改的数据完整性验证算法。有关支持的算法列表，请参阅 [geli(8)](https://man.freebsd.org/cgi/man.cgi?query=geli\&sektion=8\&format=html)。
* **ealgo**：用于保护数据的加密算法。有关支持的算法列表，请参阅 [geli(8)](https://man.freebsd.org/cgi/man.cgi?query=geli\&sektion=8\&format=html)。
* **keylen**：用于加密算法的密钥长度。有关每种加密算法支持的密钥长度，请参阅 [geli(8)](https://man.freebsd.org/cgi/man.cgi?query=geli\&sektion=8\&format=html)。
* **sectorsize**：数据加密前拆分的块大小。较大的扇区大小会提高性能，但会增加存储开销。推荐大小为 4096 字节。

以下示例配置了使用 AES-XTS 算法、128 位密钥长度和 4 千字节扇区大小的加密交换分区：

```sh
# Device		Mountpoint	FStype	Options				Dump	Pass#
/dev/ada0s1b.eli	none		swap	sw,ealgo=AES-XTS,keylen=128,sectorsize=4096	0	0
```

## 21.13.2. 验证加密交换分区

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

如果使用的是 [gbde(8)](https://man.freebsd.org/cgi/man.cgi?query=gbde\&sektion=8\&format=html)：

```sh
% swapinfo
Device          1K-blocks     Used    Avail Capacity
/dev/ada0s1b.bde   542720        0   542720     0
```

如果使用的是 [geli(8)](https://man.freebsd.org/cgi/man.cgi?query=geli\&sektion=8\&format=html)：

```sh
% swapinfo
Device          1K-blocks     Used    Avail Capacity
/dev/ada0s1b.eli   542720        0   542720     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/handbook/di-21-zhang-cun-chu/21.13.-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.
