Introduction to GRUB
GRUB (Grand Unified Bootloader) is the default boot manager for most Linux distributions. It loads your operating system kernel and initiates the startup process. When GRUB encounters issues, your system may become unbootable. This guide covers essential configuration, troubleshooting, and recovery techniques.
I. Basic GRUB Configuration
1. Key Configuration Files
- Primary Config:
/boot/grub/grub.cfg
(⚠️ Do NOT edit directly!) - Custom Settings:
/etc/default/grub
(controls timeout, default OS, etc.) - Script Directory:
/etc/grub.d/
(generatesgrub.cfg
)
2. Making Permanent Changes
- Edit preferences:
sudo nano /etc/default/grub
Example modifications:
GRUB_DEFAULT=0 # Boot first OS entry GRUB_TIMEOUT=5 # Show menu for 5 seconds GRUB_CMDLINE_LINUX="quiet splash"
- Update GRUB:
sudo update-grub
(Regenerates
grub.cfg
safely)
II. Essential Recovery Commands
Boot Failure? Access GRUB Rescue:
- Reboot → Hold
SHIFT
(or spamESC
) during startup - You’ll see:
grub> _
Critical Commands:
ls # List partitions (e.g., (hd0,msdos1), (hd0,msdos2))
ls (hd0,msdos1)/ # Explore files in partition 1
set root=(hd0,msdos2) # Set root partition
linux /boot/vmlinuz-xyz root=/dev/sda2 # Specify kernel
initrd /boot/initrd.img-xyz # Load initramfs
boot # Boot manually
III. Reinstalling GRUB
Scenario: Corrupted GRUB after Windows install or disk changes.
From Live USB (Chroot Method):
- Boot Linux Live USB → Open Terminal
- Identify partitions:
sudo fdisk -l
(Note Linux root
/dev/sda2
and boot/dev/sda1
) - Mount partitions:
sudo mount /dev/sda2 /mnt sudo mount /dev/sda1 /mnt/boot sudo mount --bind /dev /mnt/dev sudo mount --bind /proc /mnt/proc sudo mount --bind /sys /mnt/sys
- Chroot into system:
sudo chroot /mnt
- Reinstall GRUB:
grub-install /dev/sda # Install to MBR update-grub # Rebuild config exit sudo reboot
For UEFI Systems:
Replace grub-install
with:
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB
IV. Common Fixes & Tips
-
Boot Repair Tool:
Use Ubuntu’sboot-repair
:sudo add-apt-repository ppa:yannubuntu/boot-repair sudo apt update && sudo apt install boot-repair sudo boot-repair
(Automates GRUB recovery)
-
Password Protection:
Add to/etc/grub.d/00_header
:cat < 💡 **Need Help?** Share your error in forums with outputs from `lsblk` and `sudo fdisk -l` for faster assistance.