Change root (chroot)
Change root (chroot) is an operation that changes the apparent root directory for the current running process and their children. A program that is run in such a modified environment cannot access files and commands outside that environmental directory tree.
Manually changing root in a directory
Section titled “Manually changing root in a directory”cd /location/of/new/rootmount -t proc proc proc/mount --rbind /sys sys/mount --rbind /dev dev/mount --rbind /run run/ (optionally)cp /etc/resolv.conf etc/resolv.confchroot /location/of/new/root /bin/bashsource /etc/profilesource ~/.bashrcexport PS1="(chroot) $PS1"exitcd /umount --recursive /location/of/new/rootRequirements
Section titled “Requirements”- root privileges
- another working Linux environment,such as Live CD boot or an existing distribution
- matching environment architectures of
chrootsource and destination (check current environment architecture withuname -m) - kernel modules which you may need in
chrootenvironment must be loaded (for example, withmodprobe)
Reasons to use chroot
Section titled “Reasons to use chroot”Changing root is commonly done for performing system maintenance on systems where booting and/or logging in is no longer possible.
Common examples are:
- reinstalling the bootloader
- rebuilding the initramfs image
- upgrading or downgrading packages
- resetting a forgotten password
- building software in a clean root environment
Syntax
Section titled “Syntax”- chroot [destination path] [shell or command]