Menu

Linux Installation and Package Management

Step 2 of 6 LPIC-1 102.2weight 2

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.”

View .md

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:

BytesContent
446Boot code (GRUB’s first stage)
64Partition table: 4 entries of 16 bytes
2Signature 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:

bash
efibootmgr
text
BootCurrent: 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:

bash
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=1

GRUB 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.

/boot/grub/menu.lsttext
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 +1
  • default is the entry booted when nobody picks one, counted from 0. timeout is in seconds.
  • title starts 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.
  • kernel loads the kernel and passes it options; root= there is the Linux root filesystem. initrd loads the initial RAM disk.
  • chainloader +1 hands 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:

text
# grub
grub> root (hd0,0)
grub> setup (hd0)
grub> quit

GRUB 2#

GRUB 2 is more powerful, and its main file is generated rather than written by hand.

FileWhat 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/grubYour 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:

/etc/default/grubbash
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
KeyMeaning
GRUB_DEFAULTEntry to boot: a number (from 0), a title, or saved (remember the last choice, with GRUB_SAVEDEFAULT=true)
GRUB_TIMEOUTSeconds the menu waits. -1 waits forever.
GRUB_TIMEOUT_STYLEmenu, countdown or hidden
GRUB_CMDLINE_LINUXKernel options added to every entry, recovery included
GRUB_CMDLINE_LINUX_DEFAULTExtra options for the normal entries only (Debian, Ubuntu)
GRUB_DISABLE_OS_PROBERSet 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:

bash
# 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.cfg
text
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-6.1.0-28-amd64
Found initrd image: /boot/initrd.img-6.1.0-28-amd64
done

To 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).

RHEL and Fedora keep one small file per kernel in /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.

bash
# BIOS: install to the MBR of the disk (not a partition)
sudo grub-install /dev/sda
text
Installing for i386-pc platform.
Installation finished. No error reported.
bash
# 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=debian

On 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/sdb too, so the machine still boots when /dev/sda dies.
  • Removable / fallback path. UEFI firmware always tries \EFI\BOOT\BOOTX64.EFI when its boot entries are missing or broken. grub-install --removable puts 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 /boot location. grub-install --boot-directory=/mnt/usb/boot /dev/sdc writes 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, chroot into it and run grub-install and grub-mkconfig again.

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 thisGRUB 2GRUB Legacy
Edit the selected entryee
Open the GRUB command linecc
Append options to the kernel line(edit with e)a
Boot the edited entryCtrl+X or F10b
Throw away your editsEscEsc

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:

text
linux /vmlinuz-6.1.0-28-amd64 root=/dev/mapper/vg-root ro quiet systemd.unit=rescue.target

Useful 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:

text
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> boot

If 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:

text
grub rescue> set prefix=(hd0,gpt2)/grub
grub rescue> insmod normal
grub rescue> normal

Note

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, or grub.conf on 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/grub and /etc/grub.d/, then run grub-mkconfig -o /boot/grub/grub.cfg (update-grub on Debian, grub2-mkconfig -o /boot/grub2/grub.cfg on RHEL).
  • Never edit grub.cfg by hand: the next grub-mkconfig overwrites it.
  • grub-install /dev/sda writes the boot loader to the disk. UEFI uses --efi-directory=/boot/efi.
  • At the menu: e to edit, c for a command line, Ctrl+X or F10 to boot. Add systemd.unit=rescue.target, single or init=/bin/bash to the kernel line for repairs.
  • Back up the MBR with dd if=/dev/sda of=mbr.bak bs=512 count=1.

Byte check

0/3

Answer these 3 questions to commit this byte to your learning path.

  1. You changed GRUB_TIMEOUT in /etc/default/grub on a Debian machine. What’s the next step?
  2. In GRUB Legacy, what does (hd0,1) point to?
  3. Which file holds the GRUB Legacy configuration on a Red Hat system?
Search posts and bytes

Optional account

Keep your progress everywhere

Everything on AminBytes is free to read, with or without an account. Sign in only if you want your commits, streak and merged topics saved, on every device.

or

We keep only your name, avatar and which bytes you finished, and you can delete it all anytime. Privacy