GRUB2 Bootloader – Deep Dive-Free Linux kernel development course

 

Previous Lecture                                                                                                                                                                                              Next Lecture

GRUB2 Bootloader – Deep Dive
Chapter 3 · Linux Kernel Programming · EmbeddedPathashala
🎯 Kernel 6.x Updated
🆓 100% Free Course
💡 Interview Q&A Included

Topics Covered:

GRUB2 Bootloader
Kernel Parameters
Boot Custom Kernel
Single User Mode
Linux Boot Process
GRUB Edit Mode
Root Password Recovery
Kernel 6.x

What Is GRUB and Why Should You Care?

When you press the power button on your Linux machine, a lot happens before you see the login screen. One of the most important players in this sequence is GRUB — the Grand Unified Bootloader.

GRUB’s job is simple to state but critical in practice: it finds the kernel image on disk, loads it into RAM, and hands control over to it. But GRUB can also do much more — it lets you choose which kernel to boot, pass special options to the kernel, and even recover from certain problems like a forgotten root password.

In this tutorial, we focus on the part of the boot process that many students skip — the GRUB menu itself and how you can interact with it at runtime. This is a skill that will serve you throughout your career as a Linux or embedded systems engineer.

How the Boot Sequence Reaches GRUB

Before we interact with GRUB, let’s understand where it fits in the big picture. The boot sequence on an x86 machine (simplified for modern UEFI + kernel 6.x systems) looks like this:

Linux Boot Sequence — From Power-On to Kernel
1. Power ON 2. UEFI / BIOS
POST, hardware init
3. GRUB2
Menu + kernel loader
4. Linux Kernel
Decompress + init
5. init / systemd
PID 1, user space

GRUB sits at step 3 — between firmware and the kernel

On a modern system using UEFI (which is the default on most laptops and desktops since ~2012), GRUB is stored as an EFI application in the EFI System Partition (ESP). On older BIOS-based systems, GRUB’s first-stage loader sits in the Master Boot Record (MBR). Either way, from your point of view as a developer, interacting with GRUB is the same.

The GRUB Menu — Selecting Which Kernel to Boot

When the machine starts, GRUB displays a menu listing all bootable entries. On Ubuntu 22.04 / 24.04 with kernel 6.x, a typical menu looks like this (simplified):

GRUB2 Boot Menu (Typical View)
GNU GRUB version 2.12┌─────────────────────────────────────────────────────────┐
│ * Ubuntu, with Linux 6.8.0-mykernel │
│ Ubuntu, with Linux 6.8.0-mykernel (recovery mode) │
│ Ubuntu, with Linux 6.5.0-45-generic │
│ Ubuntu, with Linux 6.5.0-45-generic (recovery mode) │
└─────────────────────────────────────────────────────────┘Use ↑↓ arrow keys to select. Press ENTER to boot.
Press ‘e’ to edit the selected entry.
Press ‘c’ for a command-line.

The entry marked with * is the default. If you have compiled a custom kernel (for example 6.8.0-mykernel), it appears at the top because it was the most recently installed. Use the arrow keys to highlight your custom entry and press Enter to boot it.

💡 Tip (Kernel 6.x): On Ubuntu 22.04 and later, GRUB may be hidden by default and boot directly. To see the menu, hold Shift (BIOS systems) or press Escape (UEFI systems) during boot.

Editing a Boot Entry — The ‘e’ Key

This is where things get really interesting. When you highlight a GRUB menu entry and press e, GRUB opens an editor showing the raw configuration for that boot entry.

The edit screen contains several lines of GRUB script. The most important one starts with the word linux. It tells GRUB which kernel image to load and what parameters to pass to that kernel.

GRUB Edit Screen — Key Lines Explained
insmod ext2
set root=’hd0,msdos1′

echo ‘Loading Linux 6.8.0-mykernel …’
linux /boot/vmlinuz-6.8.0-mykernel root=UUID=abc123 ro quiet splash ← KEY LINE
echo ‘Loading initial ramdisk …’
initrd /boot/initrd.img-6.8.0-mykernel

Anatomy of the ‘linux’ Line in GRUB
Part Example Value What It Means
linux GRUB command: “load this kernel image”
/boot/vmlinuz-6.8.0-mykernel vmlinuz Path to the compressed kernel image on disk
root=UUID=abc123 UUID Which partition is the root filesystem
ro read-only Mount root as read-only initially (kernel remounts rw later)
quiet Suppress most kernel log messages during boot
splash Show the graphical splash screen instead of text logs

After pressing e, you can change any of these values. When you are done editing, press Ctrl + X or F10 to boot with your changes. These changes are temporary — they only apply to this one boot session. The stored GRUB config on disk is not touched.

💡 Try This: Remove quiet and splash from the linux line and boot. Instead of the Ubuntu logo, you will see all kernel messages scrolling past. This is called verbose boot and is extremely useful when debugging boot problems.

What Are Kernel Parameters?

The values you see after the kernel image path on the linux line are called kernel command-line parameters (also called boot parameters or kernel boot arguments). These are passed by GRUB to the kernel at boot time, and the kernel reads them very early in its startup sequence — before any user-space program runs.

You can view the parameters that your currently running kernel was booted with at any time:

cat /proc/cmdline

On a typical Ubuntu 24.04 system with kernel 6.8.x, the output looks something like:

BOOT_IMAGE=/boot/vmlinuz-6.8.0-45-generic root=UUID=d1a2b3c4-... ro quiet splash

