Difference between revisions of "Change root"

From ArchWiki
Jump to: navigation, search
m (Run graphical chrooted applications: Added more verbiage to better describe the process.)
(Move unmounting command to under chroot, since it does not apply to arch-chroot)
 
(107 intermediate revisions by 27 users not shown)
Line 1: Line 1:
 
[[Category:System recovery]]
 
[[Category:System recovery]]
[[es:Change Root]]
+
[[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-cn: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''.
  
== Requirements ==
+
== Reasoning ==
  
* 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).
+
Changing root is commonly done for performing system maintenance on systems where booting and/or logging in is no longer possible. Common examples are:
  
* Root privileges are required in order to chroot.
+
* Reinstalling the [[bootloader]].
 +
* Rebuilding the [[mkinitcpio|initramfs image]].
 +
* Upgrading or [[downgrading packages]].
 +
* Resetting a [[Password recovery|forgotten password]].
  
* 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:
+
See also [[Wikipedia:Chroot#Limitations]].
  
: {{bc|# uname -m}}
+
== Requirements ==
  
* 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.
+
* Root privilege.
  
== Mount the partitions ==
+
* Another Linux environment, e.g. a LiveCD or USB flash media, or from another existing Linux distribution.
  
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:
+
* 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).
  
# lsblk /dev/sda
+
* Kernel modules loaded that are needed in the chroot environment.
  
You can also run the following to get an idea of your partition layout.
+
* Swap enabled if needed: {{bc|# swapon /dev/sdxY}}
  
# fdisk -l
+
* Internet connection established if needed.
  
Now create a directory where you would like to mount the root partition and mount it:
+
== Usage ==
  
# mkdir /mnt/arch
+
{{Note|
# mount /dev/sda3 /mnt/arch
+
*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).}}
  
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:
+
There are two main options for using chroot, described below.
  
# mount /dev/sda1 /mnt/arch/boot/
+
=== Using arch-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.
+
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.
  
== Change root ==
+
==== Enter a chroot ====
  
Mount the temporary filesystems as root:
+
Run arch-chroot with the new root directory as first argument:
  
{{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.}}
+
# arch-chroot ''/location/of/new/root''
  
cd /mnt/arch
+
For example, in the [[installation guide]] this directory would be {{ic|/mnt}}:
mount -t proc proc proc/
+
mount -t sysfs sys sys/
+
mount -o bind /dev dev/
+
mount -t devpts pts dev/pts/
+
  
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.
+
# arch-chroot /mnt
  
cp -L /etc/resolv.conf etc/resolv.conf
+
To exit the chroot simply use:
  
Now chroot into your installed system and define your shell:
+
# exit
  
chroot /mnt/arch /bin/bash
+
==== Run a single command and exit ====
  
{{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.}}
+
To run a command from the chroot, and exit again append the command to the end of the line:
{{Note|If you see the error {{ic|chroot: '/bin/bash': permission denied}}, remount with the exec permission: {{ic|mount -o remount,exec /mnt/arch}}.}}
+
Optionally, to source your Bash configuration ({{ic|~/.bashrc}} and {{ic|/etc/bash.bashrc}}), run:
+
  
  source ~/.bashrc
+
  # arch-chroot ''/location/of/new/root'' ''mycommand''
source /etc/profile
+
  
Optionally, create a unique prompt to be able to differentiate your chroot environment:
+
For example, to run {{ic|mkinitcpio -p linux}} for a chroot located at {{ic|/mnt/arch}} do:
  
  export PS1="(chroot) $PS1"
+
  # arch-chroot /mnt/arch mkinitcpio -p linux
  
== Run graphical chrooted applications ==
+
=== Using chroot ===
  
If you have [[X]] running on your system, you can start graphical applications from the 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.}}
  
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:
+
In the following example ''/location/of/new/root'' is the directory where the new root resides.
  
$ xhost +
+
First, mount the temporary api filesystems:
  
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
+
# cd ''/location/of/new/root''
 +
# mount -t proc proc proc/
 +
# mount --rbind /sys sys/
 +
# mount --rbind /dev dev/
  
$ echo $DISPLAY
+
And optionally:
  
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
+
# mount --rbind /run run/
  
# export DISPLAY=:0
+
Next, in order to use an internet connection in the chroot environment copy over the DNS details:
  
Now you can launch GUI apps from the chroot command line. ;)
+
# 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:
  
  # umount {proc,sys,dev,boot,[...],}
+
# cd /
 +
  # umount --recursive ''/location/of/new/root''
  
Finally, attempt to unmount your root partition:
+
{{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.}}
  
# cd ..
+
== Run graphical applications from chroot ==
# 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:
+
If you have an [[X server]] running on your system, you can start graphical applications from the chroot environment.
  
* A program was left running inside of the chroot.
+
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:
  
* 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:
+
$ xhost +local:
  
: {{bc|lsblk /dev/sda}}
+
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
  
: If you are still unable to unmount a partition, use the {{ic|--force}} option:
+
$ echo $DISPLAY
  
: {{bc|# umount -f /mnt}}}}
+
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
  
After this, you will be able to safely reboot.
+
# export DISPLAY=:0
  
== Example ==
+
== Without root privileges ==
This may protect your system from Internet attacks during browsing:
+
 
{{bc|1=
+
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.
# # as root:
+
 
# cd /home/''user''
+
=== Proot ===
# mkdir myroot
+
 
# pacman -S arch-install-scripts
+
[[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''.
# # pacstrap must see myroot as mounted:
+
 
# mount --bind myroot myroot
+
=== Fakechroot ===
# pacstrap -i myroot base base-devel
+
 
# mount -t proc proc myroot/proc/
+
{{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.  
# mount -t sysfs sys myroot/sys/
+
 
# mount -o bind /dev myroot/dev/
+
# fakechroot fakeroot chroot ~/my-chroot bash
# mount -t devpts pts myroot/dev/pts/
+
 
# cp -i /etc/resolv.conf myroot/etc/
+
== See also ==
# chroot myroot
+
 
# # inside chroot:
+
* [https://help.ubuntu.com/community/BasicChroot Basic Chroot]
# passwd # set a password
+
# useradd -m -s /bin/bash ''user''
+
# passwd ''user'' # set a password
+
# # in a shell outside the chroot:
+
# pacman -S xorg-server-xnest
+
# # in a shell outside the chroot you can run this as ''user'':
+
$ Xnest -ac -geometry 1024x716+0+0 :1
+
# # continue inside the chroot:
+
# pacman -S xterm
+
# DISPLAY=:1
+
# xterm
+
# # xterm is now running in Xnest
+
# pacman -S xorg-server xorg-xinit xorg-server-utils
+
# pacman -S openbox
+
# # for java we need icedtea-web which requires some fonts:
+
# nano /etc/locale.gen
+
# # uncomment en_US.UTF-8 UTF-8, save and exit
+
# locale-gen
+
# echo LANG=en_US.UTF-8 > /etc/locale.conf
+
# export LANG=en_US.UTF-8
+
# pacman -S ttf-dejavu
+
# pacman -S icedtea-web
+
# pacman -S firefox
+
# firefox
+
# # firefox is now running in Xnest
+
# exit
+
# # outside chroot:
+
# chroot --userspec=''user'' myroot
+
# # inside chroot as ''user'':
+
$ DISPLAY=:1
+
$ openbox &
+
$ HOME="/home/''user''"
+
$ firefox
+
}}
+
See also: [https://help.ubuntu.com/community/BasicChroot Basic Chroot]
+

Latest revision as of 14:57, 13 October 2016

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