A chroot gives maintenance and build tools a separate root filesystem while sharing the host kernel. DNF can build an RPM-based chroot with a working userland that you can verify before safely detaching the host filesystems after use.

The DNF package manager can populate an empty directory with the host distribution's Core package group before the environment exists. DNF5 on current Fedora releases requires host repository configuration to be selected explicitly for an empty install root, while DNF4 on RHEL and CentOS Stream falls back to the host repository files. The host needs working repositories for its own release, including authorized content access on RHEL.

The host /dev and /sys trees are exposed inside the chroot, so only trusted root processes should enter it; a chroot is not a security boundary. Recursive slave propagation keeps mount and unmount events created below the chroot from propagating back to the corresponding host mount. Removing the directory while host filesystems remain mounted can delete host device entries or affect the host through /sys.

Steps to build a chroot environment on RHEL, CentOS Stream, and Fedora:

  1. Create the chroot root at /var/lib/chroot/fedora.
    $ sudo mkdir -p /var/lib/chroot/fedora
  2. Populate the chroot with the Core group for the host release.
    $ sudo dnf --installroot=/var/lib/chroot/fedora --use-host-config --assumeyes install @core

    On RHEL and CentOS Stream, which use DNF4, replace the DNF5 option --use-host-config with --releasever=/.

  3. Bind the host /dev tree recursively into the chroot with slave propagation.
    $ sudo mount --rbind --make-rslave /dev /var/lib/chroot/fedora/dev
  4. Mount a fresh proc filesystem inside the chroot.
    $ sudo mount --types proc proc /var/lib/chroot/fedora/proc
  5. Bind the host /sys tree recursively into the chroot with slave propagation.
    $ sudo mount --rbind --make-rslave /sys /var/lib/chroot/fedora/sys
  6. Replace the chroot resolver file with the host's current DNS configuration.
    $ sudo cp --remove-destination /etc/resolv.conf /var/lib/chroot/fedora/etc/resolv.conf
  7. Enter the chroot with Bash.
    $ sudo chroot /var/lib/chroot/fedora /bin/bash
  8. Refresh repository metadata from inside the chroot.
    # dnf --refresh makecache
    Updating and loading repositories:
     Fedora 45 - aarch64 - Updates          100% |  18.2 KiB/s |   5.9 KiB |  00m00s
     Fedora 45 openh264 (From Cisco) - aarc 100% |   3.2 KiB/s | 987.0   B |  00m00s
     Fedora 45 - aarch64                    100% |  10.4 KiB/s |   3.1 KiB |  00m00s
    Repositories loaded.
    Metadata cache created.

    A metadata error means the chroot cannot reach its repositories. On RHEL, repository access inside the chroot needs its own valid configuration because --installroot does not copy the host's subscription credentials.

  9. Exit the chroot shell.
    # exit
  10. Unmount the chroot /dev, /proc, and /sys trees recursively.
    $ sudo umount --recursive /var/lib/chroot/fedora/{dev,proc,sys}

    A busy-mount error means a process, often another shell, still holds the filesystem open. The directory is not safe to delete until this unmount succeeds.

  11. Confirm that nothing is still mounted in the chroot.
    $ mount | grep /var/lib/chroot/fedora

    No output means the host filesystems are detached and the directory is safe to keep or delete. Entering it again needs the same mounts.

Share this guide