“chroot” Command Usage Examples in Linux

«
»

This tutorial explains Linux “chroot” command, options and its usage with examples.

chroot – change root directory

Description:

advertisement

A chroot command on Unix operating systems is an operation that changes the apparent root directory for the current running process and its children.When you change root to another directory you cannot access files and commands outside that directory.This directory is called a chroot jail. Changing root is commonly done for system maintenance, such as reinstalling the bootloader or resetting a forgotten password.

Synopsis:

chroot NEWROOT [COMMAND [ARGS]…]

advertisement
advertisement

The root partition of the Linux system that you’re trying to chroot into needs to be mounted first using mount command. After that create a directory where you would like to mount the root partition and mount it and also mount all the separate partitions of the system.

While it’s possible to mount filesystems after you’ve chrooted, it is more convenient to do so beforehand. The reasoning for this is that you’ll have to unmount the temporary filesystems after you exit the chroot, so this lets you umount all the filesystems with a single command.

Example :

advertisement
$ chroot /mnt/arch /usr/bin/bash

Using this command we change the root to /mnt/arch and we have defined the shell.

Note: If you see the error chroot: cannot run command ‘/usr/bin/bash’: Exec format error, it is likely that the two architectures do not match.(one on which the linux distribution is running and other one where we are changing root to)

advertisement

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter