chroot command is used to change the root directory to a given directory. Once you change it to this directory, you can execute a command by using this directory as the root directory.

##
# `chroot` basic usage
##

# `chroot` command is used to change the root directory to a given
# directory. Once you change it to this directory, you can execute a
# command by using this directory as the root directory.

# For example, if you make `/home/jon` as `chroot` directory, and try to
# execute `/bin/mycommand` command, it will really use the `mycommand` from
# `/home/jon/bin` directory.

    $ chroot /home/jon /bin/mycommand

# NOTE: Make sure the `/home/jon/bin/mycommand` is statically linked. If
# not, you should find all other dependencies, and put it in the
# appropriate location under `/home/jon` for the above to work properly.

##
# `chroot` exit status
##

# The following are various chroot exist status:
#
# - 125 chroot failed
# - 126 command found, but can't execute it
# - 127 command not found

    $ chroot /home/jon /bin/tar ; echo $?
    chroot: failed to run command /bin/tar: No such file or directory
    127

    $ chroot /home/jona/bin/tar ; echo $?
    chroot: cannot change root directory to /home/jona: No such file or directory
    125

# > Note: If it works properly, it will return the exit status of the
# > command itself.

##
# Execute command a different user
##

# By default, the following command will execute `/bin/mycommand` as
# the user/group who invokved the `chroot`.

    $ chroot /home/jon /bin/mycommand

# But, if you like it to execute as a different user/group, you should
# specify userspec as shown below.

    $ chroot --userspec=jon:jon /home/jon /bin/mycommand

##
# Specify Additional Groups to Execute Command
##

# Apart from specifying the primary user and group using –userspec, you
# can also pass the secondary groups using –groups command as shown
# below.

    $ chroot --userspec=jon:jon --groups=dba,developer /home/jon /bin/mycommand