chroot — Change the root directory for the execution of a command

Format

chroot directory command

Description

If you have appropriate privileges, the chroot command changes the root directory to the directory specified by the directory parameter of a specific command. The new root directory will also contain its children.

Rule: In order to use chroot, you must either be a superuser (UID=0), or have READ permission to the BPX.SUPERUSER resource profile in the FACILITY class.

The directory path name is always relative to the current root. If a nested chroot command is in effect, the directory path name is still relative to the current (new) root of the running process.

In order for your process to operate properly after the chroot is issued, you need to have in your new root all the files that your program depends on. For example, if your new root is /tmp and you issue an ls, you will get a not found error. To use ls with /tmp as your new root, you will need a /tmp/bin with ls in it before you issue the chroot command.

In addition, utilities that depend on locale-sensitive files (/usr/lib/nis/*) may be unsuccessful if these files are not in the new root file system.

After chroot is issued, your current working directory is the new root (directory), chroot does not change environment variables.

directory
Specifies the new root directory
command
Specifies a command to run with the chroot command

Examples

  1. To run the ls command with the /tmp directory as the root file system, enter:
    mkdir /tmp/bin
    cp /bin/ls /tmp/bin
    chroot /tmp ls
  2. To run a child shell with another file system as the root file system (assuming that /tmp is the mount point of a file system), enter:
    mkdir /tmp/bin
    cp /bin/sh /tmp/bin
    chroot /tmp sh       or      chroot /tmp /bin/sh
    This makes the directory name / (slash) refer to the /tmp for the duration of the /bin/sh command. It also makes the original root file system inaccessible. The file system on the /tmp file must contain the standard directories of a root file system.

    Running the sh command creates a child shell that runs as a separate process from your original shell. Press the END OF FILE (Crtl-D) key sequence or type exit to end the child shell and go back to where you were in the original shell. This restores the environment of the original shell, including the meanings of the . (current directory) and the / (root directory).

  3. To create a file relative to the original root, not the new one, enter:
    chroot Directory Command > file
    For example, chroot /tmp ls > /bin/file will create the file in /bin/file.
    Note: Redirection is handled by the current shell before chroot is executed.
  4. To create a file relative to the new root, enter:
    chroot Directory 'Command > file'
    For example, chroot /tmp 'ls > /bin/file' will create the file in /tmp/bin/file.
  5. Examples of how the current root changes:
    Given the standard directories of the file system plus:
    
    # echo $PATH
    /bin
    # ls /tmp/bin
    bin file2 sh
    # ls /tmp/bin/bin
    file1 sh
    
    # whence file2
    #
    # whence file1
    #
    
    # chroot /tmp 'whence file1'
    #
    # chroot /tmp 'type file2'
    /bin/file2
    
    # chroot /tmp/bin 'type file1'
    /bin/file1

Exit values

0
The command completed successfully
1
Failure due to any of the following:
  • chroot seteuid failed
  • User not authorized to issue chroot
2
Failure due to any of the following:
  • Cannot chdir to directory specified
  • chroot cannot change root
  • Unable to execute the shell
  • Incorrect command syntax

If the SHELL environment variable is set, chroot uses its value to invoke the shell.