Changing Root 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 for such tasks as reinstalling [[Grub#Bootloader_installation|GRUB]] or resetting a forgotten password. Changing root is often done from from a LiveCD or LiveUSB into a mounted partition that contains an installed system.

Changing Root 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 for such tasks as reinstalling [[Grub#Bootloader_installation|GRUB]] or resetting a forgotten password. Changing root is often done from from a LiveCD or LiveUSB into a mounted partition that contains an installed system.

Revision as of 13:58, 13 June 2012

Changing Root 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 for such tasks as reinstalling GRUB or resetting a forgotten password. Changing root is often done from from a LiveCD or LiveUSB into a mounted partition that contains an installed system.

Contents

Requirements

You'll need to boot to another working Linux environment (for example, to a LiveCD or USB flash disk). For Arch install mediums there is both the LiveCD and USB Flash disk.

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 by:

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/<device-or-partition-name>) and to connect to your network before chrooting.

Mounting the device

The device or partition with the Linux system on it will need to be mounted. To discover the kernel name of the storage device name, type:

fdisk -l

Create a directory where you would like to mount the device or partition, then mount it:

Changing Root

Mount other parts of your filesystem (e.g. /boot, /var, /usr...) that reside on separate partitions but which you need access to. For example:

mount /dev/<device-or-partition-name> boot/

It's possible to mount filesystems after you've chrooted, but it's more convenient to do so beforehand. The reasoning for this is you'll have to unmount the temporary filesystems after you exit a 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.

If you've setup your network and want to use it in the chroot environment, copy over your DNS servers so that you will be connected to the network:

cp -L /etc/resolv.conf etc/resolv.conf

Now chroot to your installed device or partition and define your shell:

chroot . /bin/bash

If you see the error, "chroot: cannot run command '/bin/bash': Exec format error" it is likely the two architectures do not match.

If you get an error saying that /mnt (or any other partition) is busy, this can mean one of two things:

A program was left running inside of the chroot.

Or more frequently: a sub-mount still exists. For example, /mnt/arch/usr within /mnt/arch.

In the latter case, unmount the sub-mount mount point first. To get a reminder of all the current mount points, run mount with no parameters. If you still are unable to mount a partition, use the force option: