Change Root
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
1 Requirements
- You'll need to boot from another working GNU environment (e.g. from a LiveCD or USB flash media, or from another installed GNU/Linux distribution).
- Root privileges are required in order to chroot.
- Be sure that the architecture of the GNU 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.
2 Mount the partitions
The root partition of the GNU/Linux system that you're trying to chroot into needs to be mounted. To find out the device name assigned by the kernel, run:
# lsblk /dev/sda
Then create a directory where you would like to mount this partition and mount it:
# mkdir /mnt/parabola # mount /dev/sda3 /mnt/parabola
Next, if you have separate partitions for other parts of your system (e.g. /boot, /home, /var, etc), you should mount them, as well:
# mount /dev/sda1 /mnt/parabola/boot/ # mount /dev/sdb5 /mnt/parabola/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 GNU environment knows all mounted partitions, it can safely unmount them during shutdown.
3 Change root
Mount the temporary filesystems:
# cd /mnt/parabola # 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:
# cp -L /etc/resolv.conf etc/resolv.conf
Now chroot into your installed system and define your shell:
# chroot . /bin/bash
Optionally, to source your Bash configuration (~/.bashrc and /etc/bash.bashrc), run:
# source ~/.bashrc # source /etc/profile
Optionally, create a unique prompt to be able to differentiate your chroot environment:
# export PS1="(chroot) $PS1"
4 Run graphical chrooted applications
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 run the following from a terminal:
# xhost +
Then, to direct the applications to your X server, run:
# export DISPLAY=:0.0
5 Perform system maintenance
At this point you can perform whatever system maintenance you require inside the chroot environment. A few common examples are:
- Reinstall the bootloader.
- Rebuild your initramfs image.
- Upgrade or downgrade packages.
- Reset a forgotten password.
6 Exit the chroot environment
When you're finished with system maintenance, exit the chroot:
# exit
Then unmount the temporary filesystems and any mounted devices:
# umount {proc,sys,dev,boot,[...],}
Finally, attempt to unmount your root partition:
# cd .. # umount parabola/
- A program was left running inside of the chroot.
- Or, more frequently, a sub-mount still exists (e.g. /mnt/parabola/boot within /mnt/parabola). Check with lsblk to see if there are any mountpoints left:
-
lsblk /dev/sda
- If you are still unable to unmount a partition, use the --force option:
-
# umount -f /mnt
After this, you will be able to safely reboot.