# Boot the system

> Follow a Linux machine from power button to login prompt: firmware, boot loader, kernel, initramfs and init.

Source: https://aminbytes.dev/bytes/system-architecture/boot-the-system/


When a machine doesn't come up, the question is always *where* it stopped. Did the firmware find a disk? Did GRUB load? Did the kernel panic? Did a service hang? If you know the boot sequence by heart, the last message on the screen tells you which stage failed and where to look next.

It's also where you get your best rescue tricks: a few words added to the kernel line at boot can get you into a broken system without a rescue disk.

## The boot sequence at a glance

```text
Power on
  └─ Firmware (BIOS or UEFI): test hardware, find something to boot
       └─ Boot loader (GRUB 2): pick a kernel, pass it options
            └─ Kernel: set up CPU, memory, devices
                 └─ initramfs: load drivers, find and mount the real root filesystem
                      └─ init (PID 1): systemd or SysVinit starts all services
                           └─ Login prompt or graphical login
```

Let's walk through each stage.

## 1. Firmware: BIOS or UEFI

The **firmware** is the software built into the motherboard. It runs first, tests the hardware (the **POST**, power-on self-test) and looks for something to boot.

**BIOS** (Basic Input/Output System) is the old way, from the original IBM PC:

- It reads the first 512-byte sector of the boot disk: the **MBR** (Master Boot Record).
- Those 512 bytes hold 446 bytes of boot code, the 64-byte partition table and a 2-byte signature (`0x55AA`).
- 446 bytes is far too small for a real boot loader, so that code only loads the next stage of GRUB from elsewhere on the disk.

**UEFI** (Unified Extensible Firmware Interface) is the modern replacement:

- It understands partitions and FAT filesystems itself.
- It keeps a list of boot entries in its own memory (NVRAM).
- Each entry points to an `.efi` program on the **EFI System Partition** (ESP), a small FAT32 partition that Linux mounts at `/boot/efi`.
- **Secure Boot** only runs programs signed with a trusted key. Linux distributions boot a small signed loader called `shim` first, which then starts GRUB.

Check which one your running system used:

```bash
ls /sys/firmware/efi >/dev/null 2>&1 && echo UEFI || echo BIOS
```

On UEFI, `efibootmgr` lists and changes the firmware's boot entries:

```bash
sudo efibootmgr
```

```console
BootCurrent: 0001
Timeout: 1 seconds
BootOrder: 0001,0000,0002
Boot0000* UEFI: PXE IPv4 Intel(R) Ethernet Connection I219-V
Boot0001* debian
Boot0002* UEFI: Built-in EFI Shell
```

## 2. The boot loader

The **boot loader** loads the kernel and the initramfs into memory and starts the kernel, passing it a line of options. On almost every Linux system today it's **GRUB 2**. (Installing and configuring GRUB is objective 102.2, here you only need to use it.)

On Debian and Ubuntu the files sit in `/boot`:

```bash
ls /boot
```

```console
System.map-6.1.0-27-amd64  efi   initrd.img-6.1.0-27-amd64
config-6.1.0-27-amd64      grub  vmlinuz-6.1.0-27-amd64
```

`vmlinuz-*` is the compressed kernel, `initrd.img-*` the initramfs. On RHEL and Fedora the initramfs is called `initramfs-<version>.img` instead, and GRUB's files are in `/boot/grub2/`.

### Using the GRUB menu

| Key | What it does |
| --- | --- |
| `↑` `↓` | pick a menu entry |
| `Enter` | boot the selected entry |
| `e` | edit the entry, for this boot only |
| `Ctrl+X` or `F10` | boot the edited entry |
| `c` | open the GRUB command line |
| `Esc` | leave the editor or command line, drop your edits |

Ubuntu hides the menu by default. Hold `Shift` (BIOS) or tap `Esc` (UEFI) while it starts to make it appear.

When you press `e`, you see the commands that boot this entry:

```text
setparams 'Debian GNU/Linux'
    insmod part_gpt
    insmod ext2
    search --no-floppy --fs-uuid --set=root 2f6c1a7e-8d41-4c0a-9a5e-1b3f0e2c7d55
    linux   /boot/vmlinuz-6.1.0-27-amd64 root=UUID=2f6c1a7e-8d41-4c0a-9a5e-1b3f0e2c7d55 ro quiet
    initrd  /boot/initrd.img-6.1.0-27-amd64
```

