Stage 3 · Build
Boot & Init Systems
GRUB Bootloader
grub.cfg, kernel command lines, rescue shells, and regenerating menus with grub-mkconfig.
GRUB Overview
GRUB (GRand Unified Bootloader) is the standard boot loader for Linux. It loads the kernel and initramfs, presents a boot menu, and passes kernel parameters. GRUB2 is the current version.
# GRUB configuration files
/boot/grub/grub.cfg # Main config (generated, do not edit)
/etc/default/grub # User-editable settings
/etc/grub.d/ # Scripts that generate grub.cfg
# GRUB modules
/boot/grub/x86_64-efi/ # UEFI modules
/boot/grub/i386-pc/ # BIOS modules
# GRUB environment
/boot/grub/grubenv # Environment block (saved entry, timeout)Never edit /boot/grub/grub.cfg directly. Edit /etc/default/grub and run grub-mkconfig to regenerate.
GRUB Configuration
# Key settings in /etc/default/grub
GRUB_DEFAULT=0 # Default menu entry (0=first)
GRUB_TIMEOUT=5 # Seconds to wait before booting
GRUB_TIMEOUT_STYLE=menu # menu, hidden, or countdown
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" # Kernel params for default
GRUB_CMDLINE_LINUX="" # Kernel params for all entries
GRUB_DISABLE_OS_PROBER=false # Enable OS probing (dual boot)
# Regenerate grub.cfg
sudo grub-mkconfig -o /boot/grub/grub.cfg
# For UEFI systems
sudo grub-mkconfig -o /boot/efi/EFI/ubuntu/grub.cfgAfter modifying /etc/default/grub, always regenerate grub.cfg with grub-mkconfig.
Kernel Command Line
# View current kernel command line
cat /proc/cmdline
# BOOT_IMAGE=/boot/vmlinuz-6.1.0-15-generic root=/dev/sda2 ro quiet splash
# Common kernel parameters
# root=/dev/sda2 — Root filesystem device
# ro — Mount root read-only initially
# quiet — Suppress boot messages
# splash — Show splash screen
# init=/bin/bash — Emergency shell (bypass init)
# single / 1 / systemd.unit=rescue.target — Boot to single-user
# nomodeset — Disable kernel mode setting (GPU issues)
# net.ifnames=0 — Legacy network interface names
# mitigations=off — Disable CPU vulnerability mitigations
# Edit GRUB at boot (press 'e' in GRUB menu)
# Add parameters to the linux line
# Press Ctrl+X to bootKernel parameters control boot behavior. Add them in GRUB_DEFAULT or edit at boot time by pressing 'e' in the GRUB menu.
GRUB Repair
# Boot from live USB, then:
# Mount root and boot partitions
sudo mount /dev/sda2 /mnt
sudo mount /dev/sda1 /mnt/boot/efi
# Chroot into the system
for i in /dev /proc /sys /run; do
sudo mount --bind $i /mnt$i
done
sudo chroot /mnt
# Reinstall GRUB
grub-install /dev/sda
update-grub
exit
# Or for UEFI
grub-install --target=x86_64-efi --efi-directory=/boot/efi
update-grubGRUB repair typically involves chrooting from live media and reinstalling. This fixes corrupted GRUB files and misconfigured boot entries.
GRUB Customization
# Add custom menu entries
cat > /etc/grub.d/40_custom << 'EOF'
#!/bin/sh
exec tail -n +3 $0
menuentry "Ubuntu Recovery" {
set root='(hd0,1)'
linux /boot/vmlinuz-$(uname -r) root=/dev/sda2 ro single
initrd /boot/initrd.img-$(uname -r)
}
menuentry "Windows" {
set root='(hd0,2)'
chainloader +1
}
EOF
sudo chmod +x /etc/grub.d/40_custom
sudo grub-mkconfig -o /boot/grub/grub.cfg
# Set default entry
GRUB_DEFAULT="Ubuntu Recovery"
# or use index
GRUB_DEFAULT=2
# Set timeout
GRUB_TIMEOUT=10
# Hide GRUB menu (show on Shift key)
GRUB_TIMEOUT_STYLE=hidden40_custom is the standard place for custom menu entries. It is preserved across grub-mkconfig runs.
Rescue Mode
# GRUB rescue shell (when GRUB cannot find config)
grub> set root=(hd0,1)
grub> set prefix=(hd0,1)/boot/grub
grub> insmod normal
grub> normal
# Emergency boot from GRUB menu
# Press 'e' to edit, add to kernel line:
# init=/bin/bash — Bypasses init, drops to root shell
# After emergency boot, fix filesystem
fsck -y /dev/sda2
mount -o remount,rw /dev/sda2
# Rebuild initramfs
update-initramfs -u
# Reinstall GRUB
grub-install /dev/sda
update-grubGRUB rescue mode is triggered when GRUB cannot find its configuration. The minimal shell lets you manually specify boot parameters.
Always keep a Linux live USB available for emergency repairs. It can chroot into your system and fix GRUB, filesystem, and boot issues.
grub.cfg is regenerated by grub-mkconfig. Manual edits will be lost. Always edit /etc/default/grub and /etc/grub.d/ scripts.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.