Table of Contents
Role of GRUB2 in the Linux Boot Chain
GRUB2 (GRand Unified Bootloader, version 2) is the stage between your firmware/bootloader and the kernel. In a typical modern Linux system:
- Firmware (BIOS/UEFI) runs and finds the boot device.
- GRUB2 is loaded from disk (or from the EFI System Partition on UEFI).
- GRUB2 presents a menu, loads the kernel and initramfs, and passes control to the kernel with given parameters.
In this chapter we focus on how GRUB2 is organized, how it’s configured, and how you can interact with it and fix common issues.
GRUB2 Architecture and Components
GRUB2 is modular and has several parts; their exact layout depends on BIOS vs UEFI, but the concepts are similar.
Main Components
- Boot image / core image
- On BIOS systems:
boot.imglives in the MBR (first 512 bytes) of the disk; it’s extremely small.core.imgis placed in the “post-MBR gap” or in a dedicated BIOS Boot Partition on GPT disks.- On UEFI systems:
- A single EFI executable like
grubx64.efilives in the EFI System Partition (ESP), usually underEFI/<distro>/grubx64.efi. - GRUB modules
- Stored under something like
/boot/grub/i386-pc/(BIOS) or/boot/grub/x86_64-efi/(UEFI). - Provide features:
- Filesystems:
ext2.mod,btrfs.mod,xfs.mod,fat.mod, etc. - Crypto:
cryptodisk.mod,luks.mod, etc. - RAID, LVM:
mdraid09_be.mod,lvm.mod, etc. - Graphics:
gfxterm.mod,vbe.mod,efi_gop.mod, etc. - GRUB loads only what it needs at runtime.
- Configuration file
- Usually
/boot/grub/grub.cfgor/boot/grub2/grub.cfg(distro-specific path). - Auto-generated on most distributions; you do not normally edit this file directly.
- Environment block
- A small file, typically
/boot/grub/grubenv. - Stores persistent settings such as
saved_entry(last selected menu entry) or boot counter flags. - Modified using
grub-set-default,grub-reboot, and internal GRUB commands likesave_env.
GRUB2 Configuration Files and Layout
While grub.cfg is GRUB’s main runtime configuration, it is generally generated from templates and scripts.
grub.cfg: The Generated Configuration
- Location examples:
- Debian/Ubuntu:
/boot/grub/grub.cfg - RHEL/Fedora/CentOS:
/boot/grub2/grub.cfg - Generated by:
- Debian/Ubuntu:
update-grub(wrapper forgrub-mkconfig) - RHEL/Fedora:
grub2-mkconfig -o /boot/grub2/grub.cfg - Contains:
- Global settings: terminal type, timeout, default entry.
- Menu entries for each kernel installed.
- Possibly entries for other OSes (Windows, older kernels, rescue mode).
You treat grub.cfg as read-only and control it via higher-level configuration.
/etc/default/grub: Primary Tuning Point
This file is used by grub-mkconfig to build grub.cfg. Typical contents:
GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=Ubuntu
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""Common keys:
GRUB_DEFAULT- Which menu entry to boot by default.
- Can be:
- An index, starting at
0 - A menu entry title string
saved(use the entry saved ingrubenv)GRUB_TIMEOUT- Seconds before automatically booting the default entry.
-1means wait indefinitely.GRUB_CMDLINE_LINUX_DEFAULT- Additional kernel command-line parameters used for normal boot.
- Often for visual/comfort options, e.g.
quiet,splash. GRUB_CMDLINE_LINUX- Kernel parameters applied to all entries (normal and recovery).
GRUB_GFXMODE,GRUB_GFXPAYLOAD_LINUX- Control menu resolution and whether the kernel keeps that resolution.
To apply changes:
- Debian/Ubuntu:
- Edit
/etc/default/grub, then:
sudo update-grub- RHEL/Fedora (BIOS):
sudo grub2-mkconfig -o /boot/grub2/grub.cfg- RHEL/Fedora (UEFI; path may differ by distro):
sudo grub2-mkconfig -o /boot/efi/EFI/<distro>/grub.cfg/etc/grub.d/ Scripts
On many distributions, /etc/grub.d/ contains scripts that assemble the final grub.cfg. Examples:
00_header– sets up GRUB environment, general variables.10_linux– adds entries for installed Linux kernels.20_linux_xen– Xen-specific entries (if applicable).30_os-prober– adds entries for other OSes found on disks.40_custom– place custom entries here.
These scripts output GRUB configuration syntax to stdout; grub-mkconfig runs them in order and concatenates the results.
For most users:
- You don’t modify the numbered files like
10_linux. - To add a custom entry:
- Edit
/etc/grub.d/40_custom. - Add your custom menu entry.
- Run
grub-mkconfig(orupdate-grub) to regenerategrub.cfg.
GRUB2 Language Basics
GRUB2’s configuration language is shell-like but simplified. Some core constructs:
set, terminal, and environment
setassigns variables:
set default=0
set timeout=5
set root='hd0,msdos1'- Variables can be referenced with
${var}or$var.
if, else, fi (Basic Logic)
GRUB2 supports if statements:
if [ "$grub_platform" = "efi" ]; then
set timeout=3
else
set timeout=5
fi
Tests use GRUB’s built‑in [ ] command, not the shell’s; syntax is similar but not identical.
menuentry Blocks
Each boot option is defined by a menuentry block:
menuentry 'My Linux' --class gnu-linux --class gnu --class os {
insmod gzio
insmod part_gpt
insmod ext2
set root='hd0,gpt2'
linux /vmlinuz-6.1.0 root=UUID=1234-5678 ro quiet
initrd /initrd.img-6.1.0
}Within the block:
insmodloads GRUB modules.set root=...tells GRUB where the kernel/initramfs and other files are.linuxloads the kernel and passes parameters.initrdloads the initramfs.
Device and Partition Naming in GRUB2
GRUB2 uses its own naming scheme for devices and partitions.
Disk and Partition Syntax
Format:
hdX,part(BIOS/MBR & GPT)hd0– first disk,hd1– second disk, etc.- Partition naming:
- MBR:
msdos1,msdos2, … - GPT:
gpt1,gpt2, …
Examples:
- First GPT disk, second partition:
hd0,gpt2 - Second MBR disk, first partition:
hd1,msdos1
A full root specification often looks like:
set root='hd0,gpt2'
You can also refer by UUID or label using GRUB commands, but this is usually handled automatically when grub.cfg is generated.
Interacting with the GRUB2 Menu
When the system starts and GRUB loads, you typically see:
- A menu of boot entries.
- A timeout counter before the default entry boots.
Key interactions (may vary slightly by distro):
- Arrow keys – select a different entry.
Enter– boot selected entry.e– edit the highlighted menu entry temporarily.c– enter the GRUB command-line.Esc– go back to the menu from certain sub-screens.
These changes are temporary and lost on next boot unless saved via persistent configuration.
Temporarily Editing Boot Parameters (On-Demand Debugging)
Modifying parameters from the GRUB menu is a common way to debug boot problems without permanently changing configuration.
Editing a Menu Entry
- Select your kernel entry with the arrow keys.
- Press
eto edit. - You’ll see a small editor with lines like:
linux /boot/vmlinuz-... root=UUID=... ro quiet splash
initrd /boot/initrd.img-...- Use arrow keys to navigate,
Home/Endto jump to line starts/ends (keys vary by system). - Add or remove kernel parameters on the
linuxline, for example: - Remove
quietandsplashto see verbose boot messages. - Add
systemd.unit=rescue.targetto boot into rescue mode. - Add
nomodesetto work around graphics-driver issues. - Press
Ctrl+xorF10to boot with the edited entry.
This does not modify grub.cfg; it only affects the current boot.
Kernel Parameters From GRUB2
GRUB is responsible for supplying the kernel’s command-line. It’s common to add or adjust parameters via /etc/default/grub.
Examples:
- To always show detailed boot messages:
GRUB_CMDLINE_LINUX_DEFAULT="verbose"- To add debug parameters, e.g.
debugorloglevel=7:
GRUB_CMDLINE_LINUX_DEFAULT="quiet loglevel=7"
After editing, regenerate grub.cfg and reboot.
GRUB2 on BIOS vs UEFI Systems
While GRUB2 itself behaves similarly, installation and locations differ.
BIOS (Legacy) Setup
- The disk’s MBR contains
boot.img. core.imgis placed either:- In the free space after the MBR (post‑MBR gap), or
- In a small dedicated BIOS Boot Partition on GPT disks (usually type
EF02). core.imgloads modules from/boot/grub/...and readsgrub.cfg.
Installation example (conceptual):
sudo grub-install /dev/sda
sudo grub-mkconfig -o /boot/grub/grub.cfg
grub-install writes GRUB to the MBR and related sectors, not to a partition like /dev/sda1.
UEFI Setup
- UEFI firmware reads an EFI executable from the EFI System Partition (ESP), usually a FAT32 partition with type
EF00. - Typical ESP mount point:
/boot/efi. - GRUB’s EFI binary is often installed as:
/boot/efi/EFI/<distro>/grubx64.efi(name and path may vary).- UEFI keeps boot entries in NVRAM;
efibootmgris used to view and adjust them.
Installation example (conceptual):
sudo grub-install --target=x86_64-efi --efi-directory=/boot/efi \
--bootloader-id=GRUB
sudo grub-mkconfig -o /boot/efi/EFI/GRUB/grub.cfgDetails vary by distro, but the pattern is:
- Put the GRUB EFI binary on the ESP.
- Tell UEFI firmware about it.
- Generate a
grub.cfgsomewhere GRUB knows to look.
Persistent Defaults: GRUB_DEFAULT and Saved Entries
GRUB2 can either boot a fixed default entry or remember the last used entry.
Fixed Default by Index or Name
In /etc/default/grub:
- By index:
GRUB_DEFAULT=0 # first entry in grub.cfg- By title:
GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 6.1.0-26"The title must match exactly (including spaces).
Using Saved Default
To always boot the last selected entry:
- Set in
/etc/default/grub:
GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true- Regenerate
grub.cfg.
Now:
- GRUB will read the
saved_entryfromgrubenv. - Whenever you select an entry and boot it, GRUB saves that as the new
saved_entry.
Alternatively, from the OS:
grub-set-default <ENTRY>– set a new persistent default.grub-reboot <ENTRY>– set the default only for the next boot.
<ENTRY> can be an index or title, similar to GRUB_DEFAULT.
GRUB Command Line and Manual Recovery
The GRUB command-line is useful to investigate or manually boot when the menu is broken.
Entering the GRUB Command Line
- From the menu screen, press
c.
You’ll see a prompt like:
grub>Useful commands include:
ls– list devices and partitions recognized by GRUB:
grub> ls
(hd0) (hd0,gpt1) (hd0,gpt2) (hd1) (hd1,gpt1)ls (hd0,gpt2)/– list contents of a partition.set– display current variables (such asroot,prefix).help– list available commands (orhelp <cmd>).
Manually Booting a System
A typical manual boot sequence:
- Identify the root partition:
grub> ls
grub> ls (hd0,gpt2)/- Set root and prefix:
grub> set root=(hd0,gpt2)
grub> set prefix=(hd0,gpt2)/boot/grub- (Optional) Load normal mode and config:
grub> insmod normal
grub> normalThis often brings you back to a menu.
- Or load kernel and initrd manually, for example:
grub> linux /boot/vmlinuz-6.1.0 root=UUID=1234-5678 ro
grub> initrd /boot/initrd.img-6.1.0
grub> bootThis doesn’t fix the configuration permanently but can rescue a boot so you can repair from the running system.
Common Adjustments and Tweaks
Changing the Menu Timeout and Visibility
In /etc/default/grub:
- Hide the menu unless
ShiftorEscis pressed:
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=5- Always show the menu for 10 seconds:
GRUB_TIMEOUT_STYLE=menu
GRUB_TIMEOUT=10
Regenerate grub.cfg after editing.
Changing Resolution and Appearance
Basic options in /etc/default/grub:
GRUB_GFXMODE=1024x768
GRUB_GFXPAYLOAD_LINUX=keep
GRUB_THEME=/boot/grub/themes/mytheme/theme.txtGRUB_GFXMODE– menu resolution (if supported by firmware).GRUB_GFXPAYLOAD_LINUX=keep– keep that resolution when handing off to the kernel.GRUB_THEME– use a custom theme (if available on your system).
After changes, regenerate grub.cfg.
GRUB2 and Encrypted/Complex Storage
GRUB2 supports:
- LUKS-encrypted partitions (with
cryptodisk/luksmodules). - LVM volumes (with
lvmmodule). - Software RAID (with
mdraidmodules).
Basic behavior:
- GRUB loads necessary modules (e.g.
insmod luks,insmod lvm). - Prompts for a passphrase if the root filesystem is encrypted.
- Opens the encrypted container and then accesses the kernel and initramfs inside it.
The exact configuration is typically generated by the distro’s tools when you install with encryption, so you rarely need to edit this part directly, but it explains why GRUB may ask for a passphrase before the kernel even starts.
GRUB2 and Multiple Operating Systems
When multiple OSes are installed:
os-prober(if enabled) scans disks for other OS installations.grub-mkconfigadds menu entries for them (e.g. Windows, another Linux).
Common scenarios:
- Dual-boot with Windows:
- GRUB adds a “Windows Boot Manager” or similar entry.
- GRUB chainloads the Windows EFI or boot partition.
- Multi-boot Linux:
- Each installed Linux kernel can appear as an entry.
If you do not see other OSes:
- Ensure
os-proberis installed and enabled (some distros disable it by default for safety). - Regenerate
grub.cfg.
Safety Considerations
- Backups: Before major changes (e.g. editing GRUB scripts), back up:
/etc/default/grub- Relevant files in
/etc/grub.d/ - Do not edit
/boot/grub/grub.cfgdirectly unless you specifically know why; your changes will be lost on the next regeneration and errors can make the system unbootable. - Have a live USB: If GRUB becomes unusable, you may need to:
- Boot from a live Linux environment.
- Chroot into your installation.
- Reinstall GRUB and regenerate configuration.
Understanding GRUB2’s layout, configuration model, and interactive tools makes it far easier to customize the boot process and recover from boot-related problems.