> 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-31-zhang-linux-jail/di-31.6-jie-linux-jail-zhong-de-gui.md).

# 31.6 Linux Jail 中的 GUI

## Jail 中的 GUI

本节示例环境为 Windows 10 物理机，在 VirtualBox 中安装了 FreeBSD 13.1 虚拟机。

在 FreeBSD 虚拟机中已部署 4 个 Jail。其中包含一个 FreeBSD Jail，为区别于 VirtualBox 中的 FreeBSD 虚拟机，称其为 `freebsd-jail`：

Windows 10 物理机 → FreeBSD 13.1 虚拟机 → FreeBSD Jail（`freebsd-jail`）+ debian Jail + Ubuntu Jail + Alpine Jail。

```sh
# jls
   JID  IP Address      Hostname                      Path
     1  192.168.5.1     debian                      /usr/jails/debian
     2  192.168.5.2     ubuntu                    /usr/jails/ubuntu
     3  192.168.5.4     alpine                        /usr/jails/alpine
     4  192.168.5.5     freebsd-jail                  /usr/jails/freebsd-jail
```

目录结构：

```sh
/usr/jails/
├── debian/          # JID 1, IP 192.168.5.1
├── ubuntu/          # JID 2, IP 192.168.5.2
├── alpine/          # JID 3, IP 192.168.5.4
└── freebsd-jail/    # JID 4, IP 192.168.5.5
```

在 4 个 Jail 中分别安装 xclock、Firefox、Chrome 和 jwm，其中 jwm 用于界面管理。Alpine 中 VNC 方法未能调试成功，Firefox 和 Chrome 也未能运行，但 xclock、xterm 等可正常运行。

> **技巧**
>
> 无需安装 xorg。

在 Ubuntu 22.04 中，Firefox 默认以 snap 包形式分发，snap 依赖 systemd，无法在 Jail 中使用，因此需使用 deb 包安装。Chrome 在 Ubuntu 中仅提供 deb 包，不依赖 snap，可直接安装。方法如下：

```sh
# 下载 Google Chrome 稳定版安装包
# wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

# 使用 dpkg 安装下载的 deb 包
# dpkg -i google-chrome-stable_current_amd64.deb

# 如果 dpkg 安装有依赖问题，可使用 apt 安装本地 deb 包并自动处理依赖
# apt install ./google-chrome-stable_current_amd64.deb
```

## 带 X Server 的终端

使用 MobaXterm。MobaXterm 采用默认配置，无需额外配置。

![配置 MobaXterm](/files/HxlvKFR7LDyxsIOvHe8a)

确保 X server 已经启用，记录 `DISPLAY` 值，格式是 `hostname:displaynumber.screennumber`，这里是 **192.168.56.1:0.0**。

登录 Jail（登录方式不限、用户不限）：

```sh
$ export DISPLAY=192.168.56.1:0.0  # Jail 中，此处为 sh/zsh/bash。csh/tcsh 是 setenv DISPLAY 192.168.56.1:0.0，下同
$ xclock&  # 在后台运行 xclock
```

![运行 xclock](/files/gpCsFBS4Gs2sHUMfeTLT)

在 Windows 10 桌面上显示 4 个独立的 xclock，均可正常运行。

## 内嵌 X Server

Xnest/Xephyr 是嵌套式 X Server 技术，对 X11 应用而言是 X Server，对宿主 X Server 而言是 X Client，这种嵌套结构可在一个 X 窗口中运行另一个完整的 X 会话。

Xnest/Xephyr 嵌套 X Server 架构：

![X Server 架构](/files/Y55XyjAPWFeprYvylppj)

在 FreeBSD 虚拟机里安装 Xnest 或 Xephyr。选择其中一种即可：

```sh
# pkg install xorg-nestserver
```

或：

```sh
# pkg install xephyr
```

在 FreeBSD 中启用：

```sh
$ Xephyr :1 -listen tcp
```

参数说明：

* `:1` 即 `DISPLAY` 值中的 `displaynumber`。FreeBSD 系统 IP 为 **10.0.2.15**，故完整的 `DISPLAY` 值为 **10.0.2.15:1.0**。因为 FreeBSD 系统 X Server 的 `displaynumber` 值为 `0`，故从 `1` 开始。
* `-listen tcp` 监听 TCP 端口。

在 Jail 中，使用与前述相同的方法：

```sh
$ export DISPLAY=10.0.2.15:1.0
$ xclock&
```

四个 Jail 可以同时在 FreeBSD 开启的一个 Xnest/Xephyr 窗口中打开 xclock。但此时没有窗口管理器，xclock 缺少窗口装饰和基本交互功能。可在执行 xclock 前先执行 jwm，如下：

```sh
$ export DISPLAY=10.0.2.15:1.0
$ jwm &
$ xclock&
```

jwm 执行一次即可，无需在每个 Jail 中分别执行。此处使用 jwm 是因为其轻量级，Xfce 等桌面环境也可使用，可根据需要选用。

