Linux Basics 24: Boot And Rescue — UEFI, GRUB, initramfs And Emergency Mode

Published 2022-05-02 20:30 Updated 2022-05-02 22:10 1048 words 6 min read ... Page views Handwritten Every word here was typed by me.

Linux Basics 25: Wrapping Up — Self-Audit Checklists And Where To Go DeeperLinux Basics 24: Boot And Rescue — UEFI, GRUB, initramfs And Emergency ModeLinux Basics 23: Containers And Kernel Boundaries — Namespaces, Cgroups And What docker exec DoesLinux Basics 22: Logs And A Debugging Method — Six Steps From Symptom To Root CauseLinux Basics 21: Scheduled Tasks — The Traps Of crontab And systemd TimersLinux Basics 20: Shell Scripting, Part Two — trap, Logging, Idempotence And LockingLinux Basics 19: Shell Scripting, Part One — Quoting, Tests And Exit CodesLinux Basics 18: Performance And Tracing — Where Is The Machine Actually StuckLinux Basics 17: Firewalls — How iptables, nftables, firewalld And ufw RelateLinux Basics 16: SELinux — When Every Permission Is Correct And The Service Is Still DeniedLinux Basics 15: Advanced Permissions — sudoers Wildcards, ACLs And CapabilitiesLinux Basics 14: SSH And Remote Operations — Keys, Permissions And Three Kinds Of TunnelLinux Basics 13: Networking Fundamentals — Troubleshoot From The Bottom UpLinux Basics 12: Disks And Filesystems — From Block Device To Mount PointLinux Basics 11: systemd — units, systemctl And journalctl Done ProperlyLinux Basics 10: Package Management — What apt Actually Does, And The Four Common ErrorsLinux Basics 09: Users And Groups — passwd, shadow And sudoersLinux Basics 08: Environment Variables And Shell Startup Files — Why ssh Cannot Find Your PATHLinux Basics 07: Processes And Signals — The Real Difference Between TERM And KILLLinux Basics 06: Pipelines And Redirection — Three Streams, One Ordering TrapLinux Basics 05: Text Processing — Turning grep/sort/uniq Into Your Analysis ToolkitLinux Basics 04: Permissions And Ownership — rwx Is Not The Same On Files And DirectoriesLinux Basics 03: The Real Semantics Of ls, cp, mv, rm — And The * That The Shell ExpandsLinux Basics 02: Why / Looks Like This, and What An Inode Really StoresLinux Basics 01: Terminals, Shells, and How Commands Are Actually Found
The full boot chain from firmware to GRUB2 to kernel, initramfs and systemd, observing boot time with systemd-analyze, editing kernel parameters in GRUB to reach rescue mode, the standard root password reset, and how to repair emergency mode after a mistyped fstab plus the discipline that prevents it.

What This Chapter Solves

You edit /etc/fstab, reboot, and the machine stops in emergency mode — a wall of red text ending in Give root password for maintenance. Or you simply forgot the root password and need to reset it from the console. Whether you get the machine back depends on knowing what the boot chain looks like and which link in it you can intervene in by hand.

This chapter walks the boot sequence, then covers three rescue entry points and two classic rescue scenarios.

1. The Boot Chain

power on → UEFI/BIOS self test → read a boot entry → boot loader (GRUB2)
     → load the kernel vmlinuz plus initramfs (a temporary root with drivers and unlock)
     → the kernel mounts the real root filesystem → systemd (PID 1) → the target
ComponentRole
firmware UEFI/BIOSfinds a boot device; the ESP is mounted at /boot/efi, FAT32
GRUB2boot menu and kernel parameters; configured in /boot/grub2/grub.cfg
initramfsa mini root filesystem that mounts the root, unlocks LUKS, loads drivers
systemdbrings services up to multi-user.target or graphical.target

Bottom line: /boot holds two life-saving things — a known-good older kernel and the GRUB menu. When a new kernel will not boot, you can select the old one and fix things from there. Keeping them matters more than anything.

2. Observing Boot

systemd-analyze                     # total kernel plus userspace time
systemd-analyze blame | head        # per-service time, ranked
systemd-analyze critical-chain      # the critical path — what delayed the trunk
cat /proc/cmdline                    # this boot's kernel parameters
journalctl -b -1 -p err             # errors from the previous boot — check after a crash

