16.1. Selecting a Recovery Environment
The first decision is not which command to run but where to run it. A running system with a working SSH session is the cheapest environment; a rescue target on the machine's own console is next; installer rescue mode and live media are for failures that prevent the installed system from starting at all. Each step away from the running system gives up convenience and services, and each gives back a cleaner view of the storage and configuration underneath.
The walkthrough uses one Debian 13 machine with disposable secondary disks:
| Host | Role | Practice storage |
|---|---|---|
recovery1 |
installed Debian system being inspected and repaired | /dev/vdb (4 GiB) for the LVM data volume, /dev/vdc (4 GiB) for a disposable recovery copy, /dev/vdd (1 GiB) for a LUKS container, /dev/vde (2 GiB) for an independent backup |
| rescue media | installer rescue mode or a Debian live image, booted on the machine's own console | none; it supplies the tools when recovery1 cannot boot |
The recovery copy on /dev/vdc is a standby root built from the installed
system. It gives the exercises a safe target for chroot, initramfs, and GRUB
repairs without touching the running root. It is not a substitute for bootable
installation media: when the boot loader or the firmware entry is the failure,
the machine must be started from separate media or from a rescue target that
the firmware can still reach. The data volume on /dev/vdb, the LUKS container
on /dev/vdd, and the backup disk on /dev/vde are disposable. Every byte on
them is erased during the exercises.
Commands entered in john's ordinary shell begin with $, and commands entered
in a root shell begin with #; the prompt marker is not typed. john's account
and sudo access were established during installation (Chapter
2) and are described in Chapter
5. Use $ sudo -i to enter a root shell and
exit to leave it; a single elevated command is written $ sudo command. The
first sudo command in a session normally asks for john's password, which is not
echoed. A rescue shell and the installer's shell are already root, so their
commands use # without sudo.
Create the reader workspace before the first example. Recovery evidence,
archives, and the practice images stay under it; the system paths used later -
/etc, /var/log, /srv, /mnt, and the device nodes - remain where the
system expects them:
$ mkdir -p ~/debian-handbook/recovery
The command prints nothing. Make that directory the working directory for the rest of the chapter:
$ cd ~/debian-handbook/recovery
The change of directory is silent. Relative names such as storage-layout.txt,
full.img, and appdata-before-repair.sha256 below refer to this directory;
commands that run in a root shell either use absolute paths or change back
before using a relative one. The tools used in the chapter come from packages
beyond a base installation. Install them with the ordinary interactive
procedure from Chapter 6, answering apt's
confirmation prompt:
$ sudo apt install lvm2 cryptsetup cryptsetup-initramfs initramfs-tools grub-pc-bin grub-efi-amd64-bin grub2-common parted gdisk e2fsprogs xfsprogs lsof psmisc rsync acl attr debsums dosfstools sqlite3
lvm2 provides pvcreate, vgchange, and lvs; cryptsetup provides the
LUKS tools and the initramfs hook; grub-pc-bin and grub-efi-amd64-bin
provide the legacy and UEFI GRUB platforms; e2fsprogs provides e2fsck and
mkfs.ext4; lsof and psmisc show open files and lock holders; acl and
attr preserve extended metadata; dosfstools creates the ESP; and sqlite3
gives the data-volume lab a small application database. apt prints its plan
and progress, which are not repeated here.
Record the tool and kernel versions with the recovery notes, because a procedure that depends on a GRUB or cryptsetup behavior should say which version produced it:
$ dpkg-query -W -f='${Package} ${Version}\n' lvm2 cryptsetup initramfs-tools grub2-common
cryptsetup 2:2.7.5-2
grub2-common 2.12-9+deb13u2
initramfs-tools 0.148.4
lvm2 2.03.31-2
The output is one line per package; the exact versions differ as Debian
receives updates. The chapter's examples were recorded on Debian 13.5 with
kernel 6.12.86+deb13-amd64. Device names, UUIDs, key fingerprints, process
IDs, and timestamps in the captured output belong to one run and will change
when the sequence is repeated.
The last preparation creates the encrypted practice container on /dev/vdd,
which Section 16.2 unlocks. Its credential is a key file rather than an
interactive passphrase, so the unlocking exercises can be repeated without
retyping a password; Chapter 13 covers key slots,
header backups, and the passphrase form. The key file holds a practice
passphrase in a root-only location. cryptsetup luksFormat destroys every
byte on the device it formats, so confirm that /dev/vdd is the 1 GiB
practice disk before continuing:
$ sudo -i
# install -d -m 700 /etc/luks
# printf 'handbook-luks-passphrase\n' > /etc/luks/recovery.key
# chmod 600 /etc/luks/recovery.key
# cryptsetup luksFormat --key-file=/etc/luks/recovery.key --label recoverycrypt /dev/vdd
WARNING!
========
This will overwrite data on /dev/vdd irrevocably.
Are you sure? (Type 'yes' in capital letters): YES
# exit
The first three commands are silent: install -d -m 700 /etc/luks creates the
key directory with root-only permissions, the printf writes the practice
passphrase into recovery.key, and chmod 600 restricts the file to root.
luksFormat prints its warning, waits for the uppercase answer, and writes
the LUKS2 header and the recoverycrypt label that the later blkid and
lsblk listings show; it is silent once the answer is accepted. The exit
returns to john's ordinary shell. To rebuild the container from scratch
later, wipe the disk with wipefs -a /dev/vdd and repeat the format command.
Figure 16.1 summarizes the order this chapter follows. The flow is conceptual: the exact branch depends on what can still be reached.
Figure 16.1. The recovery environment is chosen from what is still reachable; preservation and validation come before any destructive repair.
16.1.1. Previous Kernels, Rescue Targets, and Emergency Mode
The running kernel names the image that is currently booted, but the useful question is which kernels are installed and which one GRUB will select. Start with the running release:
$ uname -r
6.12.86+deb13-amd64
The release string combines the upstream version, Debian's ABI suffix, and the
architecture. The module directory under /lib/modules must match it exactly;
after a kernel upgrade the old modules remain for the old kernel and are never
mixed with the new one. List the installed kernel and initramfs pairs:
$ ls -1 /boot/vmlinuz-* /boot/initrd.img-*
/boot/initrd.img-6.12.86+deb13-amd64
/boot/vmlinuz-6.12.86+deb13-amd64
One pair is present on a freshly installed machine; a system that has received
kernel updates keeps the older pair as well. The vmlinuz file is the
compressed kernel, and the matching initrd.img is the initramfs that the boot
loader hands to the kernel. If the newest kernel fails to start, an older pair
is a complete fallback, not a partial workaround.
The default target tells you how far a normal boot is supposed to go:
$ systemctl get-default
graphical.target
A server normally uses multi-user.target; a workstation with a display
manager uses graphical.target. The target does not decide which services are
enabled, only which synchronization point the boot transaction reaches. If a
graphical login fails but the machine is otherwise healthy, changing the
default to multi-user.target is a reversible way to get a text console; it
does not repair the display manager. The target model and its service
dependencies are described in Chapter 8.
GRUB's menu is generated from /etc/default/grub and the fragments under
/etc/grub.d; read the effective timeout and default rather than assuming
they are the package defaults:
$ grep -E "^(GRUB_DEFAULT|GRUB_TIMEOUT)" /etc/default/grub
GRUB_DEFAULT=0
GRUB_TIMEOUT=5
At boot, the menu offers the normal entry and an Advanced options for Debian
GNU/Linux submenu. The submenu contains the normal kernel, a recovery-mode
entry, and older kernels when they are installed. The recovery entry adds
single and dis_ucode_ldr to the command line, which brings systemd up to
rescue.target rather than the default target. Selecting the previous kernel
is often enough to distinguish a kernel regression from a configuration
failure; if the previous kernel starts, the storage and most of the
configuration are intact and the problem is in the new kernel, its initramfs,
or a module it loads.
The generated menu is large and machine-specific. The first entries show the shape:
$ sudo grep -E "^(menuentry|submenu)" /boot/grub/grub.cfg | head -6
menuentry 'Debian GNU/Linux' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-be225267-eaf4-410c-9ffa-5cd32b221ce0' {
submenu 'Advanced options for Debian GNU/Linux' $menuentry_id_option 'gnulinux-advanced-be225267-eaf4-410c-9ffa-5cd32b221ce0' {
grub.cfg is mode 0600 on Debian, so an ordinary user cannot read it; use
sudo for inspection and never edit this generated file by hand. The UUID in
the menu identifier is the root filesystem's UUID, not a kernel version. The
menu is displayed only for GRUB_TIMEOUT seconds unless a key is held or a
previous boot was interrupted; the exact key depends on the firmware and GRUB
build, so press Esc repeatedly during the firmware handoff if the menu stays
hidden.
Once the boot loader has loaded a kernel, systemd can still be told to stop at
a diagnostic target. rescue.target pulls in the base system and the system
mounts, then starts a rescue shell with almost no services. emergency.target
is smaller still: it starts a shell with the system manager, but it does not
pull in other services or mounts, and the root filesystem may be read-only.
systemd.special(7) describes both and notes that rescue mode is the better
choice when the local filesystems are needed, while emergency mode is for the
case where even the normal mounts are failing. The targets can be inspected
without entering them:
$ systemctl status rescue.target --no-pager
○ rescue.target - Rescue Mode
Loaded: loaded (/usr/lib/systemd/system/rescue.target; static)
Active: inactive (dead)
Docs: man:systemd.special(7)
The status exits nonzero when the target is inactive, which is normal on a
running system. systemctl list-dependencies multi-user.target shows the
services that a normal boot brings in; the same view for rescue.target shows
how much is deliberately left out:
$ systemctl list-dependencies --no-pager multi-user.target | head -12
multi-user.target
● ├─chrony.service
○ ├─console-setup.service
● ├─cron.service
● ├─dbus.service
○ ├─e2scrub_reap.service
○ ├─grub-common.service
● ├─networking.service
● ├─rsyslog.service
● ├─ssh.service
● ├─systemd-ask-password-wall.path
● ├─systemd-logind.service
The filled circles are active units, the hollow circles are inactive but
loaded, and the × marker used elsewhere in systemctl output means a failed
unit. A rescue shell normally asks for the root password through sulogin; if
the root account is locked, that is itself part of the recovery problem and
must be fixed from separate media. To request a target for one boot, add
systemd.unit=rescue.target (or the SysV alias 1) at the GRUB editor; to
make it persistent, use systemctl set-default, as shown in Section 16.3.4.
Isolating rescue.target from a remote session would stop ssh.service and
the session with it; this is a console operation, not a remote one.
The previous boot is often more informative than the current one:
$ sudo journalctl --list-boots --no-pager
IDX BOOT ID FIRST ENTRY LAST ENTRY
0 bff25a0c7ba7470ca02c43b68ac6cb3e Sat 2026-11-21 03:12:20 UTC Sat 2026-11-21 03:17:32 UTC
Boot 0 is the current boot; negative indexes count backwards. A machine that
failed to reach a login may have only the current boot, because the failed
attempt never wrote a complete journal. If the system can boot an older
kernel, journalctl -b -1 shows the failed boot's final messages. Use the
journal before rebooting repeatedly: each attempt can overwrite the evidence
that explains the first failure. Chapter 8 covers
persistent storage, priority filters, and unit-specific queries.
16.1.2. Installer Rescue Mode and Live Media
When the installed system cannot reach a shell, the installer's rescue mode
provides one without reinstalling. Boot the installation media and choose
Advanced options, then Rescue mode, or type rescue at the boot
prompt. The first screens are the installer's own, with a note that nothing
will be overwritten. Rescue mode reuses the installer's hardware detection, so
disks, RAID, LVM, and network devices are available. Instead of the
partitioner, it lists the partitions it found and asks which one holds the
root filesystem to repair.
If the selected root can be mounted and a shell can run in it, the rescue menu
offers a shell with the installed system's tools. If the filesystem is too
damaged for that, it offers a shell in the installer environment instead, with
the selected root normally mounted at /target. The installer's rescue mode
does not repair anything by itself; it only gives you a trusted environment and
the mount. The Debian Installation Guide's documented example for restoring
GRUB to the first disk's master boot record is grub-install '(hd0)', run in
a shell that is already root. The (hd0) spelling is GRUB's own device
naming and may not name the same disk as /dev/vda.
Confirm the disk with lsblk before writing boot code; writing to the wrong
disk is destructive. On UEFI machines the ESP must be mounted, and the UEFI
command from Section 16.3.3 is the appropriate one. Rescue mode boots the
installer's kernel, not the installed kernel, so the tools and module versions
in the rescue shell may differ from the installed system's.
A Debian live image is the other separate environment. It is a full temporary
system with a desktop or console, a network stack, and the usual storage tools;
it can mount LUKS and LVM, copy data to external media, run ddrescue or
smartctl, and keep a browser open for documentation while the machine under
repair is offline. The installer's rescue mode is smaller and closer to the
target system; live media is more convenient when the repair needs a complete
toolset or a graphical browser. Both are booted from the same firmware path as
the installed system, so if the firmware no longer offers the installation
media, fix the firmware boot order first.
The choice between the two is a practical one:
| Environment | Best for | Trade-offs |
|---|---|---|
| Running installed system | Service, package, account, or capacity repair that does not require a reboot | Cannot help when the boot chain or root mount is broken; services may be racing the repair |
rescue.target / emergency.target |
Repairing the installed root with its own tools and no services | Needs a working boot loader and console; root may be read-only in emergency mode; remote sessions are lost |
| Installer rescue mode | Mounting an installed root and repairing GRUB, fstab, accounts, or packages |
Installer kernel and tool versions; the root may be too damaged to enter; no graphical desktop |
| Debian live media | Imaging a failing disk, unlocking complex storage, copying data, reading documentation | A full temporary system; the live environment's tools are not the installed system's; more memory and media |
The rescue and emergency targets are console paths: they are selected at the machine's boot menu, not over SSH, and they replace the running service set. Their menus and target semantics are documented in the installation and boot chapters. The mounted-root operations in Section 16.2 are the same once a shell can see the installed root, whether that shell came from the installer, a live image, or a rescue target.
16.1.3. Preparing Recovery Media and Offline Documentation
Recovery media is only useful if it is current, bootable, and independent of the machine it may have to rescue. Prepare at least two items: installation or live media that boots on the machine's firmware, and a separate copy of the data and configuration that the recovery will need. Keep a checksum of the image and verify it before writing the USB device; the verification procedure is the same as in Chapter 2. Label the media with the Debian release and the date, because a rescue image from an older release may not contain the tools or firmware needed by newer hardware.
The second item is a recovery dossier: the facts that are difficult to reconstruct when the system is down. On a healthy system, record the storage layout, the boot configuration, the package versions, and the network addressing. The storage overview is the most valuable page:
$ lsblk -o NAME,SIZE,TYPE,FSTYPE,FSVER,LABEL,UUID,MOUNTPOINTS
NAME SIZE TYPE FSTYPE FSVER LABEL UUID MOUNTPOINTS
vda 100G disk
└─vda1 100G part ext4 1.0 be225267-eaf4-410c-9ffa-5cd32b221ce0 /
vdb 4G disk LVM2_member LVM2 001 IDDoFF-b1nz-1QW0-yNKb-5KEf-jZcj-3xtJQy
└─datavg-appdata 1G lvm ext4 1.0 appdata ebee9a29-b855-4af5-a623-e9b4010ac3ee /srv/appdata
vdc 4G disk
├─vdc1 1M part
├─vdc2 512M part vfat FAT32 RECOVERYESP D166-6DF7 /mnt/recovery/boot/efi
└─vdc3 3.5G part ext4 1.0 recoveryroot bb532cbe-ae1c-4501-9d1e-17dc0e284919 /mnt/recovery
vdd 1G disk crypto_LUKS 2 recoverycrypt 78f570f6-b31e-4d57-a6df-c942bc125f37
vde 2G disk
The columns are the device name, size, type, filesystem type and version,
label, filesystem UUID, and current mount point. The indentation shows the
stack: vdb contains the LVM physical volume, datavg-appdata is the
logical volume, and /srv/appdata is its mount point. vdd is a LUKS
container with no inner filesystem visible until it is unlocked. This capture
shows the recovery copy already mounted for the later exercises; a dossier
recorded before an incident leaves those two mount points empty. Record the
output, not just the device names: a UUID survives renaming and reordering in
a way that /dev/sdb does not.
The root mount and the static configuration answer the next questions:
$ findmnt -o TARGET,SOURCE,FSTYPE,OPTIONS /
TARGET SOURCE FSTYPE OPTIONS
/ /dev/vda1 ext4 rw,relatime,discard,errors=remount-ro
$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# <file sys> <mount point> <type> <options> <dump> <pass>
# device during installation: /dev/loop0p1
UUID=be225267-eaf4-410c-9ffa-5cd32b221ce0 / ext4 rw,discard,errors=remount-ro 0 1
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
#VAGRANT-END
UUID=ebee9a29-b855-4af5-a623-e9b4010ac3ee /srv/appdata ext4 defaults 0 2
The #VAGRANT-BEGIN block is part of this box image's /etc/fstab; on a
machine installed from official media it is absent. The important lines are
the root entry and the application volume entry. The root entry's errors=
option controls whether an ext4 error remounts the filesystem read-only;
/srv/appdata uses defaults, which is adequate for a data volume but does
not enable quotas or ACL-specific options. Chapter 11
explains the fields and mount options in detail. The encryption table is
equally important:
$ cat /etc/crypttab
# <target name> <source device> <key file> <options>
A comments-only /etc/crypttab means no encrypted volume is unlocked at boot.
On an encrypted installation the file contains one line per mapping, usually
with a UUID= source and either none for an interactive passphrase or a key
file path. Record it before a repair; a /etc/crypttab that is regenerated
incorrectly can turn a working encrypted boot into an emergency shell. The
crypttab format and the difference between Debian's cryptdisks wrapper and
systemd's generator are covered in Chapter 13.
A recovery dossier should also include the boot loader's effective settings, the network configuration, and the package versions. The following commands write the storage and package overview into the workspace rather than the terminal; the redirection creates or replaces the file, so the terminal stays empty:
$ lsblk -o NAME,SIZE,TYPE,FSTYPE,LABEL,UUID,MOUNTPOINTS > storage-layout.txt
Read it back to prove the file is not empty:
$ cat storage-layout.txt
NAME SIZE TYPE FSTYPE LABEL UUID MOUNTPOINTS
vda 100G disk
└─vda1 100G part ext4 be225267-eaf4-410c-9ffa-5cd32b221ce0 /
vdb 4G disk LVM2_member IDDoFF-b1nz-1QW0-yNKb-5KEf-jZcj-3xtJQy
└─datavg-appdata 1G lvm ext4 appdata ebee9a29-b855-4af5-a623-e9b4010ac3ee /srv/appdata
vdc 4G disk
├─vdc1 1M part
├─vdc2 512M part vfat RECOVERYESP D166-6DF7 /mnt/recovery/boot/efi
└─vdc3 3.5G part ext4 recoveryroot bb532cbe-ae1c-4501-9d1e-17dc0e284919 /mnt/recovery
vdd 1G disk crypto_LUKS recoverycrypt 78f570f6-b31e-4d57-a6df-c942bc125f37
vde 2G disk
The reduced column set here omits the filesystem version, which is why this listing differs slightly from the earlier one. A checksum lets you tell later whether the dossier was edited or truncated:
$ sha256sum storage-layout.txt
3a9759078ae75a005f253f23f7ec1be44b846598d8f4ab182bdd790dee03e735 storage-layout.txt
Store the dossier on the recovery media or another machine, not only on the
system being repaired. The same applies to LUKS header backups and key
material: Chapter 13 shows cryptsetup luksHeaderBackup
and the key-file permissions that protect it. A header backup without its
passphrase is useless, and a passphrase stored only on the encrypted disk is
not a recovery plan. Keep the backup and the key material in separate places,
and test that the backup can be read before the emergency.
For offline documentation, keep the Debian Installation Guide, the Release
Notes, the relevant package manual pages, and this book's chapters on storage,
boot, services, and backups. The installed manual pages are part of the
package set: man 5 crypttab, man 8 update-initramfs, man 8 grub-install,
man 8 e2fsck, man 5 systemd.special, and man 8 lsof answer most of the
questions in this chapter without a network. The Debian wiki and the package
/usr/share/doc directories are useful when a driver or firmware problem is
specific to the hardware; record the exact model and error string before
searching. Chapter 1 describes the documentation
channels and how to report a reproducible bug.