Skip to content
System

Chroot under Linux, repair a system from a live USB

Avatar illustré de Julien Macé 4 min read
Tux utilise un live USB pour réparer un système Linux installé sur un disque
Tux utilise un live USB pour réparer un système Linux installé sur un disque

A Debian or Ubuntu that no longer boots does not necessarily deserve a reinstallation. From a live USB, chroot opens a shell in the system installed on the disk to complete package repairs, change a password, or restore GRUB.

The risk occurs before chroot. Identify the correct root, mount the pseudo-filesystems, initiate the repair related to the incident, and verify the result before restarting.

Identify the installed root

Do not start from an old example like /dev/sda2. On a recent laptop, the root may be /dev/nvme0n1p2, and the order of disks changes as soon as a USB SSD is connected.

From the live USB, display the filesystems, UUIDs, and known mount points.

sudo lsblk -f
sudo blkid

The root partition generally contains etc, usr, var, and often home. A small vfat partition of a few hundred MB likely corresponds to the EFI.

Mount a candidate under /mnt, check its contents, and keep this mount if the root is correct.

sudo mount /dev/nvme0n1p2 /mnt
ls /mnt

If the content does not match, unmount before testing another partition.

sudo umount /mnt

On a LUKS encrypted disk or an LVM installation, first unlock the volume. Then use the path displayed by lsblk -f, for example /dev/mapper/vg0-root.

Mount the necessary pseudo-filesystems

If the root is not mounted yet, mount it under /mnt. The following lines assume that /dev/nvme0n1p2 is the root of the system to repair.

sudo mount /dev/nvme0n1p2 /mnt

Next, create the mount points and then attach the live pseudo-filesystems. /dev, /proc, /sys, and /run provide the chroot with devices, processes, kernel information, and some sockets useful for repair tools.

sudo mkdir -p /mnt/dev/pts /mnt/proc /mnt/sys /mnt/run
sudo mount --bind /dev /mnt/dev
sudo mount --bind /dev/pts /mnt/dev/pts
sudo mount -t proc /proc /mnt/proc
sudo mount -t sysfs /sys /mnt/sys
sudo mount --bind /run /mnt/run

Check the mounts before entering the chroot. mountpoint is run point by point, as it expects a path to verify.

findmnt -R /mnt
mountpoint /mnt/dev
mountpoint /mnt/dev/pts
mountpoint /mnt/proc
mountpoint /mnt/sys
mountpoint /mnt/run

If the repair affects GRUB on a UEFI machine, also mount the EFI partition to the path used by the installation. First, check this path in /mnt/etc/fstab.

grep -E 'vfat|/boot/efi' /mnt/etc/fstab
sudo mkdir -p /mnt/boot/efi
sudo mount /dev/nvme0n1p1 /mnt/boot/efi

For a simple package repair, keep the EFI out of the mount. Only add it if the repair writes to the bootloader or its EFI files.

Enter the chroot and check the target

Now open a shell in the mounted root.

sudo chroot /mnt /bin/bash

The prompt may change, but this does not prove that you are working on the correct system. Check the distribution, the visible root, and the available space.

cat /etc/os-release
df -h /

chroot changes the visible root for the shell launched. It does not start the kernel or the services of the installed system, so some systemctl commands may fail in an otherwise healthy chroot.

If apt needs to download packages, also check DNS resolution from the chroot. On Debian, test the Debian repository. On Ubuntu, adapt it with the Ubuntu mirror configured in /etc/apt/sources.list.

getent hosts deb.debian.org

Do not blindly replace /etc/resolv.conf. On systems with systemd-resolved, this file may be a symbolic link, and a forceful replacement can sometimes leave a dirty state upon reboot.

Repair only the targeted incident

In the chroot, initiate the repair corresponding to the symptom. For a Debian or Ubuntu installation interrupted during an update, start by completing the configuration of packages, then fix the dependencies.

dpkg --configure -a
apt --fix-broken install
dpkg --audit

dpkg --audit should return empty or list only one issue still to address. If it indicates a specific package, keep the package name and resolve this issue before exiting.

To reset the root password, run passwd in the chroot. Then check that /etc/shadow has indeed been modified at the time of the intervention.

passwd
stat /etc/shadow

For GRUB, do not copy a BIOS command on a UEFI machine. The boot type, the target disk, and the mount of /boot/efi determine the grub-install command. If this point is unclear, stop at the diagnosis rather than writing the bootloader in the wrong place.

Check and exit cleanly

Before exiting, verify the repair with a command related to the incident. For packages, dpkg --audit is often sufficient. For a password, stat /etc/shadow at least confirms that the file has changed.

  • Review the last destructive command run in the chroot.
  • Keep the exact error message if a repair fails.
  • Exit the chroot with exit before unmounting.
  • Unmount /mnt before removing the live USB.
exit
sudo umount -R /mnt
sudo reboot

If umount -R /mnt indicates that a point is busy, check your current directory and close any open file explorers from the live environment.

pwd
lsof +f -- /mnt

Only force the unmount after understanding the blocking process. Forcing too soon can hide an ongoing write on the repaired system.

Know the limits of chroot

A chroot helps when the root mounts and the problem involves files, packages, a password, or a correctly prepared bootloader. It does not repair a disk that returns hardware errors.

If the partition does not mount, start with kernel messages and SMART status before any application repair.

dmesg -T | tail -n 50
sudo smartctl -a /dev/nvme0

On a remote server, use an out-of-band console or a backup VM. The host’s console is safer than a regular SSH session, as a network repair or unmount can cut the only active connection.

The GNU Coreutils documentation describes the behavior of chroot. For the exact mounts of a Debian or Ubuntu installation, also read your distribution’s documentation before writing to the bootloader.

Share Facebook X WhatsApp LinkedIn Pinterest Reddit Email