Commonly Used Kernel Boot Parameters
Parameter Effect When to Use
quiet Hides most boot messages Normal boot (cleaner output)
splash Displays graphical boot screen Desktop systems
single or 1 Boot into single-user (rescue) mode Maintenance, password recovery
ro Root filesystem initially read-only Standard — always present
nomodeset Disables kernel mode-setting for GPU When display is broken at boot
init=/bin/bash Starts bash as PID 1 instead of systemd Emergency recovery
debug Enables extra kernel debug messages Kernel development / debugging
loglevel=N Sets kernel log verbosity (0=silent, 7=all) Controlling how much you see at boot

The full list of kernel parameters is documented in the kernel source tree under Documentation/admin-guide/kernel-parameters.txt. As of Linux 6.x, there are hundreds of parameters covering everything from CPU scheduling to memory management to device drivers.

Root Password Recovery Using GRUB

Here is a practical skill that will save you one day: recovering from a forgotten root password without reinstalling the OS.

The trick is to boot the system into single-user mode (also called runlevel 1 or rescue mode). In this mode, the system starts up minimally and drops you into a root shell without asking for a password.

Here is how to do it step by step:

1

At the GRUB menu, highlight the kernel entry you want to boot. Press e to open the editor.

2

Find the line that starts with the word linux. Navigate to its end.

3

Add the word single (or just the number 1) at the very end of that line:
linux  /boot/vmlinuz-6.8.0-mykernel root=UUID=abc123 ro quiet splash single

4

Press Ctrl + X to boot. The system starts in single-user mode and gives you a root shell.

5

Now change the password:
passwd username

6

Reboot normally: reboot
⚠️ Security Note: This technique reveals something important: if an attacker has physical access to a machine and can reach the GRUB menu, they can get root access without knowing any password. This is why high-security environments must password-protect GRUB itself (using grub-mkpasswd-pbkdf2) and lock down physical access to servers.
💡 Kernel 6.x Note: On Ubuntu 22.04/24.04 with systemd, single-user mode may ask for the root password. In that case, use init=/bin/bash instead of single on the kernel command line for true password-bypass recovery. Then remount root as read-write with mount -o remount,rw / before changing passwords.

Making Permanent Changes to GRUB

The GRUB edits you make by pressing e are one-time-only. They last only for that single boot session. To make permanent changes, you edit the GRUB configuration file.

On modern Debian/Ubuntu systems, the main GRUB configuration file that you should edit is:

/etc/default/grub

Some important options inside this file:

# Set the default boot entry (0 = first entry)
GRUB_DEFAULT=0

# How long to show the menu before auto-booting (seconds)
GRUB_TIMEOUT=5

# Command-line options passed to the kernel for normal boots
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

# Options passed to ALL boot entries (including recovery)
GRUB_CMDLINE_LINUX=""

After editing this file, you must regenerate the actual GRUB config file that the bootloader reads:

sudo update-grub
💡 What update-grub actually does: It runs grub-mkconfig and writes the result to /boot/grub/grub.cfg. That file is the one GRUB reads at boot time. You should never edit /boot/grub/grub.cfg directly — it gets overwritten every time you run update-grub or install a new kernel.

Interview Questions & Answers

These are questions commonly asked in embedded Linux and kernel development interviews related to the GRUB bootloader topic.

Q1. What is GRUB and what role does it play in the Linux boot process?
GRUB (Grand Unified Bootloader) is the first software that runs after the firmware (UEFI/BIOS) finishes its hardware initialization. Its main job is to locate the Linux kernel image on disk, load it into RAM along with the initial ramdisk (initrd/initramfs), and then pass control to the kernel. It also presents a menu that allows you to choose between multiple installed kernels or operating systems.
Q2. What are kernel command-line parameters and how can you view them on a running system?
Kernel command-line parameters are arguments passed by the bootloader to the kernel at boot time. They control many aspects of kernel behavior such as which filesystem to mount as root, verbosity of boot messages, and the mode in which the system starts. On a running system, you can view the parameters the kernel was booted with by reading the /proc/cmdline file.
Q3. How is /etc/default/grub different from /boot/grub/grub.cfg?
/etc/default/grub is the human-editable configuration file where you set high-level options like timeout and kernel parameters. It is a source file. /boot/grub/grub.cfg is the actual file that GRUB reads at boot time — it is auto-generated by running update-grub (or grub-mkconfig) and should never be edited manually, as it gets overwritten on every kernel installation or GRUB update.
Q4. What is single-user mode and when would you use it?
Single-user mode (runlevel 1) is a minimal boot state where the system starts with only essential services and gives you a root shell. It is primarily used for system maintenance and recovery tasks such as resetting a forgotten root password, repairing a broken filesystem, or investigating boot failures. You enter it by appending single or 1 to the kernel command line in GRUB.
Q5. Why is physical security of a server considered an important aspect of Linux system security?
Because if an attacker has physical access to a machine, they can reboot it, access the GRUB menu, and boot into a rescue mode that gives them root-level access without knowing any password. This bypasses all software-level security. Proper security in high-security environments involves password-protecting GRUB itself, encrypting the disk, restricting physical access to the machine, and potentially disabling boot from removable media in the firmware settings.
Q6. What is the difference between the quiet and splash kernel parameters?
quiet suppresses most kernel log messages from appearing on the console during boot, reducing the log level so only critical messages are shown. splash is a desktop-environment parameter that tells the boot system to show a graphical splash screen (like the Ubuntu logo with a progress bar) instead of a text console. Removing both parameters gives you verbose boot output, showing every kernel message as the system starts — this is very useful for debugging kernel or driver issues during boot.

Continue Your Linux Kernel Journey

Next up: Verifying your custom kernel configuration after boot →

Previous Lecture                                                                                                                                                                                                  Next Lecture

Leave a Reply

Your email address will not be published. Required fields are marked *