Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

chroot

Run a command (default: a shell) with another folder as the root / — the key step to repair an installed system from a rescue shell.

Level
RHCSA exam
Group
System information
Needs root
Yes
Package
coreutils

What it does

After chroot /mnt/sysroot, commands see /mnt/sysroot as /: they use the installed system's programs, config and package database. That is how you reset a password with rd.break, reinstall GRUB or rebuild the initramfs from a rescue disk. For tools like grub2-install or dracut, /proc, /sys, /dev and /run should be mounted inside first (the RHEL rescue mode does this for you).

chroot is not a security boundary; use containers for isolation.

How to write it

chroot [OPTION]... NEWROOT [COMMAND [ARGUMENT]...]

Options 6

Most used

NEWROOT
The folder that becomes the new /.
--userspec=USER:GROUP
Run as this user and group.
--groups=G_LIST
Extra groups.
--skip-chdir
Do not change the working folder to /.
--help
Show help.
--version
Show the version.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Enter the installed system from rd.break

    mount -o remount,rw /sysroot
    chroot /sysroot

    Now passwd works on the real /etc/shadow.

  2. 2From the rescue disk

    chroot /mnt/sysroot

    The rescue mode mounted the system there.

  3. 3Mount the special file systems by hand

    for d in proc sys dev run; do mount --rbind /$d /mnt/sysroot/$d; done
    chroot /mnt/sysroot

    Needed when you mounted the root yourself.

  4. 4Run one command

    chroot /mnt/sysroot dracut -f --regenerate-all

    Without an interactive shell.

  5. 5Reinstall GRUB from rescue

    chroot /mnt/sysroot grub2-install /dev/vda

    BIOS systems.

  6. 6Check packages of the installed system

    chroot /mnt/sysroot rpm -qa | grep kernel

    The installed kernels, not those of the ISO.

  7. 7As another user

    chroot --userspec=lisa:lisa /mnt/sysroot /bin/bash

    Test user rights inside.

  8. 8Leave the chroot

    exit

    Back to the rescue shell.

  9. 9Is my shell in a chroot?

    ls -di /
    2 /

    An inode number other than 2 (or 128 on btrfs) often means a chroot.

  10. 10Do not use it for security

    podman run --rm -it registry.access.redhat.com/ubi10/ubi bash

    Containers isolate with namespaces and SELinux; chroot does not.

Exit codes

125 :: chroot itself failed. 126 :: The command could not be run. 127 :: The command was not found. 0 :: Otherwise the exit status of the command.