GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing wiki.archlinux.org — chroot - ArchWiki
Related articles A 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 the specified directory tree. This modified environment is called a chroot jail. ReasoningChanging 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. RequirementsIn order to use chroot, you need another Linux installation or installation media (of any distribution), with:
Prepare new root locationThe chroot target should be a directory which contains a file system hierarchy. In the installation guide, this directory would be Run Mount the file system: # mount /dev/sdXY /mnt If you have an EFI system partition and need to make changes in it (e.g. updating the vmlinuz or initramfs images): # mount /dev/sdXZ /mnt/esp If you have any discrete partitions, mount them too. In the following examples, UsageNote
There are two main options for using chroot, described below. Using arch-chrootThe bash script arch-chroot(8) is part of the arch-install-scripts package. arch-chroot wraps the chroot command while ensuring that important functionality is available, e.g. mounting Enter a chrootRun arch-chroot with the new root directory as first argument: # arch-chroot -S /path/to/new/root You can now do most of the operations available from your existing installation. Some tasks which needs D-Bus will not work as noted in #Usage. Exit a chrootTo exit the chroot, use: # exit Run a single command and exitTo run a command from the chroot and exit again, append the command to the end of the line: # arch-chroot -S /path/to/new/root mycommand For example, to run # arch-chroot -S /mnt/arch mkinitcpio -p linux Running on BtrfsOn a Btrfs root file system with subvolumes, you have to make sure that all subvolumes are properly mounted as specified in fstab before entering chroot. An example with the Btrfs default setup from archinstall: # mount -o subvol=@ /dev/sdXY /mnt # mount -o subvol=@home /dev/sdXY /mnt/home # mount -o subvol=@pkg /dev/sdXY /mnt/var/cache/pacman/pkg # mount -o subvol=@log /dev/sdXY /mnt/var/log # mount -o subvol=@snapshots /dev/sdXY /mnt/.snapshots # mount /dev/sdXZ /mnt/boot # arch-chroot -S /mnt Tip You may also want to specify other mount options, such as for compression.
Using chrootIf you run chroot directly, below steps are needed before actual chroot. First, mount the temporary API filesystems: # cd /path/to/new/root # mount -t proc /proc proc/ # mount -t sysfs /sys sys/ # mount --rbind /dev dev/ 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.And optionally: # mount --rbind /run run/ If you are running a UEFI system, you will also need access to EFI variables. Otherwise, when installing GRUB, you will receive a message similar to: # mount --rbind /sys/firmware/efi/efivars sys/firmware/efi/efivars/ 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 # chroot /path/to/new/root /bin/bash Note If you see the error:
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 /path/to/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 findmnt -R /path/to/new/root to find and then 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 possible future conflicts.Run graphical applications from chrootIf 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 (see also Xhost): $ xhost +local: Then, to direct the applications to the X server from chroot, set the $ echo $DISPLAY as the user that owns the X server to see the value of # export DISPLAY=:0 Without root privilegesChroot 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. PRootPRoot may be used to change the apparent root directory and use Fakechrootfakechroot 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 Unshareunshare(1), part of util-linux, can be used to create a new kernel namespace. This works with the usual chroot command. For example: $ unshare --map-root-user chroot ~/namespace /bin/sh Setprivsetpriv(1), part of util-linux, can be used to sandbox applications. The following example applies it to the non-graphical lynx web browser. Three different techniques are used:
First, we create a temporary path with TMPDIR=$HOME/Downloads/.lynx_tmp LYNX_TEMP_SPACE=$HOME/Downloads/.lynx_tmp \
setpriv --no-new-privs --landlock-access fs \
--landlock-rule path-beneath:read-file,execute:/usr \
--landlock-rule path-beneath:read-file:/etc/lynx.cfg \
--landlock-rule path-beneath:read-file:/etc/lynx.lss \
--landlock-rule path-beneath:read-file:/etc/resolv.conf \
--landlock-rule path-beneath:read-file:/etc/hosts \
--landlock-rule path-beneath:read-file:/etc/nsswitch.conf \
--landlock-rule path-beneath:read-file:/etc/passwd \
--landlock-rule path-beneath:read-file:/etc/ca-certificates/extracted \
--landlock-rule path-beneath:read-file,write-file,make-reg,make-dir:$HOME/Downloads \
lynx file://localhost/etc/passwd
As a test of the functionality, execute the above command in a terminal, enter the Lynx shortcut Tips and trickschroot detection
Troubleshootingarch-chroot: /path/to/new/root is not a mountpoint. This may have undesirable side effects.Upon executing ==> WARNING: /path/to/new/root is not a mountpoint. This may have undesirable side effects. See arch-chroot(8) for an explanation and an example of using bind mounting to make the chroot directory a mountpoint. See alsoRetrieved from "https://wiki.archlinux.org/index.php?title=Chroot&oldid=885586" |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |