Here are some resources:
"Changing root" or "chrooting" is a method for zooming in on part of your filesystem, so that, for example,
/path will refer to what was formerly accessible at
/mnt/path. The "root" in the expression "chroot" refers to the root filesystem
/, not to the root user. (Though typically you will need root user privileges in order to chroot.)
All the steps in this guide will have to be performed as root user.
We suppose your hard disk is at /dev/sda1 and its filetype is ext3.
If you don't know the location and filetype of your disk, read the
Be sure that the architectures of the system you booted into (e.g. it's a
32bit LiveCD) and the system you wish to enter (e.g. it's a 32bit
installation on your hard drive) match. You can determine the
architecture you booted with using
Be sure any kernel modules you need have been loaded.
Set up your network if you need it (e.g., to install updated packages).
Initialize your swap partition if necessary (e.g.,
Performing the chroot
mount -t ext3 /dev/sda1 /mnt
mount -t proc proc /mnt/proc
mount -t sysfs sys /mnt/sys
mount -o bind /dev /mnt/dev
/boot directory is on a different partition from your
/, and you want to manipulate files on it (e.g., if you'll be working with GRUB, performing a kernel upgrade, etc.), you'll also need to mount that partition. If it's at /dev/sda2 and its filetype is ext2, then do:
mount -t ext2 /dev/sda2 /mnt/boot
Similarly for any other parts of your filesystem (
/usr) that reside on separate partitions but which you need access to. Generally when you're chrooting to fix something you won't need access to /home, so you don't need to bother with it.
(It's also possible to mount filesystems after you've chrooted, but it's smarter to do so beforehand. The reason is that when you do it after, the outside/kernel environment won't know about the mounted filesystems, so if you forget to umount them before exiting the chroot, the system won't know to umount them when it shuts down, either. This could damage those filesystems.)
If you've setup your network and want to use it in the chrooted system, copy over
/etc/resolv.conf so that you'll be able to resolve domain names:
cp -L /etc/resolv.conf /mnt/etc/resolv.conf
Now you're ready to move into the mounted filesystem:
chroot /mnt /bin/bash
(If this returns an error
chroot: cannot run command '/bin/bash': Exec format error, this usually indicates that you booted with one architecture (e.g. 32bit) and are trying to chroot into another (e.g. x86_64). The solution is to use a LiveCD which has the same architecture as the system you want to chroot into.)
At this point, you're still running the kernel you booted with, but all paths
/path will refer to what used to be
If you'll be doing anything with GRUB, you'll need to be sure your
/etc/mtab file is up-to-date:
grep -v rootfs /proc/mounts > /etc/mtab
It might also be helpful at this point to do:
export PS1="(chroot) $PS1" # add a reminder to your prompt
Do your dirty work
At this point, you can perform whatever troubleshooting you need to do:
- resintall GRUB to your disk's MBR
- reset a forgotten password
- perform a kernel upgrade (or downgrade)
- rebuild your initramdisk
- fix your /etc/fstab
- reinstall packages using your package manager
When you're finished, ensure that all running programs have stopped. Then exit the chroot:
Now unmount all the partitions you mounted:
umount /mnt/boot # if you mounted this or any other separate partitions
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:
A program was left running inside of the chroot.
Or more frequently: a mount point still exists on this mount. For example, /mnt/usr is still mounted when trying to unmount /mnt.
In the latter case, simply unmount the offending mount point first. To get a reminder of all the current mount points, run
mount with no parameters.