Before changing into a different directory using chroot, the target directory must be mounted using the mount command. Mounting the directory is possible even after navigating to it. But it’s recommended to do so before using the chroot command. The modified directory is called the chroot jail or jailed directory.
The basic syntax of using the chroot command is:
Key Uses of the chroot Linux Command:
There are 3 uses for the command:
- To recover forgotten Linux password
- To reinstall the boot loader
- To create a test environment
chroot Command Options
1. –help: used when opening up the help page
2. -groups=G_LIST: used when specifying supplementary groups.
3. userspec=USER:GROUP: used when specifying the group name or ID.
4. –version: used when getting the chroot version
To use the chroot command, begin by creating the directory to navigate.
Here, test-jail is our new directory.
Next, create other directories, bin and lib64, inside the test-jail directory. For this, use the command below:
With the directories created, you can now navigate the directory without using chroot.
For our example, we will add the ls and bin commands into the created chroot jail using the commands below:
Once you copy the files, you must add the dependencies for the binaries. To add the binaries, use the ldd and add binaries for shared libraries, as shown below.
The above output adds binaries for the bash. Proceed to add for the ls using the same syntax.
For the binaries added, you must copy their libraries. For instance, the image above shows the libraries for the ls dependencies that you should add to proceed to use the command in the chroot jail.
You can copy the libraries one by one or create a bash script to copy all the files simultaneously.
For instance, to copy the /bin/bash libraries, you can use the script below and run it in the current directory in the chroot directory.
The syntax below will copy the libraries one by one.
Ensure to copy the libraries for the ls and the bash.
Once all the libraries are copied, it’s time to use the chroot command to navigate our created chroot jail directory.
Here, you can use either ls or bash. Let’s use the bash with the chroot, as shown below.
That will move you into the chroot directory from which you can’t access other directories or files outside it.
If you wish to leave the chroot jail, type exit on the command line. Finally, you will navigate to the normal root directory for your system.
This guide covered the chroot command and how you can use the different options it offers to create a chroot jail and navigate into it. The bottom line is that the chroot command is a simple but effective Linux command. We’ve seen how you can use it to create virtual environments and build a chroot jail that works independently of the main system.