LinuxCommandLibrary

chroot

Change the root directory for a command

TLDR

Run a command with a different root directory
$ sudo chroot [path/to/new_root] [command]
copy
Run an interactive shell in the new root
$ sudo chroot [path/to/new_root]
copy
Specify a custom shell to run
$ sudo chroot [path/to/new_root] /bin/bash
copy
Specify user and group for the command
$ sudo chroot --userspec=[user:group] [path/to/new_root] [command]
copy
Change to a specific directory after chrooting
$ sudo chroot [path/to/new_root] /bin/sh -c "cd /home && ls"
copy

SYNOPSIS

chroot [OPTION] NEWROOT [COMMAND [ARG]...]

DESCRIPTION

chroot changes the root directory of the calling process to the specified path and runs a command within that environment. All pathname lookups starting with / will be relative to the new root.
If no command is specified, chroot runs the shell from the SHELL environment variable or defaults to /bin/sh.
The new root directory must contain all necessary files, libraries, and device nodes required by the command being executed.

PARAMETERS

NEWROOT

The directory to use as the new root filesystem
COMMAND
Command to run in the chroot environment
ARG
Arguments to pass to the command
--userspec=USER:GROUP
Specify user and group (name or ID) to run as
--groups=GROUPS
Specify supplementary groups
--skip-chdir
Do not change working directory to /

CAVEATS

Requires root privileges or CAP_SYS_CHROOT capability. Not designed for security sandboxing—privileged users can escape by creating directories and using relative paths. Open file descriptors may provide access outside the chroot.
The new root must contain essential files: /bin/sh or the specified command, required shared libraries (check with ldd), and device nodes like /dev/null if needed.

HISTORY

The chroot system call originated in Version 7 Unix (1979) at Bell Labs. It was initially used for building and testing new system versions. The GNU coreutils version provides the standard command-line interface on Linux systems.

SEE ALSO

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard