Menu
Install a boot manager
“GRUB Legacy and GRUB 2: where they live, how to configure and install them, and how to talk to them at boot time.”
The boot loader is the first program that starts from your disk. The firmware hands over control, and the boot loader finds the kernel, loads it with the initramfs, passes it options and lets it run. If it’s broken or misconfigured, the system doesn’t boot at all, no matter how healthy everything else is.
Almost every Linux system uses GRUB (GRand Unified Bootloader). The exam covers two generations: GRUB Legacy (version 0.97, on old systems you’ll still meet) and GRUB 2 (everything current). You need to know how to install both, how to change their settings and how to use them interactively when something goes wrong.
Where the boot loader lives#
BIOS and the MBR. A BIOS reads the first sector of the boot disk, the Master Boot Record (MBR), and runs it. That sector is only 512 bytes:
| Bytes | Content |
|---|---|
| 446 | Boot code (GRUB’s first stage) |
| 64 | Partition table: 4 entries of 16 bytes |
| 2 | Signature 55 AA |
446 bytes is far too small for GRUB, so the first stage just loads the next one. GRUB 2 puts its core.img in the gap between the MBR and the first partition (or in a BIOS boot partition on GPT disks), and the rest in /boot/grub/.
UEFI. There’s no boot code in the MBR. The firmware reads a .efi program straight from the FAT-formatted EFI System Partition (ESP), for example /boot/efi/EFI/debian/grubx64.efi. With Secure Boot, the firmware first loads shimx64.efi, which is signed by Microsoft, and shim then checks and loads GRUB. The firmware keeps a list of boot entries in NVRAM; efibootmgr shows it:
efibootmgrBootCurrent: 0001
BootOrder: 0001,0000
Boot0000* UiApp
Boot0001* debian HD(1,GPT,...)/File(\EFI\debian\shimx64.efi)Before you experiment with an MBR, make a backup. The first command saves all 512 bytes; the restore with count=446 writes back only the boot code and leaves the partition table alone:
sudo dd if=/dev/sda of=/root/mbr.bak bs=512 count=1
sudo dd if=/root/mbr.bak of=/dev/sda bs=446 count=1GRUB Legacy#
GRUB Legacy reads one file, /boot/grub/menu.lst. Red Hat called it /boot/grub/grub.conf (with menu.lst and /etc/grub.conf as links to it). You edit it directly, and the change counts at the next boot: there’s nothing to regenerate.
default 0
timeout 5
# password --md5 $1$... (protects menu editing)
title Debian GNU/Linux, kernel 2.6.32
root (hd0,0)
kernel /vmlinuz-2.6.32-5-amd64 root=/dev/sda2 ro quiet
initrd /initrd.img-2.6.32-5-amd64
title Windows
rootnoverify (hd0,1)
chainloader +1defaultis the entry booted when nobody picks one, counted from 0.timeoutis in seconds.titlestarts a menu entry.root (hd0,0)is the partition that holds/boot. GRUB Legacy counts disks and partitions from 0, so(hd0,0)is/dev/sda1.kernelloads the kernel and passes it options;root=there is the Linux root filesystem.initrdloads the initial RAM disk.chainloader +1hands over to another boot loader, here the first sector of a Windows partition.
To install GRUB Legacy into the MBR, run grub-install /dev/sda, or use the GRUB shell:
# grub
grub> root (hd0,0)
grub> setup (hd0)
grub> quitGRUB 2#
GRUB 2 is more powerful, and its main file is generated rather than written by hand.
| File | What it is |
|---|---|
/boot/grub/grub.cfg (Debian, Ubuntu) | The real config GRUB reads at boot. Don’t edit it. |
/boot/grub2/grub.cfg (RHEL, Fedora, SUSE) | Same file, different folder |
/etc/default/grub | Your settings: timeout, default entry, kernel options |
/etc/grub.d/ | Scripts that build grub.cfg: 10_linux finds kernels, 30_os-prober finds other systems, 40_custom holds your own entries |
A typical /etc/default/grub on Debian:
GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet"
GRUB_CMDLINE_LINUX=""
#GRUB_DISABLE_OS_PROBER=false| Key | Meaning |
|---|---|
GRUB_DEFAULT | Entry to boot: a number (from 0), a title, or saved (remember the last choice, with GRUB_SAVEDEFAULT=true) |
GRUB_TIMEOUT | Seconds the menu waits. -1 waits forever. |
GRUB_TIMEOUT_STYLE | menu, countdown or hidden |
GRUB_CMDLINE_LINUX | Kernel options added to every entry, recovery included |
GRUB_CMDLINE_LINUX_DEFAULT | Extra options for the normal entries only (Debian, Ubuntu) |
GRUB_DISABLE_OS_PROBER | Set to false to let GRUB find Windows and other systems. It’s off by default since GRUB 2.06. |
After a change, rebuild grub.cfg with grub-mkconfig:
# Debian / Ubuntu (update-grub is a short wrapper for the same thing)
sudo grub-mkconfig -o /boot/grub/grub.cfg
sudo update-grub
# RHEL / Fedora: the tools are named grub2-*
sudo grub2-mkconfig -o /boot/grub2/grub.cfgGenerating grub configuration file ...
Found linux image: /boot/vmlinuz-6.1.0-28-amd64
Found initrd image: /boot/initrd.img-6.1.0-28-amd64
doneTo add your own menu entry, append it to /etc/grub.d/40_custom and run grub-mkconfig again. Note one change from Legacy: GRUB 2 counts partitions from 1 (disks still from 0), so /dev/sda1 is (hd0,1), also written (hd0,msdos1) or (hd0,gpt1).
/boot/loader/entries/ (the Boot Loader Specification). There, the easy way to change kernel options is grubby, for example sudo grubby --update-kernel=ALL --args="console=ttyS0".Installing GRUB 2 with grub-install#
grub-mkconfig writes the menu. grub-install puts the boot loader itself on the disk, and copies its modules to /boot/grub/. You need it after a new install, a disk swap, or when another OS overwrote the MBR.
# BIOS: install to the MBR of the disk (not a partition)
sudo grub-install /dev/sdaInstalling for i386-pc platform.
Installation finished. No error reported.# UEFI: install the .efi file into the ESP and add a firmware boot entry
sudo grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debianOn RHEL the BIOS command is grub2-install /dev/sda. On RHEL with UEFI, don’t use grub2-install: reinstall the signed packages instead with sudo dnf reinstall grub2-efi-x64 shim-x64.
Alternative and backup boot locations#
A boot loader in only one place is a single point of failure. Some options:
- Second disk. With software RAID 1, run
grub-install /dev/sdbtoo, so the machine still boots when/dev/sdadies. - Removable / fallback path. UEFI firmware always tries
\EFI\BOOT\BOOTX64.EFIwhen its boot entries are missing or broken.grub-install --removableputs GRUB there, which also helps with firmware that forgets its NVRAM entries. - Partition boot sector. GRUB Legacy could be installed into the first sector of a partition (
setup (hd0,0)) and chain-loaded by another boot loader. GRUB 2 can still do it with--force, but it’s fragile and discouraged. - Another
/bootlocation.grub-install --boot-directory=/mnt/usb/boot /dev/sdcwrites GRUB’s files somewhere else, for example onto a rescue USB stick. - Rescue media. A live USB or the installer’s rescue mode can mount your disk,
chrootinto it and rungrub-installandgrub-mkconfigagain.
Talking to GRUB at boot time#
Most systems hide the menu. Hold Shift (BIOS) or tap Esc (UEFI) while booting to make it appear. Then:
| To do this | GRUB 2 | GRUB Legacy |
|---|---|---|
| Edit the selected entry | e | e |
| Open the GRUB command line | c | c |
| Append options to the kernel line | (edit with e) | a |
| Boot the edited entry | Ctrl+X or F10 | b |
| Throw away your edits | Esc | Esc |
Edits are one-off: they last for this boot only. A classic use is fixing a system that won’t start properly. Find the line starting with linux (GRUB 2) or kernel (Legacy) and add a kernel option at the end:
linux /vmlinuz-6.1.0-28-amd64 root=/dev/mapper/vg-root ro quiet systemd.unit=rescue.targetUseful options: systemd.unit=rescue.target (single-user rescue mode), single or 1 (the same on SysV init), init=/bin/bash (a root shell with no init at all), systemd.unit=multi-user.target (skip the graphical desktop).
At the grub> command line you can boot by hand. Tab completion works, and ls shows disks and files:
grub> ls
(hd0) (hd0,gpt1) (hd0,gpt2) (hd0,gpt3)
grub> set root=(hd0,gpt2)
grub> linux /vmlinuz-6.1.0-28-amd64 root=/dev/sda3 ro
grub> initrd /initrd.img-6.1.0-28-amd64
grub> bootIf GRUB can’t find its own files, it stops at a grub rescue> prompt with very few commands. Point it at the right partition and load the normal module:
grub rescue> set prefix=(hd0,gpt2)/grub
grub rescue> insmod normal
grub rescue> normalNote
Other boot loaders you may hear about: LILO (very old, wrote the kernel’s disk location into the MBR, so you had to rerun lilo after every change), SYSLINUX/ISOLINUX (common on USB sticks and install CDs) and systemd-boot (a small UEFI-only boot manager).
Exam checklist#
- The MBR is the first 512 bytes: 446 bytes boot code, 64 bytes partition table, 2 bytes signature.
- GRUB Legacy:
/boot/grub/menu.lst, orgrub.confon Red Hat. Edit it directly, no regeneration. - GRUB Legacy counts disks and partitions from 0:
(hd0,0)=/dev/sda1. GRUB 2 counts partitions from 1. - GRUB Legacy keywords:
default,timeout,title,root,kernel,initrd,chainloader,password. - GRUB 2: edit
/etc/default/gruband/etc/grub.d/, then rungrub-mkconfig -o /boot/grub/grub.cfg(update-grubon Debian,grub2-mkconfig -o /boot/grub2/grub.cfgon RHEL). - Never edit
grub.cfgby hand: the nextgrub-mkconfigoverwrites it. grub-install /dev/sdawrites the boot loader to the disk. UEFI uses--efi-directory=/boot/efi.- At the menu:
eto edit,cfor a command line, Ctrl+X or F10 to boot. Addsystemd.unit=rescue.target,singleorinit=/bin/bashto the kernel line for repairs. - Back up the MBR with
dd if=/dev/sda of=mbr.bak bs=512 count=1.
Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(lpic-102): Install a boot manager"
[main 321ad32] learn(lpic-102): Install a boot manager
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: