# 20.13.加密交换分区

## 20.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
# 设备            挂载点      文件系统类型    选项           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) 加密交换分区，则使用 `.eli` 后缀：

```sh
# 设备            挂载点      文件系统类型    选项           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
# 设备            挂载点      文件系统类型    选项                                Dump   Pass#
/dev/ada0s1b.eli   none        swap    sw,ealgo=AES-XTS,keylen=128,sectorsize=4096    0       0
```

## 20.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: 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://handbook.bsdcn.org/di-20-zhang-cun-chu/20.13.-jia-mi-ci-pan-fen-qu.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.
