How to chroot


Pinoy Techie
Sometimes you find yourself in a situation where you can not load your kernel or otherwise normally boot your system but you know if you could just get into the system and make a few configuration changes/repair packages you could fix it.

This is where the chroot comes into play, it is a Unix system call with a corresponding userspace utility to tell your currently running kernel to treat a path you specify as the root file system. This will allow you to boot off of a live disk and repair your system almost as if you where able to boot it normally in the first place.

1: First boot off of your live disk and open a shell (make sure the live disk has the exact same architecture as the installed system and uses the same word size. So for example if you installed an amd64 (64 bit) distro but are using an i686 (32 bit) live disk you will run into big trouble and won't be able to finish the chroot!)

2: You need to find your root partition which can be a little tricky but once you find it things get much easier.
-If you are using an MS-DOS partition table you can run:
~$ fdisk -l
to list all the disks and partitions on your system
-If you are using a GPT partition table (sometimes called EFI/UEFI partition table) you can use gdisk in the same way to list your partitions and disks.

This is the fun part, poke around by mounting a partition and seeing what is on it like so:
~$ mount /dev/sda3 /mnt
~$ ls /mnt

if you don't see a directory structure including directories such as; etc, dev, var, home, sys, proc
you probably haven't found your root partition so unmount and move on to the next until you find it.
~$ umount /mnt

3: By the time you get to this step, you should have already located your root partition and have it mounted to /mnt
Next, have a look at /mnt/etc/fstab:
~$ cat /mnt/etc/fstab

This will list all your static file system mount points, unless you took the time to create your own partition layout most guided installers only have one main partition for your root file system and maybe have /boot on a separate partition (and if you did create your own partition layout I bet you are kicking yourself for not writing it down!)
Make sure these are all mounted, taking your /mnt directory into consideration.
For example, lets pretend /boot is on its own partition, it is on the first partition of our first SATA disk.
~$ mount /dev/sda1 /mnt/boot

Some distributions use a UUID to identify partitions instead of the traditional device nodes. In that case use the --uuid or -U option with the UUID listed in fstab like so:
~$ mount -U 39d65932-4c90-4715-be8c-242224a1aaaa /mnt/boot

4: We aren't done mounting file systems yet.
Mount a proc file system:
~$ mount -t proc none /mnt/proc
Mount a sys file system:
~$ mount -t sysfs none /mnt/sys
Bind your current device nodes:
~$ mount --rbind /dev /mnt/dev

5: You're just about ready to chroot.
Change directories into the area you prepared, this technically isn't required but is a nice little safe guard.
~$ cd /mnt
Copy DNS resolver information that is created dynamically each boot:
~$ cp -av /etc/resolv.conf /mnt/etc/resolv.conf
Finally, preform the chroot:
~$ chroot /mnt /bin/bash
You may need to set environment variables specific to your installed system:
~$ source /etc/profile

6: Now you can execute commands as if you had booted your system normally, including using your package manager. Its probably a good idea to script this whole process, and tuck it in a nice little file on the root partition of your hard drive so that next time you only ever need to remember your root partition, mount it, and execute the script, don't forget to set the executable bit if you do.

7: Cleaning up: you (hopefully) fixed your system and are ready to test it out. Execute the following commands to exit the chroot, cleanly unmount the file systems, and reboot.
~$ sync
~$ exit
~$ cd
~$ umount -a # this will complain about a few file systems that are still in use, don't worry we'll fix that
~$ umount -l /mnt/dev
~$ umount -l /mnt/proc
~$ umount -l /mnt/sys
~$ umount /mnt
~$ shutdown -r now
Top Bottom