## 共享主机 socket 方式

先在 FreeBSD 系统上执行：

```sh
$ xhost +
```

> **警告**
>
> `xhost +` 将关闭访问控制，存在安全风险。更安全的方式是指定允许访问的 IP 地址。

随后在 Jail 的 `fstab` 文件中加入下面内容，以 ubuntu Jail 的 `fstab` 为例，其他 Jail 参照修改即可：

```ini
/tmp/.X11-unix   /usr/jails/ubuntu/tmp/.X11-unix    nullfs   ro   0  0
```

必要时先 `mkdir -p /usr/jails/ubuntu/tmp/.X11-unix`，确保有挂载点。

重启 Jail 后，在 Jail 中执行：

```sh
$ export DISPLAY=:0.0
$ xclock &
```

前文提到 `fstab` 文件中有下面这样一行：

```sh
#/tmp   /usr/jails/ubuntu/tmp   nullfs  rw    0  0
```

该写法将 FreeBSD 的 **/tmp** 目录暴露给 Jail 并且可读写，破坏隔离性，因此予以注释。而仅挂载 **/tmp/.X11-unix** 可显著提高安全性。

## X Server TCP 监听加 xhost 连接管理

此处使用 SDDM 桌面管理器，如果使用其他桌面管理器，请参考相应文档，原理相同。

在 FreeBSD 内编辑或新建 **/usr/local/etc/sddm.conf** 文件，修改 `ServerArguments` 内容如下：

```sh
[X11]
ServerArguments=-listen tcp
```

重启后，在 FreeBSD 上用以下方式加入 Jail IP 以允许访问：

```sh
$ xhost + 192.168.5.1
```

这种方式比 `xhost +` 更安全，仅允许指定 IP 访问。

随后在 Jail 中，执行：

```sh
$ export DISPLAY=:0.0
$ xclock &
```

将 `DISPLAY` 设为 `:0.0`、**127.0.0.1:0.0**、**10.0.2.15:0.0** 均可正常使用，上述几种方法均可尝试。

## VNC

在 Debian Jail 中安装 VNC 服务器，可使用 tightvncserver。以 Debian Jail 为例，在 Jail 中执行：

```sh
# apt install tightvncserver
$ vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
A view-only password is not used
$ vncserver :0
```

vncserver 的端口号为 5900 加上冒号后的数字，此处为 5900，`:1` 端口号为 5901，以此类推。

使用 VNC 客户端登录 Jail：

![VNC 登录](/files/TRe6nz4pW1nW6g5MbtaS)

![VNC 登录](/files/CV0ayL31pj2MTsonjjTP)

![VNC 登录](/files/jF1dTSzquraEtdbt1Jts)

## 附录与补遗

TCP 监听默认关闭的原因之一是宿主机 X Server 通过 TCP 监听配合 xhost 连接管理的方式安全性较差。

共享宿主机 Socket 的方式，需注意仅挂载 **/tmp/.X11-unix** 而非整个 **/tmp**，并使用 `xhost +指定 IP` 而非 `xhost +`；满足这两点即可保障安全性。

带 X Server 的终端、共享主机 Socket、VNC 这三种方式值得推荐。无论采用哪种方式，Linux Jail 中的兼容性均存在一定限制，并非所有 X 应用都能运行；相比之下，FreeBSD Jail 的兼容性则更好。

除共享宿主机 Socket 的方法外，其余方法同样可在非 Jail 环境中使用。

## 参考文献

* FreeBSD Wiki. LinuxApps\[EB/OL]. \[2026-03-25]. <https://wiki.freebsd.org/LinuxApps>. 列出了在 FreeBSD 上运行的 Linux 应用及方法，为兼容性实践提供参考。
* FreeBSD 中文手册. 12.2. 配置 Linux 二进制兼容层\[EB/OL]. \[2026-03-25]. <https://handbook.bsdcn.org/di-12-zhang-linux-er-jin-zhi-jian-rong-ceng/12.2.-pei-zhi-linux-er-jin-zhi-jian-rong-ceng.html>. FreeBSD 中文手册中关于 Linuxulator 配置的详细指南。
* FreeBSD Project. linux -- Linux ABI support\[EB/OL]. \[2026-03-25]. <https://man.freebsd.org/cgi/man.cgi?query=linux&sektion=4>. 官方手册详细说明 Linux 兼容层的系统调用转换机制。
* FreeBSD Wiki. Linuxulator\[EB/OL]. \[2026-03-25]. <https://wiki.freebsd.org/Linuxulator>. 提供 Linuxulator 技术原理与实现细节的权威文档。
* FreeBSD Wiki. LinuxJails\[EB/OL]. \[2026-03-25]. <https://wiki.freebsd.org/LinuxJails>. 介绍如何在 FreeBSD Jail 中部署 Linux 用户空间环境。


---

# 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-31-zhang-linux-jail/di-31.6-jie-linux-jail-zhong-de-gui.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.
