# chroot

**chroot**（即“更改根目录”）是 Unix 及类 Unix 操作系统上的一项操作，它把调用进程的[根目录](https://www.edgepedia.org/zh/root-directory)改为管理员选定的某个目录，于是以 `/` 开头的路径名都在该目录内部解析，而不再指向真正的文件系统根目录。它既以 `chroot()` 系统调用的形式存在——最早于 1979 年出现在第七版 AT&T UNIX 中——也以 GNU coreutils 中 `chroot` 用户命令的形式存在，后者以新的根目录运行一条命令。<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup><sup> • </sup><sup>[2](https://man.openbsd.net/chroot.2)</sup><sup> • </sup><sup>[3](https://man7.org/linux/man-pages/man1/chroot.1.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 作用 | `chroot()` 把调用进程的根目录改为 `path` 给出的目录；以 `/` 开头的路径名即以该目录为起点解析，且调用进程的所有子进程都继承这一根目录<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup> |
| 所需权限 | 只有特权进程才能调用；在 Linux 上这指的是调用者用户命名空间中的 `CAP_SYS_CHROOT` 能力，在 OpenBSD 上调用者必须是超级用户<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup><sup> • </sup><sup>[2](https://man.openbsd.net/chroot.2)</sup> |
| 安全地位 | Linux 手册明确说明，该调用只改变路径名解析，不涉及其他任何东西，也不是为任何安全目的而设，既不用于把进程关进沙箱，也不用于限制文件系统相关的系统调用<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup> |
| 已知的逃逸手法 | 超级用户可以用 `mkdir foo; chroot foo; cd ..` 逃出“chroot 监狱”，因为该调用并不改变当前工作目录，而工作目录可能仍留在新根目录之外<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup> |
| 历史 | 最早出现于第七版 AT&T UNIX（1979 年），1982 年被加入 BSD；在 Linux 上，其有据可查的谱系为 SVr4、4.4BSD、SUSv1（标记为 TO BE WITHDRAWN）和 SUSv2（标记为 LEGACY），它不属于 POSIX.1-2001<sup>[2](https://man.openbsd.net/chroot.2)</sup><sup> • </sup><sup>[4](https://www.linuxfoundation.org/blog/blog/a-brief-look-at-the-roots-of-linux-containers)</sup><sup> • </sup><sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup> |
| 更强的后继者 | FreeBSD 增加了 `jail()` 系统调用，它还把用户和网络子系统虚拟化；在 Linux 上，`pivot_root()` 更换根文件系统本身，并与挂载命名空间、用户命名空间结合，提供文件系统层面的保护<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup><sup> • </sup><sup>[5](https://docs-archive.freebsd.org/doc/7.3-RELEASE/usr/share/doc/handbook/jails-intro.html)</sup><sup> • </sup><sup>[6](https://www.govinfo.gov/content/pkg/GOVPUB-C13-af6f1d2da268173ea5ea702b719bc874/pdf/GOVPUB-C13-af6f1d2da268173ea5ea702b719bc874.pdf)</sup> |
| 当今常见用途 | 用 `debootstrap`、`schroot` 和 `sbuild` 构建并进入隔离的 Debian 或 Ubuntu 环境，用于软件包开发与测试<sup>[7](https://wiki.debian.org/RichardDarst/Schroot)</sup> |

## 工作原理

`chroot()` 系统调用只接受一个参数，即某个目录的路径名。调用成功后，该目录就成为调用进程中一切以 `/` 开头的路径名的解析起点。根目录会被 `fork()` 创建的所有子进程继承，且 `execve()` 不会改动它，因此一个守护进程可以一次性确立新的根目录，让它整棵进程树都在其中运行。<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup>

该调用只影响路径名解析的一个环节，不涉及其他任何东西。进程从父进程继承根目录，内核在做路径名查找时，以这个按进程而设的根目录作为绝对路径的起点；带 `RESOLVE_IN_ROOT` 标志的 `openat2()` 系统调用则提供了一种与之相关的、仅对单次调用生效的临时限制。<sup>[8](https://man7.org/linux/man-pages/man7/path_resolution.7.html)</sup> 魔术符号链接 `/proc/pid/root` 可让观察者查明任意进程的根目录。<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup>

在 Linux 上，该调用不改变当前工作目录，因此调用之后，进程的 `.` 可能落在以新 `/` 为根的那棵树之外。OpenBSD 的行为则不同：如果程序尚未 chroot，`chroot()` 对当前目录没有影响；但如果程序已在更改过的根目录下运行，则当前目录会被改为新的根目录，以免它停留在比更改后的根目录更高的树层级上。<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup><sup> • </sup><sup>[2](https://man.openbsd.net/chroot.2)</sup>

## 为何它不是安全边界

Linux 手册页明确指出，`chroot()` 不是为任何形式的安全目的而设。由于该调用对若干方面不作处理，随之产生了以下具体弱点：<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup>

- **工作目录。** 由于当前工作目录不变，特权进程可以在监狱内建一个目录，chroot 进去，再用 `cd ..` 向上走，此时便落在了原监狱之外。<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup>
- **已打开的文件描述符。** 该调用不关闭已打开的[文件描述符](https://www.edgepedia.org/zh/file-descriptor)，而这些描述符可能让人访问 chroot 树之外的文件。<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup>
- **来自外部的改动。** OpenBSD 手册指出，从监狱外部对目录层级所作的改动可能让受限制的进程——甚至是非特权进程——逃逸；此外，从外部通过 `recvmsg()` 传来的目录文件描述符也可用于逃逸。<sup>[2](https://man.openbsd.net/chroot.2)</sup>
- **权限本身。** 在 Linux 上，持有 `CAP_SYS_CHROOT` 的进程可以再次调用 `chroot()`；美国国家标准与技术研究院（NIST）关于容器安全的指引指出，特权进程之所以能逃出 chroot 监狱，正因为该调用只影响路径名解析。<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup><sup> • </sup><sup>[6](https://www.govinfo.gov/content/pkg/GOVPUB-C13-af6f1d2da268173ea5ea702b719bc874/pdf/GOVPUB-C13-af6f1d2da268173ea5ea702b719bc874.pdf)</sup>

历史上，守护进程确实曾用 `chroot()` 先限制自身，再把不受信任的用户提供的路径传给 `open()` 之类的系统调用；《FreeBSD 手册》记载，多年来人们找到了许多逃出 chroot 环境的方法，这清楚表明 `chroot(2)` 并非保护服务的理想方案。<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup><sup> • </sup><sup>[5](https://docs-archive.freebsd.org/doc/7.3-RELEASE/usr/share/doc/handbook/jails-intro.html)</sup>

## 后继者与更强的隔离

chroot 的局限塑造了后来的隔离机制。2000 年，FreeBSD 把这一模型扩展为 **FreeBSD 监狱**（FreeBSD Jails），由 FreeBSD 开发者波尔-亨宁·坎普（Poul-Henning Kamp）在 FreeBSD 4.X 中引入。传统的 chroot 只限制进程能访问文件系统中的哪一部分，而系统用户集合、运行中的进程以及网络子系统仍与主机共享。监狱则增加了主机名、IP 地址和自己的一套用户，并且监狱的 `root` 用户从主机的角度看并非无所不能的用户。<sup>[4](https://www.linuxfoundation.org/blog/blog/a-brief-look-at-the-roots-of-linux-containers)</sup><sup> • </sup><sup>[5](https://docs-archive.freebsd.org/doc/7.3-RELEASE/usr/share/doc/handbook/jails-intro.html)</sup>

在 Linux 上，NIST 关于容器安全的出版物描述了这种分层替代方案：`pivot_root()` 移动旧根文件系统的挂载点，并把新根文件系统放到它的位置，于是主机根目录可以被卸载并变得不可访问；挂载命名空间隔离各个挂载点；用户命名空间把用户和组 ID 映射到一个权限较低的范围。完整的文件系统层面保护需要结合挂载命名空间、chroot、`pivot_root` 和用户命名空间配置。<sup>[6](https://www.govinfo.gov/content/pkg/GOVPUB-C13-af6f1d2da268173ea5ea702b719bc874/pdf/GOVPUB-C13-af6f1d2da268173ea5ea702b719bc874.pdf)</sup> systemd 服务管理器通过其 `RootDirectory=` 设置对外提供这一能力，该设置使用 `pivot_root(2)` 或 `chroot(2)` 为所执行的进程设定根目录，并建议同时配合 `MountAPIVFS=` 和 `PrivateUsers=` 使用。<sup>[9](https://man7.org/linux/man-pages/man5/systemd.exec.5.html)</sup>

## 实际用途

chroot 最常见的日常用途，是针对与主机不同的用户空间来构建和测试软件。在 Debian 和 Ubuntu 上，工作流程如下：<sup>[7](https://wiki.debian.org/RichardDarst/Schroot)</sup><sup> • </sup><sup>[10](https://sources.debian.org/src/ubuntu-packaging-guide/1.0.2/ubuntu-packaging-guide/chroots.rst)</sup>

1. 运行 `debootstrap`，把一套最小基础系统安装到目标目录，例如 `sudo debootstrap sid /srv/chroot/debian-sid`，这样该目录中就会留下一套完整的 Debian 系统。<sup>[7](https://wiki.debian.org/RichardDarst/Schroot)</sup>
2. 用 `chroot` 进入它，或更方便地用 `schroot`——一个用于管理 chroot 环境并在其中执行命令的工具；而 `sbuild` 这个从源码构建 Debian 二进制包的工具，使用的正是由 `schroot` 管理的 chroot。<sup>[7](https://wiki.debian.org/RichardDarst/Schroot)</sup><sup> • </sup><sup>[11](https://www.debian.org/doc/manuals/debian-reference/ch09.en.html)</sup>
3. 当程序需要时，把 `/tmp`、`/dev` 或 `/proc` 之类的主机目录绑定挂载进这棵树，因为 chroot 客户机与主机共享内核，但拥有完全不同的根文件系统。<sup>[10](https://sources.debian.org/src/ubuntu-packaging-guide/1.0.2/ubuntu-packaging-guide/chroots.rst)</sup><sup> • </sup><sup>[7](https://wiki.debian.org/RichardDarst/Schroot)</sup>

由于客户机与主机共享内核，chroot 若试图如此，也可能影响到自身之外的东西；Debian 的文档称其隔离不如虚拟化彻底，但在大多数非恶意用途下，它是一种简便而高效的测试手段。<sup>[7](https://wiki.debian.org/RichardDarst/Schroot)</sup>

`chroot` 命令本身属于 GNU coreutils。其用法概要为 `chroot [OPTION]... NEWROOT [COMMAND [ARG]...]`；选项包括用于以其他用户身份运行命令的 `--userspec=USER:GROUP` 和 `--groups=G_LIST`，以及用于保持工作目录不变的 `--skip-chdir`。如果未给出命令，它将运行 `"$SHELL" -i`，默认是 `/bin/sh -i`；如果 chroot 命令本身失败，则以状态码 125 退出。<sup>[3](https://man7.org/linux/man-pages/man1/chroot.1.html)</sup>

## 标准地位

尽管历史悠久，`chroot()` 在正式标准中的地位却颇为特殊：它出现在 SVr4 和 4.4BSD 中，在 SUSv1 中被标记为 TO BE WITHDRAWN，在 SUSv2 中被标记为 LEGACY，并且不属于 POSIX.1-2001。Linux 手册页把它的标准地位列为“无”。<sup>[1](https://man7.org/linux/man-pages/man2/chroot.2.html)</sup>

## References

1. [chroot(2) — Linux manual page, man-pages project](https://man7.org/linux/man-pages/man2/chroot.2.html)
2. [chroot(2) — OpenBSD manual pages](https://man.openbsd.net/chroot.2)
3. [chroot(1) — Linux manual page, GNU coreutils](https://man7.org/linux/man-pages/man1/chroot.1.html)
4. [A Brief Look at the Roots of Linux Containers, The Linux Foundation (2017)](https://www.linuxfoundation.org/blog/blog/a-brief-look-at-the-roots-of-linux-containers)
5. [Jails: What is a Jail, FreeBSD Handbook (7.3-RELEASE archive)](https://docs-archive.freebsd.org/doc/7.3-RELEASE/usr/share/doc/handbook/jails-intro.html)
6. [Security Assurance Requirements for Linux Application Container Deployments, NIST IR 8176](https://www.govinfo.gov/content/pkg/GOVPUB-C13-af6f1d2da268173ea5ea702b719bc874/pdf/GOVPUB-C13-af6f1d2da268173ea5ea702b719bc874.pdf)
7. [RichardDarst/Schroot, Debian Wiki](https://wiki.debian.org/RichardDarst/Schroot)
8. [path_resolution(7) — Linux manual page](https://man7.org/linux/man-pages/man7/path_resolution.7.html)
9. [systemd.exec(5) — Linux manual page](https://man7.org/linux/man-pages/man5/systemd.exec.5.html)
10. [File: chroots.rst, Ubuntu Packaging Guide, Debian Sources](https://sources.debian.org/src/ubuntu-packaging-guide/1.0.2/ubuntu-packaging-guide/chroots.rst)
11. [Chapter 9. System tips, Debian Reference](https://www.debian.org/doc/manuals/debian-reference/ch09.en.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Unix-like and Linux › Unix, BSD, and kernel internals

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/chroot

English original: https://www.edgepedia.org/chroot
