3.3 GRUB 2 Configuration & Troubleshooting (102.2)
Key Takeaways
- The primary GRUB 2 configuration file is /boot/grub/grub.cfg (or /boot/grub2/grub.cfg on RHEL/CentOS); it is auto-generated and must NEVER be edited directly.
- System administrators customize GRUB 2 settings by editing /etc/default/grub and adding modular executable scripts to /etc/grub.d/.
- Configuration changes are compiled into grub.cfg using the grub-mkconfig -o command (or update-grub on Debian/Ubuntu).
- Scripts located in /etc/grub.d/ must have their execute bit set (chmod +x) to be executed by grub-mkconfig.
- Emergency recovery parameters like init=/bin/bash or rd.break are appended to the linux kernel line by pressing 'e' at the interactive GRUB 2 boot menu.
3.3 GRUB 2 Configuration & Troubleshooting (102.2)
Quick Summary: GRUB 2 (Grand Unified Bootloader version 2) is the default bootloader for modern Linux distributions. Unlike its legacy predecessor, GRUB 2 relies on an auto-generated configuration file located at
/boot/grub/grub.cfg(or/boot/grub2/grub.cfg). Administrators control boot parameters, timeouts, and default kernels by editing/etc/default/gruband scripts in/etc/grub.d/, compiling them withgrub-mkconfig. Mastering interactive editing (e), the GRUB command shell (c), and rescue recovery commands is essential for system maintenance and LPIC-1 certification.
1. GRUB 2 Architecture and Component Hierarchy
GRUB 2 separates configuration source files from the active runtime configuration file. Direct manual edits to /boot/grub/grub.cfg will be completely overwritten during the next kernel update or configuration generation.
┌─────────────────────────┐ ┌─────────────────────────┐
│ /etc/default/grub │ │ /etc/grub.d/ │
│ Key-Value Global Vars │ │ 00_header, 10_linux... │
│ (TIMEOUT, CMDLINE...) │ │ Executable Gen Scripts │
└────────────┬────────────┘ └────────────┬────────────┘
│ │
└───────────────┬────────────────┘
▼
[ grub-mkconfig -o ... ]
[ (or update-grub) ]
│
▼
┌─────────────────────────┐
│ /boot/grub/grub.cfg │
│ (DO NOT EDIT DIRECT!) │
└─────────────────────────┘
Primary Configuration File Paths
- Debian / Ubuntu / openSUSE:
/boot/grub/grub.cfg - RHEL / CentOS / Fedora / Rocky Linux (BIOS):
/boot/grub2/grub.cfg - RHEL / CentOS / Fedora (UEFI):
/boot/efi/EFI/redhat/grub.cfgor/boot/efi/EFI/centos/grub.cfg(or/boot/grub2/grub.cfgsymlinked in modern releases)
2. Configuration Templates: /etc/default/grub
The /etc/default/grub file contains global variables that define bootloader behavior, visual themes, and kernel command-line parameters.
# Sample /etc/default/grub configuration
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release 2>/dev/null || true)"
GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT="true"
GRUB_DISABLE_SUBMENU="true"
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/sda2 quiet splash"
GRUB_DISABLE_RECOVERY="true"
Key Directives Reference
| Directive | Value Syntax | Operational Effect |
|---|---|---|
GRUB_DEFAULT | 0, saved, or "entry_title" | Specifies which menu entry boots by default. 0 boots the first entry; saved boots the entry selected by grub-set-default or grub-reboot. |
GRUB_TIMEOUT | Integer (e.g., 5, 10, 0, -1) | Number of seconds the boot menu displays before booting GRUB_DEFAULT. -1 waits indefinitely; 0 boots immediately without displaying the menu. |
GRUB_TIMEOUT_STYLE | menu, countdown, or hidden | menu displays the full list; hidden hides the menu unless Esc or Shift is pressed. |
GRUB_CMDLINE_LINUX | Quoted string | Arguments passed to the Linux kernel for all standard boot entries (e.g., "quiet splash console=ttyS0"). |
GRUB_CMDLINE_LINUX_DEFAULT | Quoted string | Arguments appended only to normal (non-recovery) boot entries. |
GRUB_DISABLE_RECOVERY | "true" or "false" | When "true", suppresses the generation of single-user recovery mode menu entries. |
GRUB_SAVEDEFAULT | "true" or "false" | When "true" and GRUB_DEFAULT=saved, GRUB remembers the last manually booted menu entry as the new default. |
3. Modular Script Generators: /etc/grub.d/
The /etc/grub.d/ directory contains shell scripts executed in alphanumerical order by grub-mkconfig to construct grub.cfg.
00_header: Sets environmental variables, terminal displays, graphic modes, and timeout settings from/etc/default/grub.10_linux: Scans/bootfor installed Linux kernels (vmlinuz-*) and creates corresponding boot menu entries.20_memtest86: Detects standalone memory testing binaries (e.g., Memtest86+) and adds diagnostic menu entries.30_os-prober: Executesos-proberto scan all attached storage drives for other operating systems (Windows, other Linux distributions) and generates dual-boot menu entries.40_custom: A template script where administrators can append static, custom boot stanzas.41_custom: Automatically sources/boot/grub/custom.cfgat boot time if it exists.
⚠️ LPIC-1 Trap: Any script in
/etc/grub.d/MUST have executable permissions (chmod +x /etc/grub.d/*). If a script lacks the+xbit,grub-mkconfigsilently ignores it without generating an error, and its entries will not appear ingrub.cfg.
4. Generating Configuration and Installing the Bootloader
Compiling the GRUB 2 Configuration
# Standard generic command (RHEL, Fedora, CentOS, openSUSE)
sudo grub-mkconfig -o /boot/grub/grub.cfg
# OR on RHEL/CentOS systems
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
# Debian / Ubuntu wrapper command
sudo update-grub
Installing the Bootloader to Disk
grub-install writes the core bootloader code and stage images to the Master Boot Record (MBR) or EFI System Partition (ESP).
# BIOS / MBR Installation (target the WHOLE DISK, not a partition)
sudo grub-install /dev/sda
# OR
sudo grub2-install /dev/sda
# UEFI Installation
sudo grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB
💡 LPIC-1 Exam Fill-in-the-Blank Alert: When asked for the exact command to install GRUB 2 to the master boot record of the primary SATA drive, the command is
grub-install /dev/sda(never specify partition numbers like/dev/sda1for BIOS MBR installs).
5. Interactive Boot Menu Operations & Troubleshooting
When the system boots, the GRUB 2 visual menu presents several key interactive controls:
e(Edit): Opens the inline editor for the highlighted boot entry. Allows administrators to modify kernel boot arguments dynamically for a single boot without modifyinggrub.cfg.c(Command-Line): Drops into the full interactive GRUB 2 command shell (grub>).Ctrl+XorF10: Boots the system with the edited in-memory configuration.Esc: Aborts editing and returns to the main menu.
Critical Kernel Override Arguments for Troubleshooting
To recover a system with a forgotten root password or broken systemd service, press e at the GRUB menu, navigate to the line beginning with linux (or linux16 / linuxefi), and append one of the following arguments:
# Bypass init and spawn a direct root shell
linux /vmlinuz-5.14.0 root=/dev/sda2 ro init=/bin/bash
# Boot into single-user / rescue target (systemd)
linux /vmlinuz-5.14.0 root=/dev/sda2 ro systemd.unit=rescue.target
# Emergency breakpoint in initramfs (RHEL/CentOS)
linux /vmlinuz-5.14.0 root=/dev/sda2 ro rd.break
6. GRUB Rescue Mode Shell Recovery
If GRUB 2 cannot find its configuration file or secondary stage modules in /boot/grub, it drops into the minimal grub rescue> shell.
# 1. List all detected storage devices and partitions
grub rescue> ls
(hd0) (hd0,msdos1) (hd0,msdos2)
# 2. Browse filesystems to find the boot directory and grub modules
grub rescue> ls (hd0,msdos1)/
grub rescue> ls (hd0,msdos1)/boot/grub
# 3. Set the root and prefix environment variables
grub rescue> set root=(hd0,msdos1)
grub rescue> set prefix=(hd0,msdos1)/boot/grub
# 4. Load the normal execution module and launch the standard menu
grub rescue> insmod normal
grub rescue> normal
Once the system boots successfully into Linux, the administrator must repair the bootloader permanently by running grub-install /dev/sda followed by grub-mkconfig -o /boot/grub/grub.cfg.
A system administrator needs to permanently add the kernel parameter 'console=ttyS0,115200' to all Linux boot entries on a server running GRUB 2. What is the correct procedure?
An administrator created a custom script named /etc/grub.d/50_custom_iso to boot diagnostic ISO images, but running grub-mkconfig fails to include the new menu entry in /boot/grub/grub.cfg. What is the most likely cause?
A Linux server fails to boot and drops into the 'grub rescue>' shell. The administrator identifies that the /boot/grub directory is located on partition (hd0,msdos1). Which command sequence restores normal GRUB operation from the rescue prompt?