0

Today I had to boot from a live USB to fix some files that broke my system. I followed what pretty much every guide says, so:

mount /dev/sd3 /mnt

mount /dev/sd1 /mnt/boot

chroot /mnt

(dont' know if I should also mount SWAP which is sda2 but I never saw this so I skipped it)

My question is: Why am I doing this? You can access a file from a live USB with cd, which I've done in the past by mistake. Is there anything wrong with this?

1
  • No its not. If you only want to access files and revert a few changes, chroot is not necessary. It is done if you want to make major changes, e.g. install programs, add users, change passwords. Because THEN it is simpler than manually defining the mount point as where to do the changes. – FelixJN Mar 3 at 10:20
2

By changing the root, you are effectively switching to the dead system. Somewhat akin to jump-starting a broken-down car with a working one.

Now that / refers to the dead system, every command plays along nicely. I like that my shell history now contains the contents of what I did to the dead system rather than the live system I booted. Also, I can use apt and many other tools which implicitly rely on / being the system to work on rather than /mnt. Many tools (e.g. journalctl, grub-install) have their own options to switch the root, but typing them in gets quite tedious and error-prone.

1

The chroot isn't strictly necessary for everything. If you just want to edit a file you can usually do so without it.

Some system tools expect the system to be laid out in a particular way. Things like package managers (rpm, yum, dpkg, apt, apk...). There's no way to tell these commands that your system is laid out in a different way.

So let's take a real example like Debian's dpkg. It keeps track of installed packages in /var/lib/dpkg. When you boot into a Live USB and mount your broken system to /mnt dpkg's library will now be in the wrong place... it will be in /mnt/var/lib/dpkg. You can't tell dpkg to use this, so you can't use dpkg to fix your broken system unless...

If you chroot /mnt you shift where everything is. So /mnt/var/lib/dpkg goes back to /var/lib/dpkg where it should be and so dpkg will function again correctly.


No you don't usually need to add the swap. The layman's description of swap is it uses some HD space to extend your RAM. For this type of activity you usually only need a fraction of the RAM you have. If you were about to run out of RAM you can add it at any time... So if SWAP is mounted from a file /var/swap you can either

swapon /mnt/var/swap

Before you chroot or

swapon /var/swap

After you chroot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.