Linux › Section E · How the Linux kernel boots › Topic E.3
Boot loaders and GRUB
Before the kernel can run, something has to find it on disk, load it into memory and hand it its parameters. On most Linux PCs that something is GRUB. Knowing its menu, its command line and how its configuration is generated lets you rescue a machine that will not boot.
The boot loader's puzzle
A boot loader must load the kernel, and usually an initial RAM filesystem, and start the kernel with a command line. That sounds simple, but the kernel file sits on a filesystem, and the kernel's own disk and filesystem drivers are not running yet. Boot loaders solve this in two ways. They read raw sectors through the firmware, BIOS or UEFI, which can address any block on a disk by number. And they carry their own small, read-only drivers for partition tables and common filesystems, so they can find /boot/vmlinuz by name. These drivers are separate from the kernel's and much simpler.
What boot loaders do, and which exist
A Linux boot loader lets you choose between installed kernels, choose or edit kernel parameters for one boot (for instance to reach a rescue mode) and start other operating systems. GRUB 2, the GRand Unified Bootloader, is the near-universal choice, built in separate versions for BIOS and UEFI machines; the older GRUB Legacy and LILO are obsolete. systemd-boot is a simple UEFI-only boot manager that reads one small text file per kernel. A kernel can also be built as an EFI program, the EFI stub, so firmware starts it directly. To tell which firmware you have, check for the directory /sys/firmware/efi: it exists only on UEFI boots.
Reaching the GRUB menu
Many distributions hide the GRUB menu and boot straight away. The setting GRUB_TIMEOUT_STYLE decides this: with menu it is shown; with hidden or countdown GRUB waits silently, and holding Shift or pressing Esc or F4 during that wait shows the menu. On fast machines the window is short, so start pressing as soon as the firmware logo disappears. In the menu, the arrow keys and Enter choose an entry. e opens the selected entry in an editor, Ctrl-x boots the edited version and Esc discards the edits. c opens the GRUB command line. Edits made here last for one boot only, which makes them a safe way to test.
Inside a menu entry
Pressing e shows lines like these (illustrative): insmod part_gpt, insmod ext2, search --no-floppy --fs-uuid --set=root 2f6c...e91a, linux /boot/vmlinuz-6.8.0-45-generic root=UUID=2f6c...e91a ro quiet splash, initrd /boot/initrd.img-6.8.0-45-generic. GRUB has its own modules, loaded with insmod, here for GPT tables and ext2-family filesystems (which covers ext4). search finds the partition with that filesystem UUID and stores it in GRUB's root variable. linux names the kernel file and, after it, the kernel command line. initrd names the initial RAM filesystem. None of these commands belong to Linux: they run inside GRUB before Linux exists.
Two different roots
The word root appears twice in that entry with different meanings. GRUB's root variable is the device GRUB reads files from, so the path /boot/vmlinuz-... is looked up there. The root=UUID=... after the kernel file is a kernel parameter: the filesystem Linux will mount as /. They are often the same filesystem but need not be, for instance when /boot is a separate partition. GRUB names devices its own way: disks count from 0 and partitions from 1, with the table type in front. (hd0) is the first disk, (hd0,gpt2) its second GPT partition and (hd1,msdos1) the first MBR partition of the second disk. These numbers can differ from Linux's names, which is why search by UUID is used.
Worked example: booting by hand
The menu is missing, and a bare grub> prompt appears. 1. Type ls to list devices: (illustrative) (hd0) (hd0,gpt1) (hd0,gpt2). 2. Look inside: ls (hd0,gpt2)/boot/ shows vmlinuz-6.8.0-45-generic and initrd.img-6.8.0-45-generic, so the kernels are on partition 2. 3. Type set root=(hd0,gpt2) so later paths refer to it; set on its own lists every variable, and echo $root prints one. 4. Load the kernel: linux /boot/vmlinuz-6.8.0-45-generic root=/dev/sda2 ro, guessing the Linux name or, better, using the UUID that ls -l reports. 5. Load the initramfs: initrd /boot/initrd.img-6.8.0-45-generic. 6. Type boot. Once Linux is up, regenerate the configuration so the menu returns.
How the configuration is generated
GRUB reads grub.cfg, found in /boot/grub on Debian and Ubuntu and in /boot/grub2 on Fedora and RHEL. Do not edit it by hand: it is generated and will be overwritten. Settings go in /etc/default/grub, for example GRUB_DEFAULT=0 (the first entry, counting from zero), GRUB_TIMEOUT=5 (seconds) and GRUB_CMDLINE_LINUX for parameters on every Linux entry. GRUB_CMDLINE_LINUX_DEFAULT adds parameters to normal entries but not to recovery ones. The scripts in /etc/grub.d, such as 10_linux, build the menu; your own entries go in 40_custom. Regenerate with update-grub on Debian and Ubuntu, a wrapper for grub-mkconfig -o /boot/grub/grub.cfg, or grub2-mkconfig -o /boot/grub2/grub.cfg on Fedora and RHEL.
Worked example: always show the menu
Task: on an Ubuntu machine, show the GRUB menu for 10 seconds at every boot. 1. Back up the settings: cp /etc/default/grub /etc/default/grub.bak. 2. Edit /etc/default/grub so it contains GRUB_TIMEOUT_STYLE=menu and GRUB_TIMEOUT=10. 3. Run update-grub; it reports each kernel it found as it writes /boot/grub/grub.cfg. 4. Check the result: grep timeout /boot/grub/grub.cfg should now show set timeout=10. 5. Reboot and confirm the menu appears and counts down. Running grub-mkconfig without -o is also safe: it only prints the configuration it would write, which is a useful preview.
Distribution differences and installing
Fedora and RHEL add a layer: each kernel has a small entry file in /boot/loader/entries, following the Boot Loader Specification, and the grubby tool edits those entries. Running grub2-mkconfig is then rarely needed. Since GRUB 2.06, upstream grub-mkconfig no longer runs os-prober to add other operating systems unless GRUB_DISABLE_OS_PROBER=false is set, though some distributions change this default. Installing GRUB's boot code is a separate job from configuring it, done by grub-install and normally by the installer. Warning: grub-install on the wrong disk, or the wrong firmware type, can leave a machine unbootable. Have rescue media ready; the topic "Boot loader details: MBR and UEFI boot" explains where the code goes.
Common mistakes to avoid
First, editing grub.cfg directly and losing the change at the next kernel update. Second, changing /etc/default/grub but not running update-grub or grub2-mkconfig, so nothing changes. Third, confusing GRUB's root variable with the kernel's root= parameter. Fourth, forgetting that GRUB counts disks from 0 and partitions from 1. Fifth, making a permanent change before testing it once from the menu editor. Sixth, expecting Windows to appear in the menu automatically when os-prober is disabled, the upstream default.
Key takeaways
A boot loader reads the disk through firmware with its own simple filesystem drivers, then loads the kernel and initramfs and passes the command line. GRUB 2 dominates; systemd-boot and the EFI stub are UEFI alternatives. Show the menu with Shift, Esc or F4; e edits, Ctrl-x boots, c gives a command line, and edits last one boot. In an entry, search sets GRUB's root, linux loads the kernel with its parameters and initrd loads the initramfs. GRUB names devices like (hd0,gpt2). Change /etc/default/grub, then run update-grub or grub2-mkconfig; never edit grub.cfg.
Flashcards
Flashcard
Loads the kernel and initramfs and starts the kernel with its command line
Test your understanding
Question 1 of 5
You added a parameter in the GRUB menu editor and booted with Ctrl-x. What happens at the next reboot?
Question 2 of 5
In a GRUB entry, linux /vmlinuz-6.8.0 root=UUID=2f6c... ro follows set root=(hd0,gpt1). What does each root mean?
Question 3 of 5
On Ubuntu you set GRUB_TIMEOUT=10 in /etc/default/grub and rebooted, but nothing changed. What was missed?
Question 4 of 5
In GRUB's naming, what is (hd1,gpt3)?
Question 5 of 5
Which statement about grub.cfg is correct?
