16.5. Returning the System to Service
A repair is finished only when the system can do its job again. The final pass checks the filesystem, the package database, the service set, the network, and remote access, in that order. The checks are deliberately read-only where possible; a check that changes state belongs in the repair, not in the validation. If any check fails, return to the layer that owns it rather than declaring the recovery complete.
16.5.1. Checking Filesystem, Package, and Service Integrity
Run a filesystem check only on an unmounted filesystem, or with -n when the
filesystem is mounted and the goal is a read-only inspection. The data volume
is unmounted for this check:
$ sudo umount /srv/appdata
The command is silent. Then run the read-only check:
$ sudo e2fsck -fn /dev/datavg/appdata
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
appdata: 12/65536 files (0.0% non-contiguous), 13019/262144 blocks
e2fsck exits 0 when the filesystem is clean, 1 when errors were corrected,
2 when errors were corrected but the system should be rebooted, and 4 when
errors were left uncorrected. The -f
option forces a check even if the filesystem is marked clean; -n answers
"no" to every repair question and opens the filesystem read-only. Do not run
e2fsck on a mounted filesystem except with -n; the man page warns that even
then the results are not valid. If the check finds errors, image the filesystem
before running a repair, then run e2fsck -p for the automatic preen mode or
e2fsck -y only when you have a backup and understand the questions. The
"clean" state is not proof that every file is readable; it proves that the
metadata is consistent. Application-level checks, database dumps, and test
restores are still needed.
The package database is checked without changing it:
$ sudo apt-get check
Reading package lists...
Building dependency tree...
Reading state information...
apt-get check exits 0 when the dependency graph is consistent; it does not
download or install. dpkg --audit reports unpacked, half-configured, or
half-installed packages, and debsums -s verifies installed files against
their recorded checksums, reporting only mismatches. On a normal system both
are quiet:
$ sudo debsums -s
The command prints nothing when all checked files match. debsums excludes
configuration files by default - -a includes them - and does not check files
added after installation, so a mismatch it reports is in a package's shipped
files: a mismatch on a binary is a reason to reinstall the package or
investigate, while a conffile mismatch seen with -a is often an
administrator's own edit. The package integrity checks are described in
Chapter 6.
Check the service set and the unit syntax:
$ sudo systemctl --failed --no-pager
UNIT LOAD ACTIVE SUB DESCRIPTION
0 loaded units listed.
An empty list means no unit is in the failed state at this moment. A unit that
failed during boot may have been reset by a later start; journalctl -b -p err
shows the errors that are no longer reflected in the current state. Check the
custom unit's syntax before the next boot:
$ sudo systemd-analyze verify /etc/systemd/system/recovery-demo.service
The command is silent on success. It catches a missing executable, a malformed
unit file, and some dependency mistakes; it does not prove that the service
will start under its real environment. For the boot loader's generated menu,
grub-script-check /boot/grub/grub.cfg is the equivalent syntax check from
Section 16.3.3. For an encrypted root, confirm that the initramfs contains the
cryptsetup helpers with lsinitramfs before the next reboot. A validation that
only checks the running system can miss a boot-time failure; include the
next-boot path in the check.
16.5.2. Validating Networking and Remote Access
A recovered machine that cannot be reached is not back in service. Check the address assignment and the routing table before testing a remote login. As john, the ordinary user:
$ ip -br address
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0 UP 192.168.121.236/24 fe80::5054:ff:fef2:bf78/64
The interface name is eth0 on this image because the kernel command line
disables predictable names; a standard installation may show ens3 or
enp1s0. The UP state and the address are the facts that matter. The route
table names the default gateway:
$ ip route
default via 192.168.121.1 dev eth0
192.168.121.0/24 dev eth0 proto kernel scope link src 192.168.121.236
If the default route is missing, the machine can reach its local subnet but
not the internet or a remote administrator. Check the address configuration
source (NetworkManager, systemd-networkd, /etc/network/interfaces, or a
cloud-init configuration), not just the current kernel state; a route added by
hand disappears at the next boot. Chapter 17
covers the configuration layers and their interactions.
Name resolution is a separate layer:
$ getent hosts deb.debian.org
2a04:4e42:20::644 debian.map.fastlydns.net deb.debian.org
The command uses the system's NSS configuration, not a hard-coded resolver, so
it tests the path that ordinary applications use. The answer is an IPv6
address on this network; a machine without IPv6 connectivity may still resolve
the name but fail to connect. Test both a name and an address, and check
/etc/resolv.conf or the resolver service if the lookup fails. The DNS and
DHCP services themselves are covered in Chapter 22.
Local listeners show which services are ready to accept connections:
$ ss -ltn
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
The -l option shows listening sockets, -t TCP, and -n numeric addresses
instead of names. Port 22 is the SSH daemon, which is the remote-access path.
ss -ltnp adds the process name and PID when run as root. A listener on
0.0.0.0 or :: accepts external connections; a listener on 127.0.0.1
accepts only local ones. The firewall may still block the port; Chapter
18 covers the packet filter rules.
Validate the SSH configuration without restarting the daemon:
$ sudo sshd -t
The command is silent when the effective configuration is valid. It parses
/etc/ssh/sshd_config and its includes; a syntax error is reported with the
file and line. It does not test host keys, PAM, or the firewall. Check that the
service is active:
$ systemctl is-active ssh
active
The unit is ssh.service on Debian; sshd.service is an alias on some
systems. If it is inactive, systemctl status ssh and journalctl -u ssh
show why; do not restart it blindly while the current session depends on it.
The host keys and the client's known-hosts record are part of the SSH trust
path; Chapter 20 explains how to verify a host key through a
trusted channel before accepting a changed key. A recovery that regenerates
host keys will make every client warn about a changed host key, which is
expected but must be investigated, not dismissed.
Test the gateway to separate local connectivity from upstream routing:
$ ping -c 2 -W 2 192.168.121.1
PING 192.168.121.1 (192.168.121.1) 56(84) bytes of data.
64 bytes from 192.168.121.1: icmp_seq=1 ttl=64 time=0.254 ms
64 bytes from 192.168.121.1: icmp_seq=2 ttl=64 time=0.252 ms
--- 192.168.121.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1017ms
rtt min/avg/max/mdev = 0.252/0.253/0.254/0.001 ms
The two replies prove that the local link and the gateway are reachable. A
failure here is not a DNS problem; check the interface, the route, and the
switch or virtual network. If the gateway responds but a remote host does not,
the problem is upstream or at the remote end. ICMP may be filtered while TCP
works, so a failed ping is not by itself proof that the machine is offline;
test the actual service port with ss on the server and a client connection.
Before closing the rescue session, open a second SSH session from another machine and log in as the intended administrator. A successful local command does not prove that the network path, the firewall, the SSH host key, and the user's credentials all work from outside. Keep the existing session open until the new one is proven, so a bad SSH change does not lock you out. The authentication and host-key details are in Chapter 20.
16.5.3. Deciding When Restore or Reinstallation Is the Better Repair
Repair in place is appropriate when the failure is localized and the
underlying data is trustworthy: a single configuration file, a package, a boot
loader, an account, a full filesystem, or a failed service. Restore is
appropriate when the system's integrity is uncertain or when the repair would
take longer than a known-good copy: filesystem corruption that e2fsck
cannot resolve cleanly, a compromised system, a failed disk with unreadable
sectors, or a botched upgrade that touched many packages. Reinstallation is
appropriate when the machine's role is reproducible from configuration
management and the data is already safe elsewhere; it is often faster and
more predictable than reconstructing a damaged package database by hand.
The decision depends on evidence, not on pride. A repaired system that has unknown corruption may fail again at the next write; a reinstalled system that loses an unbacked-up database is worse than a slow repair. Answer these questions before choosing:
| Question | Repair in place | Restore from backup | Reinstall |
|---|---|---|---|
| Is the failure localized to one layer? | Yes, usually the best case | Possible, but heavier | Unnecessary |
Is the filesystem metadata consistent after e2fsck? |
Required | Not required; the backup is the source | Not required |
| Is the system's integrity trustworthy? | Must be established | A clean backup is the baseline | A fresh install is the baseline |
| Is the data backed up and verified? | Still recommended | Required | Required |
| Is the configuration reproducible? | Useful, not essential | Useful | Essential for a fast rebuild |
| Is the machine's role unique or stateful? | Repair may preserve local state | Restore preserves the backup's state | Reinstall loses unmanaged state |
| Time to service | Usually shortest | Depends on data size and media | Depends on installation and configuration |
A restore is not automatically safer than a repair: an old backup can lose months of data, and a backup that has never been restored is a hope, not a plan. Use the restore exercises in Chapter 15 to establish that the copy is readable and complete. A reinstallation is not automatically cleaner either: it may reintroduce a vulnerability that was patched on the old system, and it loses the evidence that would explain the original failure. Preserve the old disk or an image before reinstalling, even when the decision is to start over.
The practical rule is to escalate only when the lower-cost repair has failed or when the evidence makes it unsafe. Repair the boot loader before restoring the root; repair the package database before reinstalling the system; restore the data volume before rebuilding the application; reinstall only after the data and the configuration are safe and the role can be rebuilt. Record the decision and its reason in the incident notes; the next administrator will need to know whether the machine is a repaired original or a rebuilt replacement.