# 17.6.管理 jail

## 17.6.1. 列出正在运行的 Jail

要列出主机系统上正在运行的 jail，可以使用 [jls(8)](https://man.freebsd.org/cgi/man.cgi?query=jls\&sektion=8\&format=html) 命令：

```sh
# jls
```

输出应该类似于以下内容：

```sh
JID  IP Address      Hostname                      Path
     1  192.168.250.70  classic                       /usr/local/jails/containers/classic
```

[jls(8)](https://man.freebsd.org/cgi/man.cgi?query=jls\&sektion=8\&format=html) 支持参数 `--libxo`，能通过 [libxo(3)](https://man.freebsd.org/cgi/man.cgi?query=libxo\&sektion=3\&format=html) 库以其他格式显示输出，例如 `JSON`、`HTML` 等。

例如，执行以下命令以获取 `JSON` 输出：

```sh
# jls --libxo=json
```

输出应该类似于如下内容：

```json
{"__version": "2", "jail-information": {"jail": [{"jid":1,"ipv4":"192.168.250.70","hostname":"classic","path":"/usr/local/jails/containers/classic"}]}}
```

## 17.6.2. 启动、重启和停止 Jail

[service(8)](https://man.freebsd.org/cgi/man.cgi?query=service\&sektion=8\&format=html) 用于在主机上启动、重启或停止一个 jail。

例如，要启动一个 jail，运行以下命令：

```sh
# service jail start jailname
```

将 `start` 参数更改为 `restart` 或 `stop` 可以执行其他操作，如重启或停止 jail。

## 17.6.3. 销毁 Jail

销毁一个 jail 不是像使用 [service(8)](https://man.freebsd.org/cgi/man.cgi?query=service\&sektion=8\&format=html) 停止 jail 并删除 jail 目录和 **/etc/jail.conf** 条目那么简单。

FreeBSD 非常重视系统安全。因此，某些文件即使是 root 用户也不能删除。这种功能称为文件标志（File Flags）。

第一步是停止所需的 jail，执行以下命令：

```sh
# service jail stop jailname
```

第二步是使用 [chflags(1)](https://man.freebsd.org/cgi/man.cgi?query=chflags\&sektion=1\&format=html) 移除这些标志，执行以下命令，其中 `classic` 是要删除的 jail 名称：

```sh
# chflags -R 0 /usr/local/jails/containers/classic
```

第三步是删除包含 jail 的目录：

```sh
# rm -rf /usr/local/jails/containers/classic
```

最后，需要删除 **/etc/jail.conf** 或 **jail.conf.d** 中的 jail 条目。

## 17.6.4. 在 Jail 中处理软件包

[pkg(8)](https://man.freebsd.org/cgi/man.cgi?query=pkg\&sektion=8\&format=html) 工具支持 `-j` 参数，用于处理安装在 jail 中的软件包。

例如，要在 jail 中安装 [www/nginx-lite](https://cgit.freebsd.org/ports/tree/www/nginx-lite/)，可以从主机执行以下命令：

```sh
# pkg -j classic install nginx-lite
```

有关在 FreeBSD 中使用软件包的更多信息，请参见 [安装应用程序：软件包和 Ports](https://docs.freebsd.org/en/books/handbook/ports/#ports)。

## 17.6.5. 访问 Jail

虽然前面提到最好从主机系统管理 jails，但可以使用 [jexec(8)](https://man.freebsd.org/cgi/man.cgi?query=jexec\&sektion=8\&format=html) 进入一个 jail。

可以从主机系统执行 [jexec(8)](https://man.freebsd.org/cgi/man.cgi?query=jexec\&sektion=8\&format=html) 命令来进入 jail：

```sh
# jexec -u root jailname
```

当进入 jail 时，系统将显示在 [motd(5)](https://man.freebsd.org/cgi/man.cgi?query=motd\&sektion=5\&format=html) 中配置的消息。

## 17.6.6. 在 Jail 中执行命令

要从主机系统在 jail 中执行命令，可以使用 [jexec(8)](https://man.freebsd.org/cgi/man.cgi?query=jexec\&sektion=8\&format=html)。

例如，要停止在 jail 中运行的服务，可以执行以下命令：

```sh
# jexec -l jailname service nginx stop
```


---

# 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-17-zhang-jail-yu-rong-qi/17.6.-guan-li-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.
