At times, when we want to experiment with something new, we always prefer to run it on a virtual machine or in a sandbox environment so that it cannot cause any damage to our original machine or its important files. The chroot command allows us to run commands and programs in a safe environment by creating an alternate root directory for them. Therefore, we will be talking about the method of using this command on Debian 11.

For using chroot on Debian 11, you will need to perform the following steps:

Launch the terminal in Debian and then type the following command in it followed by pressing the Enter key to create a new root directory:

sudo mkdir ~/new_root

This command is also shown in the image below:

<img alt="Create a new directory" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-982.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="30" src="data:image/svg xml,” width=”392″>

After successfully creating a new root directory, we will add all the relevant sub-directories in the new root directory. For doing that, type the following command in your terminal and then press the Enter key:

sudo mkdir –p ~/new_root/{bin,lib,lib64}

Running this command will create the bin, lib, and lib64 sub-directories within the new root directory. This command is shown in the following image:

<img alt="Create system directories" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-983.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="25" loading="lazy" src="data:image/svg xml,” width=”561″>

Once you have successfully created these sub-directories, the next thing that you need to do is to copy the binaries of the commands and programs that you want to test using chroot in the new root directory. For doing that, you first need to look for these binaries. In this example, we are going to find out the binaries of the following commands: ip, ls, and pwd. Moreover, we are also going to look for the binary of the bash program. For doing that, we will make use of the which command as shown below:

which ip ls pwd bash

Type this command in your terminal and then press the Enter key. It is also shown in the image below:

<img alt="Use which command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-984.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="23" loading="lazy" src="data:image/svg xml,” width=”384″>

As soon as this command will execute successfully, you will be able to see the paths to the binaries of the respective commands and program in your terminal as shown in the following image:

<img alt="Find program paths using which command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-985.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="122" loading="lazy" src="data:image/svg xml,” width=”394″>

  • Once you have found out all the respective binaries, you need to copy them into the new root directory. For doing that, type the following command in your terminal and then press the Enter key:
sudo cp –v /bin/{ip,ls,pwd,bash} ~/new_root/bin

This command is shown in the image below:

<img alt="Chroot subdirectories" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/chroot-directories.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="31" loading="lazy" src="data:image/svg xml,” width=”631″>

The successful execution of this command will show you something like this on your terminal which is, in fact, a confirmation that the binaries have been copied into the new root directory:

<img alt="Copy binary files" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-987.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="116" loading="lazy" src="data:image/svg xml,” width=”632″>

The next step is to find out and copy the dependencies of these commands and programs in the new root directory. For finding out the dependencies of the ip command, type the following command in your terminal and then press the Enter key:

ldd /bin/ip

This command is also shown in the following image:

<img alt="ldd command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-988.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="29" loading="lazy" src="data:image/svg xml,” width=”313″>

Running this command will show you all the dependencies of the ip command. You can find out the dependencies for the ls and pwd command in the very same manner. The dependencies of the ip command are shown in the image below:

<img alt="Find out which libraries a binary file requires with ldd" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-989.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="283" loading="lazy" src="data:image/svg xml,” width=”727″>

Now you need to copy all the dependencies of the ip command in the new root directory. For doing that, type the command as shown in the following image:

<img alt="Copy dependencies" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-990.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="65" loading="lazy" src="data:image/svg xml,” width=”725″>

As soon as this command will execute successfully, your terminal will look something like this:

<img alt="File dependencies" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-991.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="322" loading="lazy" src="data:image/svg xml,” width=”726″>

However, for copying the /lib64 dependency, you will need to type the command in your terminal as shown in the image below:

<img alt="lib64 dependencies" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-992.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="25" loading="lazy" src="data:image/svg xml,” width=”701″>

Your terminal will prompt you with a message showing that the /lib64 files have been copied successfully as shown in the following image:

<img alt="command copied" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-993.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="79" loading="lazy" src="data:image/svg xml,” width=”723″>

Now you need to find out the dependencies for the bash program. For doing that, type the following command in your terminal and then press the Enter key:

ldd /bin/bash

This command is also shown in the image below:

<img alt="ldd bash" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-994.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="31" loading="lazy" src="data:image/svg xml,” width=”328″>

Running this command successfully will show you all the required dependencies for the bash program as shown in the following image:

<img alt="bash shell dependencies" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-995.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="159" loading="lazy" src="data:image/svg xml,” width=”725″>

Now copy these dependencies into the new root directory by typing the command shown in the image below in your terminal and then pressing the Enter key:

<img alt="Copying bash file dependencies" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-996.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="45" loading="lazy" src="data:image/svg xml,” width=”726″>

If this command executes successfully, your terminal will look something like this:

<img alt="copied files" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-997.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="133" loading="lazy" src="data:image/svg xml,” width=”726″>

However, for copying the /lib64 dependency, type the command shown in the following image in your terminal and then press the Enter key:

<img alt="lib64 dep" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-998.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="31" loading="lazy" src="data:image/svg xml,” width=”699″>

Your terminal will display a message confirming that the above-mentioned dependency has been copied to the new root directory successfully as shown in the image below:

<img alt="copied files" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-999.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="76" loading="lazy" src="data:image/svg xml,” width=”725″>

Once these steps are executed successfully, you need to navigate to the new root directory. For doing that, type the following command in your terminal and then press the Enter key:

sudo chroot ~/new_root/bin/bash

This command will take you to the bash program environment. It is also shown in the following image:

<img alt="Use chroot" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1000.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="28" loading="lazy" src="data:image/svg xml,” width=”496″>

Once you are running the bash program in your newly created root directory, try running the commands whose configurations you have done above. In this example, I will try to run the ip command and find out if it works in the new root directory or not. For doing that, type the following command in your terminal and then press the Enter key:

ip link

Running this command will display the output shown in the image below which is a confirmation that the ip command is running successfully in the new root directory:

<img alt="use ip command in jail" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1001.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="150" loading="lazy" src="data:image/svg xml,” width=”726″>

Conclusion

By following the method described in this article, you can easily use the chroot command on Debian 11. Although the whole process seems quite long if you manage to follow all the steps correctly, you will never get yourself in any trouble.