The chroot command in Linux is used to change the root directory for a specific process or shell session, effectively creating a new root directory within the existing file-system. This can be useful for various purposes, such as system recovery, software development, or testing.
Being more precised, the command or processes executed under shell sessions, can’t be used to access the files outside the root directory. And this altered environment is termed as “jailed directory”. Here the image might help you understand this.
Here’s the basic syntax of the chroot command:
chroot /path/to/new_root [command]
/path/to/new_root: It’s actually a newly created root directory’s path specified for commands or processes executed under chroot. command (optional): Basically, this portion is optional, however it’s used to specify the command or shell to run within newly created root environment.
Options used with chroot command in Linux.
–userspec=USER:GROUP : Basically, it can be specified in form of either name or numeric id to a specific user and group to be used. –groups=G_LIST : Used to define supplementary groups as g1, g2, g3, g4, and so on. –version : Displays version details and exits. –help : Displays help regarding chroot and exits.
Important Note: Do make sure to use ‘sudo’ while using chroot as it requires superuser privileges to execute correctly and also as mentioned in the examples ahead. Also, you should be quite cautious while working with chroot as it potentially affect your system’s security and stability as well
Examples of chroot command in Linux
To understand this command and its usage in Linux, let’s take a look through some of the instances below:
1: Creating new chroot environment and initiating a shell within it.
In this example, we have created a new chroot environment and started a shell under which we can install packages, run commands and manage the environment like a separate Linux machine.
sudo mkdir /mychroot sudo debootstrap [your_debian_release] /mychroot sudo chroot /mychroot
2: Executing commands within chroot environment
Through below mentioned instance, we have executed ls / command under newly created chroot environment at /mychroot. You can run any other commands as well to execute based on your requirements.
sudo chroot /mychroot /bin/bash -c "ls /"
3: Exiting the chroot environment
In order to exit the chroot, just type ‘exit’ command or press a key combination Ctrl+D to return back to your original system.
4: Mount other filesystems under chroot environment.
Mounting the additional file-systems within chroot makes your experience more complete and ensures that essential directories such as /proc, /dev, and /sys, etc are available to use.
sudo mount -t proc proc /mychroot/proc sudo mount -o bind /dev /mychroot/dev sudo mount -o bind /sys /mychroot/sys
Using GUI within chroot environment
If at some extent, you prefer to run a graphical application within jailed directory created by chroot, you will require to add additional configuration such as X11 forwarding or install a display server. This section is actually an more advanced topic, so needs you some specific configuration as well depending on your setup.

Nishant Verma is a senior web developer who love to share his knowledge about Linux, SysAdmin, and more other web handlers. Currently, he loves to write as content contributor for ServoNode.