[[Wikipedia:Chroot|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.

[[Wikipedia:Chroot|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.

Line 18:

Line 22:

== Mount the partitions ==

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

+

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

# lsblk /dev/sda

−

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

+

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

# mkdir /mnt/arch

# mount /dev/sda3 /mnt/arch

# mount /dev/sda3 /mnt/arch

−

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

+

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

# mount /dev/sda1 /mnt/arch/boot/

# mount /dev/sda1 /mnt/arch/boot/

Line 33:

Line 41:

# mount ...

# mount ...

−

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.

+

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 ==

== Change root ==

Line 51:

Line 59:

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

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

−

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

+

Now chroot into your installed system and define your shell:

# chroot . /bin/bash

# chroot . /bin/bash

−

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

+

{{Note|If you see the error {{ic|chroot: cannot run command '/bin/bash': Exec format error}}, it is likely that the two architectures do not match.}}

Consider creating a unique prompt to be able to differentiate your chroot environment:

+

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

# export PS1="(chroot) $PS1"

# export PS1="(chroot) $PS1"

Line 69:

Line 78:

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

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

−

To allow the connection to your X server, you have to type the following into a terminal of your running system:

+

To allow the connection to your X server, you have to run the following from a terminal:

# xhost +

# xhost +

−

In your chroot environment type the following, to direct the applications to your xserver:

+

Then, to direct the applications to your X server, run:

# export DISPLAY=:0.0

# export DISPLAY=:0.0

Line 79:

Line 88:

== Perform system maintenance ==

== Perform system maintenance ==

−

At this point you can perform whatever system maintenance you require inside the chroot environment, some common examples being:

+

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

* Reinstall the bootloader.

* Reinstall the bootloader.

Line 88:

Line 97:

== Exit the chroot environment ==

== Exit the chroot environment ==

−

When you're finished with system maintenance, exit the chroot shell:

+

When you're finished with system maintenance, exit the chroot:

# exit

# exit

Line 96:

Line 105:

# umount {proc,sys,dev,boot,[...],}

# umount {proc,sys,dev,boot,[...],}

−

Finally attempt to unmount your hard drive:

+

Finally, attempt to unmount your root partition:

# cd ..

# cd ..

# umount arch/

# umount arch/

−

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

+

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

* A program was left running inside of the chroot.

* A program was left running inside of the chroot.

+

* Or, more frequently, a sub-mount still exists (e.g. {{ic|/mnt/arch/boot}} within {{ic|/mnt/arch}}). Check with {{ic|lsblk}} to see if there are any mountpoints left:

* Or, more frequently, a sub-mount still exists (e.g. {{ic|/mnt/arch/boot}} within {{ic|/mnt/arch}}). Check with {{ic|lsblk}} to see if there are any mountpoints left:

: {{bc|lsblk /dev/sda}}

: {{bc|lsblk /dev/sda}}

−

: If you are still unable to unmount a partition, use the force option:

+

: If you are still unable to unmount a partition, use the {{ic|--force}} option:

−

: {{bc|# umount -f /mnt}}

+

: {{bc|# umount -f /mnt}}}}

After this, you will be able to safely reboot.

After this, you will be able to safely reboot.

Revision as of 22:46, 22 December 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 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:

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.