Operating System CentOS

Boot Process and Recovery on AlmaLinux 9, Rocky Linux 9 and RHEL 9

The boot sequence of AlmaLinux 9, Rocky Linux 9 and RHEL 9 explained stage by stage, with recovery steps for lost root passwords, broken GRUB configs and initramfs images.

When a server does not come back after a reboot, you can only fix it quickly if you know which stage of the boot failed. This guide walks through the boot sequence of AlmaLinux 9, Rocky Linux 9, CentOS Stream 9 and RHEL 9 — firmware, GRUB 2, kernel and initramfs, systemd — and then covers the recovery jobs you are most likely to need: getting a shell on a system that will not boot, resetting a lost root password, and rebuilding the GRUB configuration and the initramfs.

CentOS 7 and RHEL 7 are the releases where systemd and GRUB 2 first appeared. CentOS 7 reached end of life in June 2024; the 9 releases keep the same overall design but describe boot menu entries differently, so several commands from older tutorials no longer apply.

What you need

  • Console access. Everything that happens before the network is up is invisible over SSH, so use your provider's web console (VNC, serial or IPMI/KVM).
  • For the worst cases, the ability to attach an installation ISO or boot a rescue system from your hosting control panel.
  • A snapshot or backup before you experiment on a machine that still boots.

The boot sequence, stage by stage

  1. Firmware. The machine runs its power-on self test and picks a boot device. With UEFI, the firmware reads the EFI System Partition, mounted at /boot/efi, and starts shim, which in turn loads GRUB from /boot/efi/EFI/<distribution>/. With legacy BIOS, the firmware runs the boot code in the first sector of the disk, which loads the rest of GRUB.
  2. GRUB 2. The boot loader reads /boot/grub2/grub.cfg and builds its menu from the Boot Loader Specification (BLS) snippets in /boot/loader/entries/, one file per installed kernel.
  3. Kernel and initramfs. GRUB loads the selected vmlinuz and its initramfs image into memory and hands over control together with the kernel command line.
  4. systemd inside the initramfs. The kernel unpacks the initramfs, a small temporary root file system built by dracut, and starts systemd from it. This instance loads the storage drivers, assembles LVM, RAID or LUKS devices, and mounts the real root file system read-only at /sysroot.
  5. Switch root. systemd switches to the real root file system and re-executes itself from disk as PID 1.
  6. System start. systemd activates default.target, starting every unit that target depends on in parallel, until the login prompt appears.

To see which firmware mode a running system uses, check whether the EFI directory exists:

[ -d /sys/firmware/efi ] && echo UEFI || echo BIOS

GRUB 2 and BLS entries

On the 9 releases you do not edit grub.cfg to change menu entries. Each entry is a small text file in /boot/loader/entries/ with title, linux, initrd and options lines, created automatically when a kernel package is installed. The tool for inspecting and changing them is grubby.

List all entries and show which kernel boots by default:

sudo grubby --info=ALL
sudo grubby --default-kernel

Make a different installed kernel the default (use a path from the list above):

sudo grubby --set-default /boot/vmlinuz-<version>

Add or remove a kernel argument permanently for every entry:

sudo grubby --update-kernel=ALL --args="console=ttyS0,115200"
sudo grubby --update-kernel=ALL --remove-args="quiet"

Global GRUB settings such as the menu timeout live in /etc/default/grub and only take effect after the configuration is regenerated (see below).

Editing an entry for one boot

Most recovery work starts with a temporary change to the kernel command line:

  1. Reboot and watch the console. When the GRUB menu appears, press an arrow key to stop the countdown.
  2. Select the entry you want and press e.
  3. Move to the line that starts with linux, press End and append the argument.
  4. Press Ctrl+X to boot. The change is not saved.

If a new kernel is the problem, you do not need to edit anything: pick the previous kernel from the menu, then fix the default with grubby --set-default.

systemd targets

A target is a named group of units that describes a system state. Targets replaced the SysV runlevels.

TargetWhat you getOld runlevel
emergency.targetA root shell on the console and nothing else; no services, no extra mounts—
rescue.targetBase system with local file systems mounted and a root shell; only the most basic services1
multi-user.targetNormal server operation with networking, text login3
graphical.targetmulti-user.target plus a graphical login5

Check and change the target the system boots into:

systemctl get-default
sudo systemctl set-default multi-user.target

Switch a running system to another target without rebooting:

sudo systemctl isolate rescue.target

Finding out what went wrong

If the system boots but slowly or with failures, these commands show where the time went and what broke:

systemd-analyze
systemd-analyze blame | head -n 15
systemctl --failed
sudo journalctl -b -p err

journalctl -b -1 shows the previous boot, which is what you want after a boot that ended in a manual reset. To watch a failing boot live, edit the entry in GRUB and remove rhgb and quiet from the linux line so kernel and service messages are printed on the console.

Recovery: rescue and emergency targets

When a service or a mount hangs the boot, start a smaller target. Append one of these to the linux line in GRUB:

systemd.unit=rescue.target
systemd.unit=emergency.target

Both ask for the root password before giving you a shell. If the root account is locked or has no password, which is common on cloud images, they are of no use and you need the rd.break method below.

