chroot directory command
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.
mkdir /tmp/bin
cp /bin/ls /tmp/bin
chroot /tmp ls
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).
chroot Directory Command > file
For
example, chroot /tmp ls > /bin/file will create
the file in /bin/file. chroot Directory 'Command > file'
For
example, chroot /tmp 'ls > /bin/file' will create
the file in /tmp/bin/file.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
If the SHELL environment variable is set, chroot uses its value to invoke the shell.