# 17.7.更新 jail

## 17.7.1. 升级经典 Jail 或使用 OpenZFS 快照的瘦 Jail

Jails **必须从主机**操作系统进行更新。FreeBSD 的默认行为是不允许在 jail 中使用 [chflags(1)](https://man.freebsd.org/cgi/man.cgi?query=chflags\&sektion=1\&format=html)，这会阻止某些文件的更新，因此从 jail 内部更新将失败。

要将 jail 更新为正在运行的 FreeBSD 版本的最新补丁版本，请在主机上执行以下命令：

```sh
# freebsd-update -j classic fetch install
# service jail restart classic
```

要将 jail 升级到新的主要或次要版本，首先按照 [执行主要和次要版本升级](https://docs.freebsd.org/en/books/handbook/cutting-edge/#freebsdupdate-upgrade) 的说明升级主机系统。待主机系统已升级并重新启动，便可以升级 jail。

> **技巧**
>
> 在版本之间进行升级时，创建一个新的 jail 比完全升级更容易。

例如，要将版本从 13.1-RELEASE 升级到 13.2-RELEASE，请在主机上执行以下命令：

```sh
# freebsd-update -j classic -r 13.2-RELEASE upgrade
# freebsd-update -j classic install
# service jail restart classic
# freebsd-update -j classic install
# service jail restart classic
```

> **注意**
>
> 必须执行两次 `install` 步骤。第一次升级内核，第二次升级其他组件。

然后，如果是主要版本升级，则需要重新安装所有已安装的软件包并重新启动 jail。这是因为在进行主要版本之间的升级时，ABI 版本会发生变化。

从主机执行以下命令：

```sh
# pkg -j jailname upgrade -f
# service jail restart jailname
```

## 17.7.2. 使用 NullFS 升级瘦 Jail

由于使用 NullFS 的瘦 Jail 共享大部分系统目录，因此它们非常容易更新。只需要更新模板即可，这样就可以同时更新多个 jail。

要将模板更新为正在运行的 FreeBSD 版本的最新补丁版本，请在主机上执行以下命令：

```sh
# freebsd-update -b /usr/local/jails/templates/13.1-RELEASE-base/ fetch install
# service jail restart
```

要将模板升级到新的主要或次要版本，首先按照 [执行主要和次要版本升级](https://docs.freebsd.org/en/books/handbook/cutting-edge/#freebsdupdate-upgrade) 的说明升级主机系统。待主机系统已升级并重新启动，便可以升级模板。

例如，要将版本从 13.1-RELEASE 升级到 13.2-RELEASE，请在主机上执行以下命令：

```sh
# freebsd-update -b /usr/local/jails/templates/13.1-RELEASE-base/ -r 13.2-RELEASE upgrade
# freebsd-update -b /usr/local/jails/templates/13.1-RELEASE-base/ install
# service jail restart
# freebsd-update -b /usr/local/jails/templates/13.1-RELEASE-base/ install
# service jail restart
```


---

# 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-17-zhang-jail-yu-rong-qi/17.7.-geng-xin-jail.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.