Move to the end of the `linux` line, add or remove kernel options, and press `Ctrl+X`.

### Booting by hand from the GRUB command line

If the menu is broken, `c` gives you a prompt where you can boot manually. These are the core commands:

```text
grub> ls
(hd0) (hd0,gpt1) (hd0,gpt2)
grub> set root=(hd0,gpt2)
grub> linux /boot/vmlinuz-6.1.0-27-amd64 root=/dev/sda2 ro
grub> initrd /boot/initrd.img-6.1.0-27-amd64
grub> boot
```

GRUB counts disks from 0 and partitions from 1, so `(hd0,gpt2)` is the second partition of the first disk. Old **GRUB Legacy** used `root (hd0,0)`, `kernel` instead of `linux`, and counted partitions from 0, which still shows up in exam questions.

## 3. Kernel options

Everything after the kernel file name on the `linux` line is the **kernel command line**. Some options are read by the kernel, some are passed on to init. See what your system booted with:

```bash
cat /proc/cmdline
```

```console
BOOT_IMAGE=/boot/vmlinuz-6.1.0-27-amd64 root=UUID=2f6c1a7e-8d41-4c0a-9a5e-1b3f0e2c7d55 ro quiet
```

The options worth knowing:

| Option | Effect |
| --- | --- |
| `root=/dev/sda2` or `root=UUID=…` | which filesystem becomes `/` |
| `ro` / `rw` | mount root read-only first (so it can be checked) or read-write |
| `quiet` | hide most kernel messages on screen |
| `splash` | show a graphical boot screen (Plymouth) |
| `single`, `1`, `S` | single-user mode (on systemd: rescue mode) |
| `systemd.unit=rescue.target` | boot into rescue mode: root shell, basic system, no network services |
| `systemd.unit=emergency.target` or `emergency` | an even smaller shell: root mounted read-only, almost nothing started |
| `systemd.unit=multi-user.target` | boot to text mode this once (like runlevel 3) |
| `init=/bin/bash` | skip init entirely and start a shell as PID 1 |
| `nomodeset` | don't let graphics drivers set the video mode, useful for a black screen |
| `modprobe.blacklist=nouveau` | don't load that module during this boot |
| `console=ttyS0,115200` | send the console to a serial port (servers, VMs) |
| `mem=4G`, `maxcpus=2` | limit memory or CPUs, handy for testing |
| `acpi=off` | disable ACPI, a last resort on buggy firmware |

