Setting up a chroot environment on RHEL (Red Hat Enterprise Linux), CentOS, or Fedora allows you to work within an isolated system environment without affecting your host OS. This is useful for tasks like system recovery, testing configurations, compiling software, or experimenting with different installations. The chroot command changes the root directory for a process, creating a controlled environment that isolates operations from the rest of the system.
The process is nearly identical for RHEL, CentOS, and Fedora, as all three systems use dnf for package management. The steps involve installing a minimal system, mounting necessary filesystems like /proc, /sys, and /dev, and then entering the chroot environment for further operations.
In this guide, we will outline the steps to create a chroot environment that applies to RHEL, CentOS, and Fedora. For Fedora, ensure you use the correct release version in the dnf commands.
$ sudo mkdir /mnt/chroot
$ sudo dnf --installroot=/mnt/chroot --releasever=8 groupinstall "Minimal Install" # RHEL and CentOS $ sudo dnf --installroot=/mnt/chroot --releasever=38 groupinstall "Minimal Install" # Fedora Installed: NetworkManager.x86_64 1.26.0-17.el8 chrony.x86_64 3.5-2.el8 systemd.x86_64 239-51.el8 ...
$ sudo mount -t proc /proc /mnt/chroot/proc
$ sudo mount --rbind /sys /mnt/chroot/sys
$ sudo mount --rbind /dev /mnt/chroot/dev
$ sudo mount --bind /dev/pts /mnt/chroot/dev/pts
$ sudo cp -L /etc/resolv.conf /mnt/chroot/etc/
$ sudo chroot /mnt/chroot /bin/bash source /etc/profile export PS1="(chroot) $PS1"
Once inside, you are operating in the chroot environment for your chosen distribution. You can perform operations in isolation.
$ dnf install gcc make
$ exit
$ sudo umount -l /mnt/chroot/dev{/shm,/pts,}
$ sudo umount -l /mnt/chroot/proc
$ sudo umount -l /mnt/chroot/sys