Difference between revisions of "Change root"

From ArchWiki
Jump to: navigation, search
(update zh-cn/tw interlanguage links to zh-hans/hant, see Help talk:I18n#Chinese interlanguage links)
 
(131 intermediate revisions by 31 users not shown)
Line 1: Line 1:
 
[[Category:System recovery]]
 
[[Category:System recovery]]
 +
[[es:Change root]]
 
[[fa:تغییر ریشه]]
 
[[fa:تغییر ریشه]]
 
[[fr:Chroot]]
 
[[fr:Chroot]]
 
[[ja:Change Root]]
 
[[ja:Change Root]]
 
[[ro:Chroot]]
 
[[ro:Chroot]]
[[zh-CN:Change Root]]
+
[[ru: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.
+
[[zh-hans:Change root]]
 +
{{Related articles start}}
 +
{{Related|Install bundled 32-bit system in Arch64}}
 +
{{Related|proot}}
 +
{{Related|Linux Containers}}
 +
{{Related|systemd-nspawn}}
 +
{{Related articles end}}
 +
[[Wikipedia:Chroot|Chroot]] is an operation that changes the apparent root directory for the current running process and their children. A program that is run in such a modified environment cannot access files and commands outside that environmental directory tree. This modified environment is called a ''chroot jail''.
 +
 
 +
== Reasoning ==
 +
 
 +
Changing root is commonly done for performing system maintenance on systems where booting and/or logging in is no longer possible. Common examples are:
 +
 
 +
* Reinstalling the [[bootloader]].
 +
* Rebuilding the [[mkinitcpio|initramfs image]].
 +
* Upgrading or [[downgrading packages]].
 +
* Resetting a [[Password recovery|forgotten password]].
 +
 
 +
See also [[Wikipedia:Chroot#Limitations]].
  
 
== Requirements ==
 
== 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 privilege.
  
* Root privileges are required in order to chroot.
+
* Another Linux environment, e.g. a LiveCD or USB flash media, or from another existing Linux distribution.
  
* 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:
+
* Matching architecture environments; i.e. the chroot from and chroot to.  The architecture of the current environment can be discovered with: {{ic|uname -m}} (e.g. i686 or x86_64).
  
: {{bc|# uname -m}}
+
* Kernel modules loaded that are needed in the chroot environment.
  
* If you need any kernel modules loaded in the chroot environment, load them before chrooting. It may also be useful to initialize your swap ({{ic|swapon /dev/sdxY}}) and to establish an internet connection before chrooting.
+
* Swap enabled if needed: {{bc|# swapon /dev/sdxY}}
  
== Mount the partitions ==
+
* Internet connection established if needed.
  
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:
+
== Usage ==
  
# lsblk /dev/sda
+
{{Note|
 +
*Some [[systemd]] tools such as ''localectl'' and ''timedatectl'' can not be used inside a chroot, as they require an active [[dbus]] connection. [https://github.com/systemd/systemd/issues/798#issuecomment-126568596]
 +
*The file system that will serve as the new root ({{ic|/}}) of your chroot must accessible (i.e., decrypted, mounted).}}
  
Then create a directory where you would like to mount this partition and mount it:
+
There are two main options for using chroot, described below.
  
# mkdir /mnt/arch
+
=== Using arch-chroot ===
# 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:
+
The bash script {{ic|arch-chroot}} is part of the {{Pkg|arch-install-scripts}} package. Before it runs {{ic|/usr/bin/chroot}}, the script mounts api filesystems like {{ic|/proc}} and makes {{ic|/etc/resolv.conf}} available from the chroot.
  
# mount /dev/sda1 /mnt/arch/boot/
+
==== Enter a chroot ====
# mount /dev/sdb5 /mnt/arch/home/
+
# 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 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.
+
Run arch-chroot with the new root directory as first argument:
  
== Change root ==
+
# arch-chroot ''/location/of/new/root''
  
Mount the temporary filesystems:
+
For example, in the [[installation guide]] this directory would be {{ic|/mnt}}:
  
{{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.}}
+
# arch-chroot /mnt
  
# cd /mnt/arch
+
To exit the chroot simply use:
# mount -t proc proc proc/
+
# mount -t sysfs sys sys/
+
# mount -o bind /dev dev/
+
# 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:
+
# exit
  
# cp -L /etc/resolv.conf etc/resolv.conf
+
==== Run a single command and exit ====
  
Now chroot into your installed system and define your shell:
+
To run a command from the chroot, and exit again append the command to the end of the line:
  
  # chroot . /bin/bash
+
  # arch-chroot ''/location/of/new/root'' ''mycommand''
  
{{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.}}
+
For example, to run {{ic|mkinitcpio -p linux}} for a chroot located at {{ic|/mnt/arch}} do:
  
Optionally, to source your Bash configuration ({{ic|~/.bashrc}} and {{ic|/etc/bash.bashrc}}), run:
+
# arch-chroot /mnt/arch mkinitcpio -p linux
  
# source ~/.bashrc
+
=== Using chroot ===
# source /etc/profile
+
  
Optionally, create a unique prompt to be able to differentiate your chroot environment:
+
{{Warning|When using {{ic|--rbind}}, some subdirectories of {{ic|dev/}} and {{ic|sys/}} will not be unmountable. Attempting to unmount with {{ic|umount -l}} in this situation will break your session, requiring a reboot. If possible, use {{ic|-o bind}} instead.}}
  
# export PS1="(chroot) $PS1"
+
In the following example ''/location/of/new/root'' is the directory where the new root resides.
  
== Run graphical chrooted applications ==
+
First, mount the temporary api filesystems:
  
If you have [[X]] running on your system, you can start graphical applications from the chroot environment.
+
# cd ''/location/of/new/root''
 +
# mount -t proc proc proc/
 +
# mount --rbind /sys sys/
 +
# mount --rbind /dev dev/
  
To allow the connection to your X server, you have to run the following from a terminal:
+
And optionally:
  
  # xhost +
+
  # mount --rbind /run run/
  
Then, to direct the applications to your X server, run:
+
Next, in order to use an internet connection in the chroot environment copy over the DNS details:
  
  # export DISPLAY=:0.0
+
  # cp /etc/resolv.conf etc/resolv.conf
  
== Perform system maintenance ==
+
Finally, to change root into ''/location/of/new/root'' using a bash shell:
  
At this point you can perform whatever system maintenance you require inside the chroot environment. A few common examples are:
+
# chroot ''/location/of/new/root'' /bin/bash
  
* Reinstall the bootloader.
+
{{Note|If you see the error:
* Rebuild your [[mkinitcpio|initramfs]] image.
+
* {{ic|chroot: cannot run command '/usr/bin/bash': Exec format error}}, it is likely that the architectures of the host environment and chroot environment do not match.
* Upgrade or [[Downgrading_Packages|downgrade]] packages.
+
* {{ic|chroot: '/usr/bin/bash': permission denied}}, remount with the exec permission: {{ic|mount -o remount,exec /mnt/arch}}.
* Reset a [[Password_Recovery|forgotten password]].
+
}}
  
== Exit the chroot environment ==
+
After chrooting it may be necessary to load the local bash configuration:
  
When you're finished with system maintenance, exit the chroot:
+
# source /etc/profile
 +
# source ~/.bashrc
 +
 
 +
{{Tip|Optionally, create a unique prompt to be able to differentiate your chroot environment:
 +
{{bc|1=# export PS1="(chroot) $PS1"}}
 +
}}
 +
 
 +
When finished with the chroot, you can exit it via:
  
 
  # exit
 
  # exit
  
Then unmount the temporary filesystems and any mounted devices:
+
Then unmount the temporary file systems:
 +
 
 +
# cd /
 +
# umount --recursive ''/location/of/new/root''
 +
 
 +
{{Note|If there is an error mentioning something like: {{ic|umount: /path: device is busy}} this usually means that either: a program (even a shell) was left running in the chroot or that a sub-mount still exists.  Quit the program and use {{ic|mount | grep /mnt/arch}} to find and {{ic|umount}} sub-mounts).  It may be tricky to {{ic|umount}} some things and one can hopefully have {{ic|umount --force}} work, as a last resort use {{ic|umount --lazy}} which just releases them.  In either case to be safe, {{ic|reboot}} as soon as possible if these are unresolved to avoid future, possible conflicts.}}
 +
 
 +
== Run graphical applications from chroot ==
 +
 
 +
If you have an [[X server]] 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 virtual terminal inside the X server (i.e. inside the desktop of the user that is currently logged in), then run the [[xhost]] command, which gives permission to anyone to connect to the user's X server:
 +
 
 +
$ xhost +local:
 +
 
 +
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
  
  # umount {proc,sys,dev,boot,[...],}
+
  # export DISPLAY=:0
  
Finally, attempt to unmount your root partition:
+
== Without root privileges ==
  
# cd ..
+
Chroot requires root privileges, which may not be desirable or possible for the user to obtain in certain situations. There are, however, various ways to simulate chroot-like behavior using alternative implementations.
# umount arch/
+
  
{{Note|If you get an error saying that {{ic|/mnt}} (or any other partition) is busy, this can mean one of two things:
+
=== Proot ===
  
* A program was left running inside of the chroot.
+
[[Proot]] may be used to change the apparent root directory and use {{ic|mount --bind}} without root privileges. This is useful for confining applications to a single directory or running programs built for a different CPU architecture, but it has limitations due to the fact that all files are owned by the user on the host system. Proot provides a {{ic|--root-id}} argument that can be used as a workaround for some of these limitations in a similar (albeit more limited) manner to ''fakeroot''.
  
* 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:
+
=== Fakechroot ===
  
: {{bc|lsblk /dev/sda}}
+
{{Pkg|fakechroot}} is a library shim which intercepts the chroot call and fakes the results. It can be used in conjunction with {{Pkg|fakeroot}} to simulate a chroot as a regular user.
  
: If you are still unable to unmount a partition, use the {{ic|--force}} option:
+
# fakechroot fakeroot chroot ~/my-chroot bash
  
: {{bc|# umount -f /mnt}}}}
+
== See also ==
  
After this, you will be able to safely reboot.
+
* [https://help.ubuntu.com/community/BasicChroot Basic Chroot]

Latest revision as of 10:15, 11 January 2017

Chroot is an operation that changes the apparent root directory for the current running process and their children. A program that is run in such a modified environment cannot access files and commands outside that environmental directory tree. This modified environment is called a chroot jail.

Reasoning

Changing root is commonly done for performing system maintenance on systems where booting and/or logging in is no longer possible. Common examples are:

See also Wikipedia:Chroot#Limitations.

Requirements

  • Root privilege.
  • Another Linux environment, e.g. a LiveCD or USB flash media, or from another existing Linux distribution.
  • Matching architecture environments; i.e. the chroot from and chroot to. The architecture of the current environment can be discovered with: uname -m (e.g. i686 or x86_64).
  • Kernel modules loaded that are needed in the chroot environment.
  • Swap enabled if needed:
    # swapon /dev/sdxY
  • Internet connection established if needed.

Usage

Note:
  • Some systemd tools such as localectl and timedatectl can not be used inside a chroot, as they require an active dbus connection. [1]
  • The file system that will serve as the new root (/) of your chroot must accessible (i.e., decrypted, mounted).

There are two main options for using chroot, described below.

Using arch-chroot

The bash script arch-chroot is part of the arch-install-scripts package. Before it runs /usr/bin/chroot, the script mounts api filesystems like /proc and makes /etc/resolv.conf available from the chroot.

Enter a chroot

Run arch-chroot with the new root directory as first argument:

# arch-chroot /location/of/new/root

For example, in the installation guide this directory would be /mnt:

# arch-chroot /mnt

To exit the chroot simply use:

# exit

Run a single command and exit

To run a command from the chroot, and exit again append the command to the end of the line:

# arch-chroot /location/of/new/root mycommand

For example, to run mkinitcpio -p linux for a chroot located at /mnt/arch do:

# arch-chroot /mnt/arch mkinitcpio -p linux

Using chroot

Warning: When using --rbind, some subdirectories of dev/ and sys/ will not be unmountable. Attempting to unmount with umount -l in this situation will break your session, requiring a reboot. If possible, use -o bind instead.

In the following example /location/of/new/root is the directory where the new root resides.

First, mount the temporary api filesystems:

# cd /location/of/new/root
# mount -t proc proc proc/
# mount --rbind /sys sys/
# mount --rbind /dev dev/

And optionally:

# mount --rbind /run run/

Next, in order to use an internet connection in the chroot environment copy over the DNS details:

# cp /etc/resolv.conf etc/resolv.conf

Finally, to change root into /location/of/new/root using a bash shell:

# chroot /location/of/new/root /bin/bash
Note: If you see the error:
  • chroot: cannot run command '/usr/bin/bash': Exec format error, it is likely that the architectures of the host environment and chroot environment do not match.
  • chroot: '/usr/bin/bash': permission denied, remount with the exec permission: mount -o remount,exec /mnt/arch.

After chrooting it may be necessary to load the local bash configuration:

# source /etc/profile
# source ~/.bashrc
Tip: Optionally, create a unique prompt to be able to differentiate your chroot environment:
# export PS1="(chroot) $PS1"

When finished with the chroot, you can exit it via:

# exit

Then unmount the temporary file systems:

# cd /
# umount --recursive /location/of/new/root
Note: If there is an error mentioning something like: umount: /path: device is busy this usually means that either: a program (even a shell) was left running in the chroot or that a sub-mount still exists. Quit the program and use mount to find and umount sub-mounts). It may be tricky to umount some things and one can hopefully have umount --force work, as a last resort use umount --lazy which just releases them. In either case to be safe, reboot as soon as possible if these are unresolved to avoid future, possible conflicts.

Run graphical applications from chroot

If you have an X server 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 virtual terminal inside the X server (i.e. inside the desktop of the user that is currently logged in), then run the xhost command, which gives permission to anyone to connect to the user's X server:

$ xhost +local:

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

Without root privileges

Chroot requires root privileges, which may not be desirable or possible for the user to obtain in certain situations. There are, however, various ways to simulate chroot-like behavior using alternative implementations.

Proot

Proot may be used to change the apparent root directory and use mount --bind without root privileges. This is useful for confining applications to a single directory or running programs built for a different CPU architecture, but it has limitations due to the fact that all files are owned by the user on the host system. Proot provides a --root-id argument that can be used as a workaround for some of these limitations in a similar (albeit more limited) manner to fakeroot.

Fakechroot

fakechroot is a library shim which intercepts the chroot call and fakes the results. It can be used in conjunction with fakeroot to simulate a chroot as a regular user.

# fakechroot fakeroot chroot ~/my-chroot bash

See also