3. Rescue: Editing Kernel Parameters In GRUB

Select an entry in the GRUB menu, press e, and append to the linux line:

ParameterPurpose
single or 1single-user, old SysV semantics, roughly rescue on systemd
rd.breakstop at an initramfs shell, before the real root is mounted
init=/bin/bashboot straight into bash, skipping systemd
systemd.unit=rescue.targetreach the rescue target, local mounts, few services
rw or romount the root filesystem writable or read-only first

Note: rd.break stops in initramfs, where the real root is still mounted at /sysroot; init=/bin/bash boots with the root mounted but no other services running. Which you choose depends on whether you are fixing something before the root is mounted or after boot.

4. Resetting The Root Password (RHEL-Family Standard)

# 1) In the GRUB menu press e, append rd.break to the linux line, then Ctrl-x to boot
# 2) At the initramfs shell:
mount -o remount,rw /sysroot        # mount the real root writable
chroot /sysroot                     # switch into the real root
passwd root                         # change the password
touch /.autorelabel                 # force SELinux relabelling on the next boot
exit                                # leave the chroot
reboot

Why touch /.autorelabel is mandatory: editing /etc/shadow from initramfs leaves the SELinux context wrong; without a relabel, the new password may actually lock you out. This is the step people most often miss.

This procedure assumes you can physically reach the machine or enter its console (cloud hosts via VNC or serial console). It is available to anyone with hardware access, so datacenter physical security and cloud console permissions are the security boundary — do not treat "change a password and walk in" as a given.

5. A Mistyped fstab → emergency mode

A typo in /etc/fstab points at a UUID that does not exist, systemd cannot mount it, and the boot lands in emergency mode:

# after the emergency shell (or by entering the root password for maintenance):
journalctl -xb | grep -i -E 'failed|fstab|mount' | tail -30   # find the offending line
mount -o remount,rw /                                          # the root is usually read-only here
vi /etc/fstab                                                  # comment out or fix the bad line
mount -a                                                       # verify every entry mounts
systemctl default                                              # continue to the default target

Note: mount -a is mandatory after editing fstab — it tries every entry once and fails loudly right there if something is wrong, instead of dropping you into emergency mode on the next reboot. Chapter 12 stressed the same point about UUIDs.

6. Discipline

  • Before touching /etc/fstab or boot parameters, run mount -a or verify on a test machine.
  • Always keep at least one known-good older kernel (dnf install kernel can install several in parallel, visible in the GRUB menu).
  • Before modifying the boot chain, memorise the three entry points: e to edit, rd.break, and init=/bin/bash — know how to get in and how to get back out.

7. Hands-On Checklist

systemd-analyze; systemd-analyze blame | head         # 1. boot timing
systemd-analyze critical-chain | head                  # 2. the critical path
cat /proc/cmdline                                      # 3. current kernel parameters
ls /boot/vmlinuz-* /boot/initramfs-* 2>/dev/null       # 4. which kernels are installed
systemctl get-default                                  # 5. the default target
sudo findmnt --verify --verbose | head                 # 6. validate the fstab entries

Summary

  • The chain: firmware → GRUB2 → kernel plus initramfs → systemd → target; the ESP lives at /boot/efi.
  • systemd-analyze (blame, critical-chain) shows timing; /proc/cmdline shows this boot's parameters; journalctl -b -1 shows the last boot.
  • Rescue entry points: in GRUB press e and add single, rd.break, init=/bin/bash or systemd.unit=rescue.target.
  • Root reset: rd.break → mount -o remount,rw /sysroot → chroot → passwd → touch /.autorelabel → exit.
  • Emergency mode from fstab: journalctl -xb to find the error → remount,rw / → fix fstab → mount -a to verify → systemctl default.
  • Discipline: validate before touching fstab or boot parameters, and always keep a known-good older kernel.

Next Up

The final chapter wraps the series: four pre-flight checklists, a symptom-to-first-command table, the directions worth going deeper into, and a debugging methodology you can keep using.

Written by Handwritten

If you enjoyed this, leave a comment~

Loading...