[[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.

+

[[ja:Change Root]]

+

[[ro:Chroot]]

+

[[zh-CN:Change Root]]

+

[[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.

== Requirements ==

== Requirements ==

Line 10:

Line 14:

* Root privileges are required in order to chroot.

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

+

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

: {{bc|# uname -m}}

: {{bc|# uname -m}}

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

+

# 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 <root> /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 <boot> /mnt/arch/boot/

+

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

−

# mount <home> /mnt/arch/home/

+

# mount /dev/sdb5 /mnt/arch/home/

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

−

Mount the temporary filesystems:

+

Mount the temporary filesystems as root:

−

{{Note|Using a newer (2012) Arch release, the following {{ic|mount}} commands can be replaced with {{ic|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.}}

+

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

−

# cd /mnt/arch

+

cd /mnt/arch

−

# mount -t proc proc proc/

+

mount -t proc proc proc/

−

# mount -t sysfs sys sys/

+

mount -t sysfs sys sys/

−

# mount -o bind /dev dev/

+

mount -o bind /dev dev/

−

# mount -t devpts pts dev/pts/

+

mount -t devpts pts dev/pts/

−

If you established an internet connection and want to use it in the chroot environment, you may have to copy over your DNS servers so that you will be connected to the network:

+

If you have established an internet connection and want to use it in the chroot environment, you may have to copy over your DNS configuration to be able to resolve hostnames.

−

# 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 /mnt/arch /bin/bash

+

chroot /mnt/arch /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.}}

Revision as of 10:38, 16 February 2013

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.