What GRUB Is and How It Works
This free resource is an educational guide. It explains how the GRUB bootloader works and how to install it from a command line. It does not provide software, services, or official government materials, and it is not affiliated with any government agency.
Free Guide to Classic Car Pricing and Values →
GRUB stands for GRand Unified Bootloader. A bootloader is a small program that starts when a computer turns on. Its job is to find an operating system on the disk and load it into memory. Most Linux systems use GRUB 2, which is the current version of the project. The older GRUB Legacy version (0.97) is no longer used by most modern distributions.
GRUB works in stages. The computer's firmware, either BIOS or UEFI, first runs a small piece of code stored on the disk. That code loads the main GRUB files, which are usually kept in a folder such as /boot/grub on Linux. GRUB then reads its configuration file, shows a menu, and loads the chosen kernel.
GRUB uses its own naming system for disks and partitions. For example, (hd0,msdos1) refers to the first partition on the first disk, which is often /dev/sda1 in Linux. The name (hd0,gpt2) refers to the second partition on a disk that uses a GPT partition table. Understanding this naming is the first step in working with the GRUB command line.
- BIOS systems usually store GRUB's boot code in the master boot record (MBR) or a small BIOS boot partition on GPT disks.
- UEFI systems store GRUB as a file inside an EFI System Partition (ESP), which is a FAT-formatted partition.
- The GRUB configuration file is generated automatically in most distributions, so users rarely edit it by hand.
Practical takeaway: Before installing GRUB, find out whether your computer uses BIOS or UEFI, and whether the disk uses MBR or GPT. These two details decide which installation commands are correct.
Preparing Before You Start
Installing or repairing a bootloader changes the part of the disk the computer reads first. A mistake can leave a system unable to start. Preparation lowers the risk of problems and makes recovery easier if something goes wrong. Most GRUB repairs are done from a live USB drive. A live Linux image can start the computer without touching the installed system, and it includes the tools needed to mount partitions and run commands inside them. Image files for many Linux distributions are published by their own project websites, and users should only use images from official project pages.
Get Your Free Global Blue Tax Refund Guide →
Several Linux commands show how the disk is laid out. These commands only read information and do not change the disk:
- lsblk lists disks and partitions in a tree view with sizes and mount points.
- sudo fdisk -l /dev/sda shows the partition table type, such as Disklabel type: gpt or dos.
- sudo blkid shows the file system type and UUID of each partition.
- sudo parted /dev/sda print shows partition numbers, file systems, and flags.
Some useful facts about partition tables help with planning. MBR partition tables support up to four primary partitions and disks up to 2 TiB in size. GPT partition tables support disks larger than 2 TiB, and Linux tools usually create 128 partition slots by default. Many UEFI systems use an EFI System Partition of about 512 MiB, which is a common size recommended by many Linux installers.
A backup of important files is a good step before any boot repair. Copying personal files to an external drive takes time, but it protects against data loss if a partition is changed by mistake.
Practical takeaway: Write down the disk name, partition table type, and firmware mode before typing any install command. Keep this record nearby while you work.
Using the GRUB Command Line Prompt
The GRUB command line is a text prompt that lets users type commands before the operating system starts. It is useful for finding partitions, loading kernels by hand, and starting a system that the normal menu cannot boot. There are two common ways to reach the prompt. On many systems, pressing c at the GRUB menu opens the command line. On some systems, holding Shift during startup (BIOS) or pressing Esc (UEFI) opens the menu first. When GRUB cannot find its own files, it may show a grub rescue> prompt instead. This is a limited version of the command line that can still load core modules.
Learn About Android Widgets and How They Function →
The following commands are common in GRUB:
- ls lists the disks and partitions that GRUB can see.
- ls (hd0,gpt2)/ shows the contents of a partition to confirm it holds the right files.
- set root=(hd0,gpt2) chooses which partition GRUB uses for files and kernels.
- linux /vmlinuz root=/dev/sda2 loads the Linux kernel and tells it where the root file system is.
- initrd /initrd.img loads the initial RAM disk that the kernel needs during startup.
- boot starts the system with the loaded kernel.
A common rescue example looks like this. A user finds that ls (hd0,gpt2)/boot/grub shows a grub.cfg file. The user then sets the root and prefix, loads the normal module, and starts the menu:
- set root=(hd0,gpt2)
- set prefix=(hd0,gpt2)/boot/grub
- insmod normal
- normal
File names and partition numbers vary by system, so these examples are only patterns. Kernel names such as vmlinuz-6.x are common, but the exact version will differ.
Practical takeaway: Use ls before any set command. Confirming what is on each partition helps prevent loading the wrong system.
Installing GRUB from a Live System
Installing GRUB from the command line usually happens inside a chroot environment. Chroot makes a mounted system act as the main root directory. This lets the installer tools see the installed system's files, kernels, and settings while running from a live USB drive. The general process has the same steps on most Linux distributions. Commands for mounting and chroot are the same, but package and path names may vary.
Learn Where to Find Storage Unit Auctions Near You →
- Boot the live USB drive and open a terminal.
- Find the root partition with lsblk and mount it, for example: sudo mount /dev/sda2 /mnt.
- For UEFI systems, mount the EFI System Partition inside the root folder: sudo mount /dev/sda1 /mnt/boot/efi.
- Bind the system folders so tools work correctly: sudo mount --bind /dev /mnt/dev, /proc, and /sys in the same way.
- Enter the installed system with sudo chroot /mnt.
Once inside the chroot, the installation commands depend on firmware type. For a BIOS system, the command is usually grub-install /dev/sda, where the disk name points to the whole disk and not a partition. For a UEFI system, a common command is:
- grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB
After installation, the configuration file must be rebuilt. On Debian and Ubuntu systems, update-grub does this. On Fedora and some other distributions, grub2-mkconfig -o /boot/grub2/grub.cfg is used. Leave the chroot with exit, unmount the folders, and reboot.
Practical takeaway: Match the install command to the firmware type shown earlier. A UEFI command used on a BIOS system, or the reverse, will not produce a working boot setup.
Updating and Customizing the GRUB Configuration
GRUB builds its menu from a set of scripts rather than from one hand-written file. On Debian-based systems, the main settings live in /etc/default/grub. Scripts in /etc/grub.d/ add menu entries for kernels and other operating systems. The final file, grub.cfg, is generated from these sources.
Your Free Guide to DPS License Appointment Scheduling →
Common settings in /etc/default/grub include:
- GRUB_TIMEOUT=5 sets how many seconds the menu waits before booting the default entry.
- GRUB_DEFAULT=0 chooses which menu entry starts automatically.
- GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" sets kernel options used for normal boots.
- GRUB_TIMEOUT_STYLE=menu keeps the menu visible during the wait.
After editing this file, rebuild the menu. Running sudo update-grub on Debian or Ubuntu, or the matching grub2-mkconfig command on other systems, writes the changes into grub.cfg. Skipping this step is a common reason that edits seem to have no effect.
Custom menu entries can be added to /etc/grub.d/40_custom. Each entry uses a menuentry block with the same commands used at the GRUB prompt. Before rebooting, users can open the generated file and read the output to confirm the entries look correct.
Practical takeaway: Make one change at a time, rebuild the configuration, and test the result. A single typing error in a custom entry can cause that entry to fail, so small steps make errors easier to find.
Troubleshooting Common GRUB Problems
Most GRUB problems come from a small number of causes. Knowing these patterns helps users read error messages and choose the right next step. Understanding how each problem happens is often more useful than memorizing commands.
Free Guide to Classic Car Pricing and Values →
The most common issues include:
- Wrong target disk: Installing GRUB to a disk that does not contain the system leaves the computer starting the wrong drive. Check the disk name with lsblk before running grub-install.
- Missing EFI partition: UEFI installs fail or produce unbootable systems if the ESP is not mounted at the expected path during install.
- Outdated configuration: If a new kernel is installed but the menu is not rebuilt, the new kernel will not appear in the boot list.
- Firmware settings: Some computers boot from a different entry than expected. Checking the firmware boot order and the signed-boot setting can explain missing entries.
- Mismatched firmware mode: A system installed in UEFI mode may fail to boot if the firmware is switched to legacy BIOS mode, and the reverse is also true.
When an install command shows an error, reading the final lines of output is often the fastest path to an answer. Messages such as no suitable mode found or cannot find EFI directory point to specific problems with mounts or firmware mode. Searching the official GRUB manual or the documentation for the distribution can also explain these messages.
Keeping notes about each step is another practical habit. A short log that lists the disk names, partition numbers, and commands run makes it easier to undo a change or ask a question on a project support forum.
Practical takeaway: Read error messages closely, confirm the disk and firmware mode, and keep a written record of each change. These steps make GRUB problems easier to understand and easier to fix.