Difference between revisions of "Change root"

(various improvements... no need to fix the fstab from a chroot, moved the Wikipedia link at the top (like most other articles), grammar fixes, added a "#" in front of the commands, added a note about the Arch 2012 release, etc)

Revision as of 11:06, 3 September 2012

Chroot is the process of changing of the apparent disk root directory (and the current running process and its children) to another root directory. 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.

Contents

Requirements

You'll need to boot from another working Linux environment (e.g. from a LiveCD or USB flash media, or from another installed Linux distribution).

Root privileges are required in order to chroot.

Be sure that the architectures of the Linux environment you have booted into matches the architecture of the root directory you wish to enter (i.e. i686, x86_64). You can find the architecture of your current environment with:

# uname -m

If you need any kernel modules loaded in the chroot environment, load them before chrooting. It may also be useful to initialize your swap (swapon /dev/sdxY) and to establish an internet connection before chrooting.

Mount the partitions

The root partition of the Linux system that you're trying to chroot into needs to be mounted. To find out the device name assigned by the kernel, run:

# fdisk -l

Then create a directory where you would like to mount this partition and mount it:

# mkdir /mnt/arch
# mount /dev/sdxY /mnt/arch

Next, if you have separate partitions for other parts of your system (e.g. /boot, /home, /var, etc) you should mount them as well:

# mount /dev/sdxZ boot/

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 in a single command. This also allows a safer shutdown. Because the external Linux environment knows all mounted partitions it can safely unmount them during shutdown.

Change root

Mount the temporary filesystems:

Note: Using a newer (2012) Arch release, the following mount commands can be replaced with arch-chroot /mnt/arch, if the root partition was mounted in that location. Of course, you may still type these, if you want, or if you only have some other "live" Linux distribution.