System Repair: HowTo chroot

Many times, especially when messing around, we have found ourselves in the predicament of not being able to access the system to repair it, but the solution is easy: use the command chroot from any LiveCD / RepairCD.

In many places (forums, blogs ...) this command is mentioned and a "copy / paste" of the code is given, but my intention with this post is to explain those steps a little, in order to make good use of this tool, with knowledge of cause.

Introduction

The command chroot It is known as CHangeROOT, that is, a command that allows you to change the root of the system on which you are working. In other words: if you are from a LiveCD and you want everything you are working on the console to have an effect on the installed system, you must first make use of chroot.

The problem is that it is not enough to use chroot As such, before we must properly mount certain partitions.

HOW TO

First we need to start a terminal, either from another installed system (on another partition / disk) or from a LiveCD. IMPORTANT: the architecture of the LiveCD must match that of the system to be repaired (32 or 64 bits).
Once we are in the terminal we will start by identifying our partitions:
fdisk -l

  • With this command we will list all our partitions / disks. We must identify which is the target partition, where our system to repair is installed, from now on we will call it broken system.

For this example we will consider that our broken system is in / Dev / sda1 .

We go on to assemble the system. First we will create the folder where we are going to work and later we mount the partition where our broken system is located in that folder
mkdir /mnt/my_linux
mount /dev/sda1 /mnt/my_linux

If you have the folder / Home o / var or any other on another partition, you should mount it / s as follows:
mount /dev/sda2 /mnt/my_linux/var

  • NOTE: I have taken the / dev / sda2 partition for the / var folder as an example, let each one adjust the code to their characteristics.

Normally this would suffice if you simply need to edit files by hand, but if we want to execute some commands that configure the system, we need to mount certain special system folders: / Dev, / Proc/ sys.
mount -t proc proc /mnt/my_linux/proc
mount -t sysfs sys /mnt/my_linux/sys
mount -o bind /dev /mnt/my_linux/dev

  • With option -t we tell mount the type of "filesystem" we want to mount. It is necessary to specify it because of the special nature of the folders / Proc y / sys.
  • With option -o we specify the options of mount. The option connective it serves to "link". In UNIX all hardware devices are accessible through the folder / Dev, that's why we must mount our current / Dev in the folder where our broken system is now. As this folder is already mounted, it is only necessary to tell mount where it is originally mounted.

It is done this way so that chroot have access to these folders as if they were the broken system, although they must be from the current system (ex: the LiveCD session) since they are related to the state of the system, processes and hardware.

Now is the time to use chroot:
chroot /mnt/my_linux/ /bin/bash

  • The command is passed as arguments the path of the new root «/» (which in our case is / mnt / my_linux) and the console you want to use (in this case we have opted for the well-known bash, found in / bin / bash). If we do not specify the console, we will find ourselves before a somewhat archaic command interpreter (it does not fill when pressing the tab, etc).

Now we can use the console as if we had the root session started on our broken system (edit files, check scripts, install / uninstall packages ...). WARNING! For the changes made to take effect, you must unmount the filesystem after exiting chrootLook at the example below.

More information in https://wiki.archlinux.org/index.php/Change_Root (more than recommended reading).

Usage example: restore GRUB2

One of the most widespread uses of chroot It is as a tool to repair the GRUB. Since if the grub breaks, it is practically impossible to boot our system to fix it.

NOTICE: this little tutorial is a mere example, it works on various distributions derived from Debian, Ubuntu and openSUSE among others. Even so, check the documentation of your distribution, since in many the command is not found update-grub.
# NOTE: these commands run once inside chroot.update-grub
grub-install /dev/sda

  • With update-grub We update GRUB2's input menu, thus adding any missing inputs. Later we reinstall GRUB on our disk, since it has been damaged.

In this case I have taken / Dev / sda like the album where we have our system, this must be adapted to your case.

Our GRUB should already be repaired, so we must exit chroot, unmount the filesystem (IMPORTANT) and reboot for the changes to take effect. If we forget to unmount the filesystem, it is possible that rebooting the files will not unmount correctly and therefore some changes will not take effect.
# we leave chrootexit
# unmount the filesystem and rebootumount /mnt/my_linux/dev
umount /mnt/my_linux/sys
umount /mnt/my_linux/proc
umount /mnt/my_linux
reboot

