Cyber Services / All lessons / CentOS / Linux Administration / Controlling the Boot Process

Controlling the Boot Process

Module 11 of 33 · 5 min read · Free to read

MODULES ↓ Command blocks have a COPY button

Mastering Systemd Targets and Recovery Modes on CentOS

The boot process is the complex sequence of events that takes a server from a powered-off state to a fully functional, logged-in environment. As a system administrator, your ability to influence and interrupt this process is a critical skill. It allows you to change the system's operational state, troubleshoot critical failures that prevent a normal boot, and perform emergency maintenance, such as resetting a lost root password.

The `systemd` Boot Sequence and Targets

In modern CentOS/RHEL systems, the entire boot process is orchestrated by systemd. After the bootloader (GRUB2) loads the kernel, the kernel starts the `systemd` process with PID 1. `systemd` then proceeds to bring the system up by activating a series of dependent units until it reaches a final "state" or milestone, known as a target.

The Modern "Runlevels": Understanding Targets

Targets are the modern equivalent of the old SysVinit runlevels. They define what services and functionality are available when the system is running.

`systemd` TargetOld RunlevelPurpose & Description
graphical.target5Standard Desktop Mode. A full, multi-user, networked system with a graphical user interface (GUI).
multi-user.target3Standard Server Mode. A full, multi-user, networked system but with a command-line interface (CLI) only. This is the preferred default for servers.
rescue.target1Single-User Rescue Mode. Mounts local filesystems and starts a minimal set of services. Useful for repairs when the system can't boot normally.
emergency.targetEmergency ShellThe most minimal environment. Mounts the root filesystem as read-only and provides a bare-bones shell. This is the last resort for severe filesystem corruption.

Managing the Default Boot Target

As a server administrator, you almost always want your system to boot into the non-graphical `multi-user.target` to save resources.

# Check the current default boot target
$ systemctl get-default
graphical.target

# Set the default boot target to the command-line interface
$ sudo systemctl set-default multi-user.target
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/multi-user.target.

# Switch to a different target in the live session without rebooting
$ sudo systemctl isolate multi-user.target

Interrupting the Boot: Rescue and Emergency Modes

What happens when a critical configuration error or filesystem corruption prevents the server from booting normally? You must interrupt the boot process to enter a special maintenance mode.

How to Interrupt the GRUB2 Bootloader

To access these modes, you must act quickly during boot.

  1. Start or reboot your CentOS VM.
  2. As soon as the BIOS/UEFI screen disappears, start pressing the Esc or Shift key repeatedly.
  3. This will bring you to the GRUB2 boot menu, which lists available kernels. This is your entry point for all recovery operations.

Rescue Mode

What it is: A single-user environment where the system attempts to mount all local filesystems and start some essential services. It's the preferred mode for most repair tasks.

How to enter: From the GRUB menu, select your kernel, press e to edit the boot parameters. Find the line that starts with `linux` and add the following to the end of that line: systemd.unit=rescue.target. Then press Ctrl+X to boot.

Emergency Mode

What it is: A more drastic recovery mode. It provides a root shell but does *not* start most services and mounts the root filesystem as read-only. This is used for critical filesystem repair when the system is unable to even mount the disks properly.

How to enter: Follow the same process as rescue mode, but add systemd.unit=emergency.target to the kernel line instead.

The Ultimate Recovery Skill: Resetting a Lost Root Password

This is one of the most common and critical recovery tasks. If you lose the root password, you are locked out of your own system. The following process allows you to regain access.

Step-by-Step Root Password Reset on CentOS/RHEL 8+
  1. Interrupt GRUB: Reboot the server and press Esc/Shift to enter the GRUB boot menu.
  2. Edit Boot Parameters: Highlight your kernel and press e.
  3. Find the Kernel Line: Use the arrow keys to find the line that starts with `linux`.
  4. Break into the initramfs: Go to the end of the `linux` line and add the parameter rd.break. This tells the system to stop the boot process very early, before the main root filesystem is even mounted, and give you a shell.
  5. Boot with the New Parameter: Press Ctrl+X to boot. You will be dropped into a `switch_root` shell.
  6. Remount the Filesystem with Write Access: The system root is mounted as read-only at `/sysroot`. You must remount it with write permissions to change the password.
    mount -o remount,rw /sysroot
  7. Enter the System Environment: Use `chroot` to make `/sysroot` your new root directory. This allows you to run commands as if you were in your fully booted system.
    chroot /sysroot
  8. Reset the Password: Now, simply use the `passwd` command. It will not ask for the old password.
    passwd root
    Changing password for user root.
    New password: 
    Retype new password: 
    passwd: all authentication tokens updated successfully.
  9. Force SELinux Relabel: This is a critical step! Because you've modified the /etc/shadow file outside of a normal SELinux context, you must tell SELinux to relabel all files on the next boot to fix the context. If you skip this, you will not be able to log in.
    touch /.autorelabel
  10. Exit and Reboot: Type exit twice. The first exits the `chroot`, the second exits the `rd.break` shell. The system will reboot. The first boot will be slow as SELinux relabels the filesystem. After this, you will be able to log in with your new root password.
Modules (33) ← CentOS / Linux Administration