grub command line boot repair

GRUB Command Line Boot Repair Guide

When your Linux system fails to boot, GRUB errors are often the culprit. The GRUB bootloader manages system startup, and corruption or misconfiguration can leave you stuck at a black screen. Fortunately, you can fix most boot repair issues directly from the command line without reinstalling your entire system. This guide walks you through the essential commands and troubleshooting steps.

GRUB Command Line Boot Repair: Fix Boot Issues

Understanding GRUB and Boot Failures

GRUB (GRand Unified Bootloader) is the first program that runs when you power on a Linux machine. It loads the kernel and hands control to your operating system. When GRUB fails, you typically see error messages like "error: no such partition" or "GRUB rescue>" prompt. Common causes include accidental partition deletion, corrupted boot sector, misconfigured boot files, or hardware changes. Understanding what went wrong helps you apply the right fix. Boot repair from the command line requires access to a live Linux environment, which you can create using a USB drive or DVD with a Linux distribution.

Accessing the GRUB Rescue Environment

If your system won't boot normally, you'll likely see the GRUB rescue prompt. This minimal environment lets you run basic commands to diagnose and fix problems. From the rescue prompt, you can list partitions using the ls command, which shows available disks and their contents. Type 'ls' to see all partitions, then 'ls (hd0,gpt1)/' to examine a specific partition's contents. Look for the /boot directory to confirm you've found your Linux installation. If you see familiar directories like bin, lib, or etc, you're on the right partition. This reconnaissance step is crucial before attempting any boot repair work.

Reinstalling GRUB from Command Line

Once you've identified your Linux partition, you can reinstall GRUB to fix boot issues. Boot from a live USB or DVD, then open a terminal. Use fdisk or lsblk to identify your disk (usually /dev/sda or /dev/nvme0n1). Mount your root partition to a temporary directory: mount /dev/sdaX /mnt (replace X with your partition number). Mount the boot partition if it's separate: mount /dev/sdaY /mnt/boot. Then reinstall GRUB with: grub-install --root-directory=/mnt /dev/sda. This command writes GRUB files to your disk's boot sector. Update the GRUB configuration afterward using grub-mkconfig to ensure all boot entries are recognized properly.

Updating GRUB Configuration Files

After reinstalling GRUB, you need to regenerate its configuration file to recognize your installed kernels and operating systems. While still in the live environment with your partitions mounted, run: grub-mkconfig -o /mnt/boot/grub/grub.cfg. This command scans your system for installed kernels and creates a new configuration file. If you see output listing detected Linux installations, the process worked correctly. The configuration file tells GRUB which kernels to load and in what order. Without updating it, GRUB won't know how to boot your system even though the bootloader itself is installed. This step is essential for completing any boot repair process.

Troubleshooting Common GRUB Errors

Different error messages point to different problems. "Error: no such partition" means GRUB can't find the partition it's configured to use, often from disk changes or partition table corruption. "Error: unknown filesystem" indicates GRUB doesn't recognize the filesystem type, sometimes fixable by reinstalling GRUB. "GRUB rescue>" prompt means GRUB couldn't load its normal environment, requiring the reinstallation steps above. If you see "error: file not found" when booting, your kernel or initramfs might be corrupted or missing. In this case, boot from live media, mount your partitions, and check if /boot/vmlinuz and /boot/initrd files exist. If missing, you may need to reinstall your kernel package using your distribution's package manager.

Verifying Boot Repair Success

After completing boot repair steps, reboot your system without the live media to test if GRUB now works. Remove the USB drive or DVD and restart. If GRUB menu appears with your operating system listed, the repair was successful. Select your Linux entry and verify the system boots normally. If you still see errors, you may need to repeat the process or investigate further. Check that you mounted the correct partitions and used the right disk identifier. Some systems have multiple disks, so confirming /dev/sda versus /dev/sdb is critical. If problems persist, consider checking your BIOS settings to ensure the correct disk is set as the boot device.

Preventing Future GRUB Boot Issues

Once your system boots reliably, take steps to prevent future problems. Keep your system updated, as kernel and bootloader updates are usually tested thoroughly. Avoid manual changes to partition tables unless absolutely necessary. If you must modify partitions, back up your boot sector first using: dd if=/dev/sda of=boot_backup.img bs=512 count=1. Document your partition layout and boot configuration for reference. Consider maintaining a bootable live USB with your preferred Linux distribution for emergency access. Regular backups of your /boot directory ensure you can restore kernel files if corruption occurs. These preventive measures significantly reduce the likelihood of encountering boot problems.

Frequently asked questions

What is GRUB and why does it fail?

GRUB is the bootloader that starts your Linux system. It fails due to partition corruption, misconfiguration, accidental deletion, or hardware changes. When GRUB fails, your system won't boot past the GRUB menu or shows error messages. Most failures are fixable through command-line repair without reinstalling your entire system.

How do I access GRUB rescue mode?

GRUB rescue mode appears automatically when GRUB can't load its normal environment. You'll see a 'GRUB rescue>' prompt. If you don't see it, you can boot from a live USB or DVD and access GRUB from there. From rescue mode, you can run basic commands like ls to diagnose boot problems.

Do I need a live USB to repair GRUB?

For most repairs, yes. A live USB gives you access to full Linux tools and lets you mount partitions safely. However, if you can access the GRUB rescue prompt, you can perform some diagnostics there. For complete boot repair with grub-install and grub-mkconfig, a live environment is essential.

Will GRUB repair delete my files?

No. GRUB repair only modifies the bootloader and its configuration files. Your personal files, documents, and system data remain untouched. The repair process reinstalls GRUB to the boot sector and updates configuration, leaving your actual data partition intact.

What if GRUB repair doesn't work?

If problems persist, verify you mounted the correct partitions and identified the right disk. Check BIOS settings to confirm boot device priority. If your kernel files are corrupted, you may need to reinstall them. Consider consulting your distribution's documentation or recovery tools if standard repair steps fail.