Chrooting to rescue your Linux System - Printable Version +- XeroLinux Community Forums (https://forum.xerolinux.xyz) +-- Forum: Linux (https://forum.xerolinux.xyz/forum-24.html) +--- Forum: Linux University (https://forum.xerolinux.xyz/forum-31.html) +--- Thread: Chrooting to rescue your Linux System (/thread-163.html) |
Chrooting to rescue your Linux System - DarkXero - 08-25-2022 Changing 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 for such tasks as reinstalling GRUB or resetting a forgotten password. Changing root is often done from from a LiveCD or LiveUSB into a mounted partition that contains an installed system. Requirements
Mounting the device The device or partition with the Linux system on it will need to be mounted. To discover the kernel name of the storage device name, type:
Create a directory where you would like to mount the device or partition, then mount it:
For LUKS encrypted devices, mount won’t work, use udisksctl instead, first unlock the device (all commands as regular user):
Get the device mapper name:
then mount the returned mapper:
Changing Root Mount the temporary filesystems:
Mount other parts of your filesystem (e.g. /boot, /var, /usr…) that reside on separate partitions but which you need access to. For example:
It’s possible to mount filesystems after you’ve chrooted, but it’s more convenient to do so beforehand. The reasoning for this is you’ll have to unmount the temporary filesystems after you exit a 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. If you’ve setup your network and want to use it in the chroot environment, copy over your DNS servers so that you will be connected to the network:
Now chroot to your installed device or partition and define your shell:
If you see the error, chroot: cannot run command '/bin/bash': Exec format error it is likely the two architectures do not match.If you’ll be doing anything with GRUB inside the chroot environment, you’ll need to be sure your /etc/mtab is up-to-date:
If you use bash, your root $HOME/.bashrc will be sourced on login provided your ~/.bash_profile specifies it (source ~/.bashrc). To source your chrooted, global bash configuration do:
If your bash configuration doesn’t use a unique prompt, consider creating one to be able to differentiate your chroot environment:
Perform System Maintenance At this point you can perform whatever system maintenance you require inside the chroot environment, some common examples being:
With this information, you are able to arch-chroot, and to be able to do that you need to have root permissions, so type the following command: Then type the following:
Now you’ve chrooted into your installed system, and you are able to access your files, install packages, or alter scripts to rescue your system. To make sure arch-chroot is working check after your users home folder ls /home that should give your username from the installed system.Exiting chroot When you’re finished with system maintenance, exit the chroot shell:
Then unmount the temporary filesystems and any mounted devices:
Finally attempt to unmount your hard drive:
If you get an error saying that '/mnt' (or any other partition) is busy, this can mean one of two things:
In the latter case, unmount the sub-mount mount point first. To get a reminder of all the current mount points, run mount with no parameters. If you still are unable to unmount a partition, use the force option:
After this you will be able to safely reboot. |