chroot in linux

Chroot is a very useful feature in Linux systems: you can update a Linux installation on your PC not within that installation, as usually we can do, but from another installation.

I.g. if you have Linux1 on /dev/sda2 and Linux2 on /dev/sda3 and you cannot login into Linux2, you can try to solve the problem by doing an update of Linux2 from Linux1. Using chroot.

There are very good guides on the web, but, let us repeat the essential steps

  • first of all be sure of the correct name of the partition you want chroot in (i.g. /dev/sda3, or /dev/sdb2)
  • create a /mnt where you will mount that installation, i.g. /mnt/chroot
  • mount that installation in /mnt/chroot: sudo mount -t ext4 /dev/sda3 /mnt/chroot (note that etx4 is if that is the filesystem, but it could be btrfs as well or another filesystem)
  • mount efi partion (if you have efi): sudo mount -t vfat /dev/sda1 /mnt/chroot/boot/efi/
  • mount virtual folders of that installation: for i in dev proc sys run; do sudo mount --bind /$i /mnt/chroot/$i; done
  • Do the chroot: sudo chroot /mnt/chroot

What you do now, within the terminal where you did the chroot is what you could do if you were in that installation (Linux2).
Typically you should do these commands

  • apt update (without sudo before apt: you are already acting as superuser)
  • apt upgrade (or apt full-upgrade)
  • update-grub

Afterwards you can exit with:

  • for i in /dev /proc /sys /run /; do sudo umount -l /mnt/chroot/$i; done
  • and: sudo umount /dev/sda2 /mnt/chroot/boot/efi/
  • and: sudo umount /dev/sda3 /mnt(chroot

Note that these last two commands could be not necessary.

Lascia un commento

Il tuo indirizzo email non sarĂ  pubblicato. I campi obbligatori sono contrassegnati *