> [!TIP] Forgot the root password?
> Add `init=/bin/bash` to the `linux` line and boot. You get a root shell with no password. Run `mount -o remount,rw /`, then `passwd`, then `sync` and `reboot -f` (there's no init running to do a normal reboot). On RHEL and Fedora also run `touch /.autorelabel` before rebooting, so SELinux fixes the label on the changed password file. This is also why physical access matters and why you can password-protect GRUB.

Changes in the GRUB editor last for one boot. To make an option permanent, edit `GRUB_CMDLINE_LINUX` in `/etc/default/grub`, then regenerate the config with `sudo update-grub` (Debian, Ubuntu) or `sudo grub2-mkconfig -o /boot/grub2/grub.cfg` (RHEL, Fedora). RHEL also offers `sudo grubby --update-kernel=ALL --args="nomodeset"`.

## 4. Kernel and initramfs

The **kernel** is the core of the operating system: it manages the CPU, memory, devices and processes. Once GRUB starts it, the kernel unpacks itself, sets up memory and detects hardware.

Then comes a chicken-and-egg problem: the drivers for your disk (SATA, NVMe, RAID, LVM, encryption) are modules *on* that disk. The **initramfs** solves it. It's a small compressed archive (cpio format) that GRUB loads next to the kernel. The kernel unpacks it into memory and runs it as a temporary root filesystem. Its scripts load the right drivers, ask for your disk encryption passphrase if needed, mount the real root filesystem, and then switch over to it.

The older name is **initrd** (initial RAM disk), and you'll still see it in file names and in GRUB's `initrd` command.

| | Debian, Ubuntu | RHEL, Fedora |
| --- | --- | --- |
| File | `/boot/initrd.img-<version>` | `/boot/initramfs-<version>.img` |
| Rebuild | `sudo update-initramfs -u` | `sudo dracut -f` |
| List contents | `lsinitramfs /boot/initrd.img-$(uname -r)` | `lsinitrd` |

If the initramfs is missing a driver, the boot stops with a message like `VFS: Unable to mount root fs` or drops you into a tiny emergency shell from the initramfs.

## 5. init: the first process

When the real root is mounted, the kernel starts **init**: the first user-space program, always with process ID 1. Every other process descends from it. If PID 1 dies, the kernel panics.

```bash
ps -p 1
ls -l /sbin/init
```

```console
    PID TTY          TIME CMD
      1 ?        00:00:03 systemd
lrwxrwxrwx 1 root root 20 Oct 31 12:00 /sbin/init -> /lib/systemd/systemd
```

 (Side note: Run `ps -p 1` inside a Docker container and you'll see `bash` or whatever command started the container. Containers usually have no init system at all, which is why `systemctl` doesn't work in them.)

Over the years Linux has had three main init systems:

### SysVinit (legacy)

The classic Unix System V design. `/sbin/init` reads `/etc/inittab`, which sets the default **runlevel** (a numbered system state, like 3 for text mode or 5 for graphical). It then runs shell scripts from `/etc/init.d/` in a fixed order, one after another, through links in `/etc/rc<N>.d/`. Simple to understand, but slow, and it can't easily restart a crashed service. Runlevels get their own byte next.

### Upstart (awareness)

Built by Canonical as an **event-based** replacement: jobs start when events happen ("filesystem mounted", "network up"). It shipped in Ubuntu from 6.10 to 14.10, in RHEL 6 and in ChromeOS. Its jobs were files in `/etc/init/*.conf`, controlled with `initctl`, `start`, `stop` and `status`. It's no longer used by major distributions. Ubuntu switched to systemd in 15.04.

### systemd (current)

The init system on Debian, Ubuntu, RHEL, Fedora, SUSE and Arch today. Instead of scripts it uses declarative **unit files** (`.service`, `.target`, `.mount`, `.socket`, `.timer`…), starts things **in parallel** based on their dependencies, and supervises services so it can restart them. It boots towards a **target** (a group of units) such as `graphical.target`. Besides init, the systemd project also brings the journal (`journald`), `logind`, `udevd` and more.

See how long each stage took:

```bash
systemd-analyze
systemd-analyze blame | head -3
```

```console
Startup finished in 3.104s (firmware) + 1.522s (loader) + 2.311s (kernel) + 1.874s (initrd) + 5.906s (userspace) = 14.719s
graphical.target reached after 5.871s in userspace.
2.913s NetworkManager-wait-online.service
1.104s apt-daily-upgrade.service
 612ms dev-nvme0n1p2.device
```

## Checking boot events in the logs

### dmesg: the kernel ring buffer

The kernel keeps its messages in a fixed-size buffer in memory, the **ring buffer** (when it's full, the oldest lines are overwritten). `dmesg` prints it:

```bash
sudo dmesg | head -3
sudo dmesg -T | grep -i usb | tail -2
```

```console
[    0.000000] Linux version 6.1.0-27-amd64 (debian-kernel@lists.debian.org) (gcc-12 (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40) #1 SMP PREEMPT_DYNAMIC Debian 6.1.115-1 (2024-11-01)
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-6.1.0-27-amd64 root=UUID=2f6c1a7e-8d41-4c0a-9a5e-1b3f0e2c7d55 ro quiet
[    0.000000] BIOS-provided physical RAM map:
[Mon Dec  1 09:41:12 2026] usb 1-2: new high-speed USB device number 4 using xhci_hcd
[Mon Dec  1 09:41:12 2026] usb-storage 1-2:1.0: USB Mass Storage device detected
```

The number in brackets is seconds since the kernel started. Useful options:

| Option | Does |
| --- | --- |
| `-T` | human-readable timestamps |
| `-H` | human format with a pager and colors |
| `-l err,warn` | only these levels (`--level`) |
| `-w` | keep watching for new messages, like `tail -f` |
| `-c` | print, then clear the buffer (root only) |

On Debian and Ubuntu, normal users may not read the buffer (`dmesg: read kernel buffer failed: Operation not permitted`), so use `sudo`.

### journalctl: the systemd journal

On systemd machines, `journald` collects kernel messages, early boot messages from the initramfs and the output of every service, all in one place:

```bash
journalctl -b             # everything from this boot
journalctl -b -p err      # only errors and worse from this boot
journalctl -k             # kernel messages only (like dmesg)
journalctl --list-boots   # all boots the journal remembers
journalctl -b -1          # the previous boot
journalctl -b -u ssh      # one service, this boot (sshd on RHEL)
```

```console
IDX BOOT ID                          FIRST ENTRY                 LAST ENTRY
 -1 5c1e0a3b9f2d4e7a8b6c1d0e2f3a4b5c Sun 2026-11-30 08:12:03 CET Sun 2026-11-30 18:40:11 CET
  0 a9b8c7d6e5f4a3b2c1d0e9f8a7b6c5d4 Mon 2026-12-01 09:14:02 CET Mon 2026-12-01 10:02:47 CET
```

`-b -1` only works if the journal is **persistent**: stored in `/var/log/journal/` instead of only in memory under `/run/log/journal/`. If `--list-boots` shows a single boot, create the directory with `sudo mkdir -p /var/log/journal` and restart `systemd-journald`. (Journal configuration is covered in 108.2.)

### Classic log files

Systems running rsyslog also write text files. Kernel and boot messages end up in `/var/log/syslog` and `/var/log/kern.log` on Debian and Ubuntu, and in `/var/log/messages` on RHEL and Fedora. RHEL also keeps the console output of services in `/var/log/boot.log`.

## Exam checklist

- Order: firmware (BIOS or UEFI) → boot loader (GRUB) → kernel → initramfs → init (PID 1) → services and login.
- BIOS reads the 512-byte MBR (446 bytes of code). UEFI runs an `.efi` file from the FAT-formatted ESP, usually mounted at `/boot/efi`.
- In the GRUB menu: `e` edits, `Ctrl+X` or `F10` boots the edit, `c` opens the command line. Edits last one boot.
- Kernel options to know: `root=`, `ro`, `rw`, `quiet`, `single` or `1`, `init=/bin/bash`, `systemd.unit=rescue.target`, `systemd.unit=emergency.target`. Check the current ones in `/proc/cmdline`.
- initramfs (older name initrd) is a temporary root filesystem with the drivers needed to mount the real root. Rebuild with `update-initramfs` (Debian) or `dracut` (RHEL).
- SysVinit: `/etc/inittab`, runlevels, sequential scripts in `/etc/init.d/`. Upstart: event-based, Ubuntu 6.10 to 14.10, RHEL 6. systemd: units, targets, parallel start, the default everywhere today.
- `dmesg` shows the kernel ring buffer (`-T` for real times). `journalctl -b` this boot, `-b -1` previous boot, `-k` kernel only, `-p err` errors, `--list-boots`.


## Byte check

1. What is the job of the **initramfs**?
   - It stores the GRUB menu entries
   - **It's a small temporary root filesystem with the drivers and tools needed to find and mount the real root filesystem** (correct)
   - It's the first process the kernel starts, with PID 1
   The kernel unpacks the initramfs into memory, uses it to load storage drivers (or unlock encryption, start LVM), mounts the real root and then hands over to init. GRUB's menu lives in `grub.cfg`.

2. You want to see only the kernel messages from the **previous** boot on a systemd machine with a persistent journal. Which command?
   - `dmesg -b -1`
   - **`journalctl -k -b -1`** (correct)
   - `journalctl --list-boots -k`
   `-k` limits the journal to kernel messages and `-b -1` picks the boot before the current one. `dmesg` only holds the current boot's ring buffer.

3. In the GRUB 2 menu you pressed `e` and added `systemd.unit=rescue.target` to the `linux` line. How do you boot with that change?
   - **Press `Ctrl+X` or `F10`** (correct)
   - Press `Esc`
   - Type `:wq`
   `Ctrl+X` or `F10` boots the edited entry once. `Esc` throws your edits away and returns to the menu. The change is never saved to disk.


