更改根目录(chroot)是一种操作,它更改当前运行进程及其子进程的根目录外观。在修改后的环境中运行的程序无法访问该环境目录树之外的文件和命令。19.1 要求
- 另一个可工作的 Linux 环境,例如 Live CD 启动或现有的发行版
- chroot 源和目标的环境架构必须匹配(使用 uname -m 检查当前环境架构)
- 你可能需要在 chroot 环境中使用的内核模块必须已加载(例如,使用 modprobe)
19.2 手动在目录中更改根目录
cd /location/of/new/rootmount -t proc proc proc/mount --rbind /sys sys/mount --rbind /dev dev/mount --rbind /run run/ (可选)
如果你需要在 chroot 环境中使用互联网连接,复制 DNS 详情:cp /etc/resolv.conf etc/resolv.conf
在 /location/of/new/root 中更改 root,指定 shell(本例中为 /bin/bash):chroot /location/of/new/root /bin/bash
chroot 之后,可能需要加载本地 bash 配置:source /etc/profilesource ~/.bashrc
可选,创建一个唯一的提示符以便能够区分你的 chroot 环境:exportPS1="(chroot) $PS1"
cd /umount --recursive /location/of/new/root
19.3 使用 chroot 的原因
更改根目录通常用于在无法启动和/或登录的系统上执行系统维护。