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.
Steps to set up a chroot environment for RHEL, CentOS, or Fedora:
- Create a directory to serve as the root for the new environment.
$ sudo mkdir /mnt/chroot
- Install a minimal system into the new directory.
$ 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 ...
- Mount the /proc filesystem for the chroot environment.
$ sudo mount -t proc /proc /mnt/chroot/proc
- Mount the /sys filesystem.
$ sudo mount --rbind /sys /mnt/chroot/sys
- Mount the /dev filesystem.
$ sudo mount --rbind /dev /mnt/chroot/dev
- Bind /dev/pts for pseudo terminals.
$ sudo mount --bind /dev/pts /mnt/chroot/dev/pts
- Copy DNS information for internet access inside the chroot.
$ sudo cp -L /etc/resolv.conf /mnt/chroot/etc/
- Enter the chroot environment.
$ 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.
- Install necessary software inside the chroot (optional).
$ dnf install gcc make
- Exit the chroot environment.
$ exit
- Unmount the /dev filesystem.
$ sudo umount -l /mnt/chroot/dev{/shm,/pts,}
- Unmount the /proc filesystem.
$ sudo umount -l /mnt/chroot/proc
- Unmount the /sys filesystem.
$ sudo umount -l /mnt/chroot/sys
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.