How to Use chroot Command in Linux

With the help of chroot command in Linux, you can create a root directory of your own and isolate desired processes from your primary system effortlessly. A great way to build fake root directories for a user or group.

In any UNIX-based system, the root implies the core directory. It is a section responsible for holding files and folders in the drive together. Working on a system loads with an enormous possibility of consequences, and some may even bring serious issues. 

This is when the chroot command clicks in. The chroot command in Linux builds a fake root directory that helps run processes within. The environment created is such that the processes are jailed. It is one of the reasons why chroot is often termed chroot jail.

Uses of chroot Command in Linux

The primary usage of the chroot command in Linux is bringing alterations in the root directory. Now the same practice can prove beneficial in various scenarios like when you’re trying to recover your file system, testing software, running software with incompatible dependencies, and more.

Mounting a damaged file system on a live CD is made possible with chroot command. This will eventually help you use damaged systems and try fixing those. In this manner, Linux’s chroot command allows recovering your file system and even help reinstall the bootloader.

Another situation where the chroot command can help save your day is when two software with clashing dependencies fail to run. You can keep the app and dependencies in a distinct chroot folder and operate the software with no issues whatsoever.

Apart from recovering the file system and running software with incompatible dependencies, chroot command in Linux is of great significance in the testing field. The chroot environment provides a user-friendly context to try scripts without affecting your main system in any manner. 

chroot Command in Linux: Syntax

The basic structure of chroot command in Linux looks something like this:


Options Available for chroot Command

There are usually five options available to run alongside chroot command. These are:

userspec=USER:GROUP: Using this, you can specify the user and the group to work with.

skip-chdir: When you run this option, it will ensure the current working directory is not changed to “/.”

groups=G_LIST: Use this option to specify supplementary groups.

help: It displays help and exits

version: You can use this option to instruct your system to print the version information and exit,

Using chroot Command in Linux

As we’ve already discussed, the chroot command in Linux helps change the apparent root directory of the process currently used.  To explain how Linux’s chroot command works, we’ll need to create a suitable environment and proceed.

Creating a chroot Directory

At first, let’s craft a chroot directory and name it secondary_root. For that, launch the Terminal using the “Ctrl+Alt+T” key combination, and then run the following command:

$ mkdir $HOME/secondary_root
using chroot command in Linux

Creating Other Required Directories within the Chroot Directory

After you create the chroot directory, the next thing that you’ll need to do is build subdirectories inside that. The subdirectories will include bin that contains binaries, alongside lib and lib64 containing libraries that these programs require.

We’ll again use the mkdir command and apply the -p flag.

$ mkdir -p $HOME/secondary_root/{bin,lib,lib64}
adding directory to the chroot environment

After that, use the cd command to make secondary_root the new root directory

$ cd $HOME/secondary_root
making fake root directories in Linux

Copying the Binaries from Root to the Chroot Environment

You must copy all the essential binaries from the root and paste them to the created chroot environment. You can also include text editors if you want to. This will help serve small editing purposes.

$ cp -v /bin/{bash,ls} $HOME/secondary_root/bin/
copying binaries

Resolving Dependencies Stored in Lin and Lib64

Copying just the binaries from the root isn’t enough. You need to ensure that the dependencies stored within lib and lib64 are also copied to the created chroot environment. This is vital for the binaries to work correctly.

$ ldd /bin/{bash,ls}
resolving dependencies within lib and lin64

Copy the Dependencies to the chroot Environment 

Use the cp command to carefully copy the dependencies to our secondary root one by one. We should copy all the dependency libraries; otherwise, our secondary_root will not work properly. 

$ cp -v {<List dependencies here>} $HOME/seconday_root/lib64

Run the command in the following format:

$ cp --parent {/lib/x86_64-linux-gnu/,/lib/x86_64-linux-gnu/,/lib/x86_64-linux-gnu/,/lib64/,/lib/x86_64-linux-gnu/,/lib/x86_64-linux-gnu/,/lib/x86_64-linux-gnu/,} ~/secondary_root/

Execute the chroot Command

After all the binaries and dependencies are successfully copied, the final step is running the chroot command. It is crucial to remember that you’ll need to have sudo privileges for performing this action.

$ sudo chroot $HOME/jail /bin/bash
executing chroot command in Linux

This will create the ready-to-use secondary_root chroot environment. You can now interact with the created directory using bash, similar to how a regular Linux system operates.

Once the chroot environment is activated, you can easily test the same by ensuring if the files can be accessed outside the secondary_root even without shooting up the root privileges.

Checking the Help Information with chroot Command in Linux

If you want your system to display the chroot help information, pass the --help flag.


$ chroot --help


chroot command help

Version Info

Using the --version flag alongside the chroot command will print the version data.


$  chroot --version


checking chroot command version info

And with that, we reach the end of this article. Here we’ve discussed the various uses of chroot command in Linux, alongside providing a step-by-step guide to how actually the command needs to be executed. 

Following the article, you’ll learn how to employ the chroot command and create a fake root directory for dealing with various situations. Stay tuned to discover more about Command-line utilities in UNIX-based systems.

If this guide helped you, please share it.

Leave a Reply
Related Posts