In the emergency target the root file system is mounted read-only. Remount it before editing files:

mount -o remount,rw /

The classic case is a wrong line in /etc/fstab: systemd waits for a device that does not exist and then drops to emergency mode by itself. Log in, remount read-write, correct or comment out the line, and reboot. For data volumes that may be absent, add the nofail mount option so a missing disk no longer blocks the boot.

Recovery: resetting the root password with rd.break

rd.break tells dracut to stop at the end of the initramfs stage, before control passes to the real system, and open a shell without asking for a password. This is the procedure Red Hat documents for RHEL 9.

  1. Reboot, press e on the default GRUB entry and append rd.break to the end of the linux line. Press Ctrl+X.
  2. At the switch_root prompt, remount the real root file system, which is mounted read-only under /sysroot, as writable:
    mount -o remount,rw /sysroot
  3. Change into it:
    chroot /sysroot
  4. Set the new password:
    passwd
  5. Tell SELinux to relabel all files on the next boot. Without this, /etc/shadow keeps a wrong label and logins fail:
    touch /.autorelabel
  6. Leave the chroot and the initramfs shell. The system continues booting:
    exit
    exit

The relabel runs before the login prompt and can take several minutes on a large disk; the server reboots once more when it finishes. Do not interrupt it.

Two things commonly get in the way. If the linux line contains console= arguments, the shell appears on the last console listed, which may be a serial port rather than the screen you are watching; remove those arguments for this boot. And if the root file system is encrypted with LUKS, you are asked for the passphrase before the shell opens.

Anyone with console access can do this, which is the reason console access to a server has to be protected as carefully as the root password itself.

Recovery: regenerating the GRUB configuration

Regenerate grub.cfg after changing /etc/default/grub, or when the file is damaged or missing:

sudo grub2-mkconfig -o /boot/grub2/grub.cfg

On the 9 releases this path is the same for BIOS and UEFI systems. The file /boot/efi/EFI/<distribution>/grub.cfg on UEFI machines is only a small stub that points to /boot/grub2/grub.cfg. Do not write to it; many RHEL 7 era instructions do exactly that and leave the system unbootable.

If the boot loader itself is damaged — the machine stops at a grub> or grub rescue> prompt or does not find a boot device — reinstall it. On a BIOS system, write GRUB to the boot disk (the disk, not a partition):

sudo grub2-install /dev/sda

On a UEFI system, do not use grub2-install. Reinstall the packages that own the files on the EFI System Partition instead:

sudo dnf reinstall grub2-efi-x64 shim-x64

Then regenerate grub.cfg as shown above.

Recovery: rebuilding the initramfs

A missing or broken initramfs typically ends in a kernel panic about not being able to mount the root file system, or in a dracut emergency shell that complains the root device does not exist. This happens after an interrupted kernel update, or after moving a disk to different hardware or a different storage controller. Boot an older kernel from the GRUB menu, then rebuild the image for the kernel that fails:

sudo dracut -f --kver <version>

Without --kver, dracut -f rebuilds the image of the running kernel. To rebuild the images of all installed kernels at once:

sudo dracut -f --regenerate-all

-f is required because dracut refuses to overwrite an existing image otherwise. Inspect what an image contains with lsinitrd:

sudo lsinitrd /boot/initramfs-$(uname -r).img | less

When nothing on the disk boots

If no kernel starts and GRUB cannot be reached, boot the installation ISO of your distribution (attach it from your hosting control panel), choose Troubleshooting and then the rescue option. The rescue environment finds your installation, mounts it and prints the chroot command to enter it. Inside the chroot, the commands from the three recovery sections above work as they do on the running system: reset a password, reinstall GRUB, regenerate grub.cfg or rebuild the initramfs, then exit and reboot without the ISO.

Frequently asked questions

The GRUB menu never appears on my VPS. How do I get to it?

Cloud images often use a very short timeout or send the menu to a serial console. Open the provider's console before you reboot and press a key as soon as the machine restarts; if your provider offers both a VNC and a serial console, try the other one. On a system that still boots, raise GRUB_TIMEOUT in /etc/default/grub and regenerate the configuration so the menu is reachable next time.

What is the difference between rescue.target, emergency.target and rd.break?

They stop at different depths. rd.break stops inside the initramfs before the real system starts and needs no password. emergency.target starts the real system's systemd with only a root shell and a read-only root file system. rescue.target additionally mounts local file systems and starts basic services. The last two require the root password.

Is there a way to skip the long SELinux relabel after a password reset?

Red Hat's documentation for RHEL 8 describes booting with enforcing=0 and then restoring only the label of /etc/shadow with restorecon before switching SELinux back to enforcing. It saves time on large disks, but the full relabel is the simpler and safer route if you are unsure.

Do old commands like grub2-set-default still work?

The GRUB tools are still installed, but menu entries are BLS files now, so grubby is the supported way to list entries, choose the default kernel and change kernel arguments. Editing grub.cfg by hand was never safe and is pointless on the 9 releases because the entries are not stored there.

Share story:

Continue reading other guides on How VPS.