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:

  1. Create a directory to serve as the root for the new environment.
    $ sudo mkdir /mnt/chroot
  2. 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  
    ...
  3. Mount the /proc filesystem for the chroot environment.
    $ sudo mount -t proc /proc /mnt/chroot/proc
  4. Mount the /sys filesystem.
    $ sudo mount --rbind /sys /mnt/chroot/sys
  5. Mount the /dev filesystem.
    $ sudo mount --rbind /dev /mnt/chroot/dev
  6. Bind /dev/pts for pseudo terminals.
    $ sudo mount --bind /dev/pts /mnt/chroot/dev/pts
  7. Copy DNS information for internet access inside the chroot.
    $ sudo cp -L /etc/resolv.conf /mnt/chroot/etc/
  8. 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.

  9. Install necessary software inside the chroot (optional).
    $ dnf install gcc make
  10. Exit the chroot environment.
    $ exit
  11. Unmount the /dev filesystem.
    $ sudo umount -l /mnt/chroot/dev{/shm,/pts,}
  12. Unmount the /proc filesystem.
    $ sudo umount -l /mnt/chroot/proc
  13. Unmount the /sys filesystem.
    $ sudo umount -l /mnt/chroot/sys
Discuss the article:

Comment anonymously. Login not required.