And that's all. I hope you enjoy it and that it helps you. A greeting!


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Leper_Ivan said

    I can use this .. At some point I have to use it.

  2.   mario said

    Have any of you used Chakra Benz? If so, could you tell me if
    is it easy for the common user? I use SolydK which is very simple but
    I understand that Chakra is pure KDE and it interests me.

    1.    wow said

      Yes, chakra is very good and more or less easy, the thing is that to install / uninstall packages you have to do it with the console, since they are still working on a graphical package manager. Try it because it is worth it. It uses the pacman package manager, inherited from Archlinux, but beware, it does not share repositories with arch and it is NOT compatible with them. If you want to learn more about pacman, take a look at the arch wiki https://wiki.archlinux.org/index.php/Pacman_%28Espa%C3%B1ol%29

      1.    izzyvp said

        Oktopi is already stable and works quite well for an average user, with that you don't need the terminal to use pacman.

  3.   x11tete11x said

    I hope this post will be visited by many, then they start their heads reinstalling for anything when with a chroot you can repair the system almost always, the chroot enables you to a lot of possibilities, including installing Gentoo xD hahaha

  4.   moony said

    nice, thanks ... I never gave it a ball because some live-cd for repair seems to me that they raise it by default, but now it's clear, thanks again.

  5.   Lolo said

    To complete it, it would be good to explain how to mount LVM partitions, encrypted partitions and RAID systems.

    1.    wow said

      mmm I haven't touched LVM and RAID in a long time, but could you inform me a bit to complete this… thanks for the tip!

      1.    Lolo said

        Well the truth is that I could use great.

        It would be very appreciated.

        1.    wow said

          take a look at this wiki, it's pretty cool http://wiki.bandaancha.st/RAID_y_LVM_en_Linux

  6.   Modem said

    If only this topic had existed a few weeks later, it turns out that I had a problem with my system and I read and read and everywhere I found chroot and blablabla but it did not work for me based on that recommendation of the live that is the same as your system , very important because I use x64, the other is to mount the system partitions, because in my case when I was applying chroot and mounting, I was throwing a command and it just didn't recognize the command.

    This topic will go to my "info compilation" files

  7.   cookie said

    An Arch LiveCD (with this tool) is all that is needed to fix your system when you break it.

    1.    RAW-Basic said

      Exactly! .. ..it's all that is needed .. even to help someone with their W $ who decided not to start ..

      I always have with me a 1gb pendrive with Arch live ... and chroot is all that is necessary so that under any problem, in 10 min you have everything in order without being alarmed .. 😉

    2.    izzyvp said

      You just have to know how to do it.

    3.    Paul said

      Hello, notice that I have a problem with centos, it sends me kernel panic etc. And I have not been able to solve it, I have my website and database there. I don't know what else to do.

  8.   jony127 said

    Good post, I had heard some chroot in passing but I didn't really know how it was used, of course I had never needed it and that's why I hadn't read something in particular. Now with your post explained very clearly, I know how to use it in case.

    Thank you very much and by the way, long live Led Zeppelin hehe.

  9.   kamala said

    thanks, i finally got out of grub rescue nightmare

    in fact I followed all the steps except umount / mnt / my_linux, because it told me it was busy, and I fuser -km, but not even that, and bounce ...

    I hope it is not important….

    thanks, again…
    all the best

  10.   Jose Antonio said

    Although the article is already a couple of years old, I want to thank the author for his generosity in sharing it is a great article because this weekend it saved my life. Involuntarily I loaded a server installing a package and when restarting it did not load. After three days of trying to fix it and reading different articles, I managed to get here by chance and was finally able to reinstall grub and fix the server.

    Thank you very much!

    1.    wow said

      I'm glad it helped you. Thanks for the comment!

  11.   Helio said

    Hello, I know that this post years ago, this great thanks for sharing it. And I had a doubt, in my case it damaged the system and also the network drivers, so I followed the steps but it does not allow me to download packages, is there any way to link the live network with the terminal

    1.    wow said

      I don't really understand your problem… When you log in with the LiveCD, the kernel that loads is the one from the LiveCD, therefore you should be able to configure an internet connection perfectly. Once the connection is made, try to do the chroot to see if you continue to maintain it, if not, you may have to configure it again, but the damaged kernel should not affect the chroot session.
      If you don't specify more, I can tell you ...

  12.   dach said

    Excellent Post… one of the few useful articles found on the Internet.
    Thank you very much for the contribution.

  13.   zopeck said

    Hello, excellent tutorial! Superbly explained, for us mere mortals!

    I have a question:
    When you mentioned that in case of needing to install packages, perform system updates or other special activity, and require mounting the / dev / proc and / sys folders, it was not clear to me if these folders are in the Live system with which we started the equipment to be repaired, or if they are in the root partition mounted first.

    Thank you.

  14.   Orlando said

    THANK YOU SO MUCH !!!!