Tuxation

Chrooting into a Linux environment

chrooting is a very important skill that any *nix administrator must know. It's essentially what allows one to repair an existing Linux environment by entering it from another environment. The process is actually relatively simple, and useful for doing tasks such as reinstalling GRUB, repairing a broken kernel, resetting the root password, etc.

What you'll need

Since you need both an existing Linux environment to enter into, and another one which you will boot off of, you should have some sort of LiveCD that contains a Linux environment capable of chrooting. If you don't know whether a LiveCD you possess is capable of doing this, feel free to try, otherwise use an Ubuntu or Gentoo CD (these have been proven to work very well with these instructions).

Procedure

First of all, you need to boot off the LiveCD. This may require adjusting the boot order of your BIOS in order to give priority to your CD/DVD reader, but every BIOS is different, and so generic instructions cannot be given here.

Once booted into the LiveCD, you need to obtain some kind of shell, and the next thing you need to do is get root access. Running

whoami

at the prompt should report back whether you are root or another user. If so, you need to obtain root. On Ubuntu, this can be accomplished by entering 'sudo su'. Some other distros simply require 'su'. Check your distro's documentation if unsure.

Once you've gained root access, it's time to start mounting the Linux environment that you want to enter into. This requires some knowledge of the layout of your hard drive partitions. Fortunately, most Linux distros tend not to distribute files on too many partitions, making the mounting process easier and simpler for you. To start off, one of the best things you can do is print off your partition table by running

fdisk -l

That will tell you all the partitions on all the hard drives on the current machine. There's most likely at least one linux-swap partition, and almost certainly there should be at least one Linux partition. If there's more than one Linux partition, you'll have to make a guess as to which one is your root partition, from which all the other data partitions are mounted onto.

The next step to mounting is to prepare a mount point, which is basically a directory which will point to the root partition of the Linux environment you want to enter. Type the following:

ls /mnt

If nothing is returned, go ahead and use /mnt as the mount point. If there are other directories in it, then feel free to create your own directory in it for your root partition:

mkdir /mnt/mylinux

Now it's time to mount your root partition. If you know the filesystem type, please specify it with '-t type', where 'type' is the filesystem type. While this is not absolutely necessary, it's better to explicitly state it and reduces the chance of a bad mount. Now, mount the partition:

mount /dev/sda4 /mnt

Replace '/dev/sda4' with the partition device that you determined was your root partition, and replace '/mnt' with the mount point you decided on. At this point, if no errors were reported, your root partition should be mounted successfully. If this is the only Linux partition on your hard drive, then you're all set to enter the environment. If you have more, continue mounting them in the same manner. If you're not sure again which partitions correspond to which mount points, run the following command:

cat /etc/fstab

It will list all the partitions to be mounted at boot time. You can then manually mount them and then proceed with entering the environment.

Now you need to mount virtual filesystems. Specifically, this includes /dev and /proc. These are relatively simple:

mount -t proc none /mnt/proc
mount -o bind /dev /mnt/dev

Again, if your mount point was /mnt/mylinux, adjust accordingly.

Finally, you're ready to enter the environment. Unsurprisingly, the chroot command accomplishes this:

chroot /mnt

You should now be in the new environment. To make it easier to remember that this is a chroot shell and not your regular shell, edit your PS1 variable:

export PS1="(chroot) $PS1"

To operate in the new environment, it would be a good idea to update your environment variables. You can do this by running

source /etc/profile

That will load the variable defaults specified in /etc/profile.

Conclusion

You're all set. You can now repair/reinstall whatever needs fixing and then reboot when you're finished.