zh-CN:Change RootChroot 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 architecture 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 first. To find out the device name assigned by the kernel, run:

# lsblk /dev/sda

You can also run the following to get an idea of your partition layout.

# fdisk -l

Now create a directory where you would like to mount the root partition and mount it:

# mkdir /mnt/arch
# mount /dev/sda3 /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:

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. This also allows for 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 as root:

Note: Using a newer (2012) Arch release, the following commands can be replaced with arch-chroot /mnt/arch. You must have arch-install-scripts installed to run arch-chroot. The following commands may still be used if you're using a different Linux distribution.

Optionally, create a unique prompt to be able to differentiate your chroot environment:

export PS1="(chroot) $PS1"

Run graphical chrooted applications

If you have X running on your system, you can start graphical applications from the chroot environment.

To allow the chroot environment to connect to an X server, open a terminal inside the X server (i.e. inside the desktop of the user that is currently logged in), then run the following command which gives permission to anyone to connect to the user's X server:

$ xhost +

Then, to direct the applications to the X server from chroot, set the DISPLAY environment variable inside the chroot to match the DISPLAY variable of the user that owns the X server. So for example, run

$ echo $DISPLAY

as the user that owns the X server to see the value of DISPLAY. If the value is ":0" (for example), then in the chroot environment run

# export DISPLAY=:0

Now you can launch GUI apps from the chroot command line. ;)

Perform system maintenance

At this point you can perform whatever system maintenance you require inside the chroot environment. A few common examples are:

An Alternative to chroot Using systemd-nspawn

Alternatively, you can use systemd-nspawn to achieve the same thing, with added benefits (listed in the "systemd-nspawn" man page).

The steps are very similar:

First mount the root partition.

mkdir /mnt/arch

mount /dev/sdx3 /mnt/arch

Then mount the boot and home partitions inside the root partition.

mount /dev/sdx1 /mnt/arch/boot

mount /dev/sdx4 /mnt/arch/home

Then, simply cd into the root partition and run systemd-nspawn.

cd /mnt/arch

systemd-nspawn

As you can see, there's no need to worry about mounting proc, sys, dev, or dev/pts. systemd-nspawn start a new init process in the contained environment which takes care of everything. It's like booting up a second Linux OS on the same machine, but it's not a virtual machine.

To quit, just log out or issue the poweroff command. You can then unmount the partitions like described above (except without having to worry about proc, sys, etc).

TODO: Describe the steps to run an X server inside the systemd-nspawn container.