> For the complete documentation index, see [llms.txt](https://handbook.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://handbook.bsdcn.org/di-12-zhang-linux-er-jin-zhi-jian-rong-ceng/12.3.-linux-yong-hu-kong-jian.md).

# 12.3.Linux 用户空间

Linux 软件的运行不仅需要 ABI，还需要安装 Linux 用户空间环境。

> **技巧**
>
> 如果只是想运行 Ports 树中已有的软件，可以通过包管理器安装，[pkg(8)](https://man.freebsd.org/cgi/man.cgi?query=pkg\&sektion=8\&format=html) 会自动设置所需的 Linux 用户空间。
>
> 例如，要安装 Sublime Text 4 及其依赖的所有 Linux 库，可执行以下命令：
>
> ```sh
> # pkg install linux-sublime-text4
> ```

## 12.3.1. 来自 FreeBSD 软件包的 Rocky Linux 基本系统

要安装 Rocky Linux 9 用户空间，可执行以下命令：

```sh
# pkg install linux_base-rl9
```

[emulators/linux\_base-rl9](https://cgit.freebsd.org/ports/tree/emulators/linux_base-rl9/) 会将源自 Rocky Linux 9 的基本系统放置在 **/compat/linux** 目录中。

安装该软件包后，可通过以下命令确认 Rocky Linux 用户空间是否已安装到 **/compat/linux** 目录：

```sh
# ls -l /compat/linux/
```

输出应类似于：

```sh
total 36
drwxr-xr-x   2 root wheel  512 Oct  9 17:28 afs
lrwxr-xr-x   1 root wheel    7 May 16  2022 bin -> usr/bin
drwxr-xr-x   3 root wheel  512 Oct  9 17:28 dev
drwxr-xr-x  24 root wheel 1536 Oct  9 17:28 etc
lrwxr-xr-x   1 root wheel    7 May 16  2022 lib -> usr/lib
lrwxr-xr-x   1 root wheel    9 May 16  2022 lib64 -> usr/lib64
drwxr-xr-x   2 root wheel  512 Oct  9 17:28 opt
drwxr-xr-x   2 root wheel  512 Oct  9 17:28 proc
lrwxr-xr-x   1 root wheel    8 Oct  1 03:11 run -> /var/run
lrwxr-xr-x   1 root wheel    8 May 16  2022 sbin -> usr/sbin
drwxr-xr-x   2 root wheel  512 Oct  9 17:28 srv
drwxr-xr-x   2 root wheel  512 Oct  9 17:28 sys
drwxr-xr-x   8 root wheel  512 Oct  9 17:28 usr
drwxr-xr-x  16 root wheel  512 Oct  9 17:28 var
```

## 12.3.2. 来自 FreeBSD 软件包的 CentOS 基本系统

> **警告**
>
> 由于上游项目已停止维护，[emulators/linux\_base-c7](https://cgit.freebsd.org/ports/tree/emulators/linux_base-c7/) 也已弃用，因此将不再收到安全更新。除非需要 32 位兼容性，否则建议使用 [Rocky Linux 基本系统](/di-12-zhang-linux-er-jin-zhi-jian-rong-ceng/12.3.-linux-yong-hu-kong-jian.md)。

要安装 CentOS 用户空间，可执行以下命令：

```sh
# pkg install linux_base-c7
```

[emulators/linux\_base-c7](https://cgit.freebsd.org/ports/tree/emulators/linux_base-c7/) 会将源自 CentOS 7 的基本系统放置在 **/compat/linux** 中。

安装完成后，可通过以下命令验证 CentOS 用户空间是否已安装：

```sh
# ls -l /compat/linux/
```

输出应类似于：

```sh
total 30
lrwxr-xr-x   1 root  wheel    7 Apr 11  2018 bin -> usr/bin
drwxr-xr-x  13 root  wheel  512 Apr 11 21:10 dev
drwxr-xr-x  25 root  wheel   64 Apr 11 21:10 etc
lrwxr-xr-x   1 root  wheel    7 Apr 11  2018 lib -> usr/lib
lrwxr-xr-x   1 root  wheel    9 Apr 11  2018 lib64 -> usr/lib64
drwxr-xr-x   2 root  wheel    2 Apr 11 21:10 opt
dr-xr-xr-x   1 root  wheel    0 Apr 11 21:25 proc
lrwxr-xr-x   1 root  wheel    8 Feb 18 02:10 run -> /var/run
lrwxr-xr-x   1 root  wheel    8 Apr 11  2018 sbin -> usr/sbin
drwxr-xr-x   2 root  wheel    2 Apr 11 21:10 srv
dr-xr-xr-x   1 root  wheel    0 Apr 11 21:25 sys
drwxr-xr-x   8 root  wheel    9 Apr 11 21:10 usr
drwxr-xr-x  16 root  wheel   17 Apr 11 21:10 var
```

## 12.3.3. 使用 debootstrap 的 Debian / Ubuntu 基本系统

另一种提供 Linux 共享库的方式是使用 [sysutils/debootstrap](https://cgit.freebsd.org/ports/tree/sysutils/debootstrap/)，它的优点是可以提供完整的 Debian 或 Ubuntu 发行版。

安装 debootstrap 的命令如下：

```sh
# pkg install debootstrap
```

[debootstrap(8)](https://man.freebsd.org/cgi/man.cgi?query=debootstrap\&sektion=8\&format=html) 需要启用 [linux(4)](https://man.freebsd.org/cgi/man.cgi?query=linux\&sektion=4\&format=html) ABI。启用后，可使用以下命令在 **/compat/ubuntu** 中安装 Ubuntu 或 Debian：

```sh
# debootstrap jammy /compat/ubuntu
```

> **注意**
>
> 虽然技术上可以安装到 **/compat/linux**，但由于可能与基于 CentOS 的软件包发生冲突，不建议这么做。应使用基于发行版或版本名的路径，例如 **/compat/ubuntu**。

输出应类似于：

```sh
I: Retrieving InRelease
I: Checking Release signature
I: Valid Release signature (key id F6ECB3762474EDA9D21B7022871920D1991BC93C)
I: Retrieving Packages
I: Validating Packages
I: Resolving dependencies of required packages...
I: Resolving dependencies of base packages...
I: Checking component main on http://archive.ubuntu.com/ubuntu...
[...]
I: Configuring console-setup...
I: Configuring kbd...
I: Configuring ubuntu-minimal...
I: Configuring libc-bin...
I: Configuring ca-certificates...
I: Base system installed successfully.
```

然后在 **/etc/fstab** 中设置挂载点。

> **技巧**
>
> 如果希望共享 home 目录的内容并运行 X11 应用程序，应使用 [nullfs(5)](https://man.freebsd.org/cgi/man.cgi?query=nullfs\&sektion=5\&format=html) 以回环方式挂载，将 **/home** 和 **/tmp** 挂载到 Linux 兼容区中。
>
> 可以将以下示例添加到 **/etc/fstab**：
>
> ```ini
> # Device        Mountpoint              FStype          Options                      Dump    Pass#
> devfs           /compat/ubuntu/dev      devfs           rw,late                      0       0
> tmpfs           /compat/ubuntu/dev/shm  tmpfs           rw,late,size=1g,mode=1777    0       0
> fdescfs         /compat/ubuntu/dev/fd   fdescfs         rw,late,linrdlnk             0       0
> linprocfs       /compat/ubuntu/proc     linprocfs       rw,late                      0       0
> linsysfs        /compat/ubuntu/sys      linsysfs        rw,late                      0       0
> /tmp            /compat/ubuntu/tmp      nullfs          rw,late                      0       0
> /home           /compat/ubuntu/home     nullfs          rw,late                      0       0
> ```
>
> 然后执行 [mount(8)](https://man.freebsd.org/cgi/man.cgi?query=mount\&sektion=8\&format=html)：
>
> ```sh
> # mount -al
> ```

要使用 [chroot(8)](https://man.freebsd.org/cgi/man.cgi?query=chroot\&sektion=8\&format=html) 进入系统，可执行：

```sh
# chroot /compat/ubuntu /bin/bash
```

然后可通过 [uname(1)](https://man.freebsd.org/cgi/man.cgi?query=uname\&sektion=1\&format=html) 检查当前 Linux 环境：

```sh
# uname -s -r -m
```

输出应类似于：

```sh
Linux 5.15.0 x86_64
```

进入 chroot 后，系统行为与普通 Ubuntu 安装基本一致。尽管 systemd 不可用，但 [service(8)](https://man.freebsd.org/cgi/man.cgi?query=service\&sektion=8\&format=html) 命令仍可照常使用。

> **技巧**
>
> 要添加默认缺失的软件包存储库，请编辑文件 **/compat/ubuntu/etc/apt/sources.list**。
>
> 对于 amd64，可以使用以下示例：
>
> ```ini
> deb http://archive.ubuntu.com/ubuntu jammy main universe restricted multiverse
> deb http://security.ubuntu.com/ubuntu/ jammy-security universe multiverse restricted main
> deb http://archive.ubuntu.com/ubuntu jammy-backports universe multiverse restricted main
> deb http://archive.ubuntu.com/ubuntu jammy-updates universe multiverse restricted main
> ```
>
> arm64 可使用以下示例：
>
> ```ini
> deb http://ports.ubuntu.com/ubuntu-ports bionic main universe restricted multiverse
> ```
