Technology Encyclopedia Home >How to repair GRUB in Linux?

How to repair GRUB in Linux?

To repair GRUB (GRand Unified Bootloader) in Linux, you typically need to follow these steps:

Step 1: Boot from a Live USB/DVD

  1. Create a Live USB/DVD: Use a tool like Rufus or Etcher to create a bootable USB drive with a Linux distribution (e.g., Ubuntu).
  2. Boot from Live Media: Insert the USB drive or DVD into your computer and restart it. Enter the BIOS settings to change the boot order so that it boots from the USB drive or DVD first.

Step 2: Identify Your Root Partition

  1. Open a Terminal: Once booted into the live environment, open a terminal.
  2. Identify the Root Partition: Use the lsblk or fdisk -l command to list all partitions and identify your Linux root partition (usually something like /dev/sda1).

Step 3: Mount the Root Partition

  1. Mount the Root Partition: Create a mount point and mount your root partition. For example:
    sudo mkdir /mnt/root
    sudo mount /dev/sda1 /mnt/root
    

Step 4: Bind Mount Necessary Filesystems

  1. Bind Mount Filesystems: Bind mount the necessary filesystems to ensure GRUB can access them:
    sudo mount --bind /dev /mnt/root/dev
    sudo mount --bind /proc /mnt/root/proc
    sudo mount --bind /sys /mnt/root/sys
    

Step 5: Chroot into Your Root Partition

  1. Chroot: Change the root directory to your mounted partition:
    sudo chroot /mnt/root
    

Step 6: Reinstall GRUB

  1. Reinstall GRUB: Install GRUB back onto your hard drive:
    grub-install /dev/sda
    
    Note: Replace /dev/sda with the appropriate device identifier for your hard drive.

Step 7: Update GRUB Configuration

  1. Update GRUB Configuration: Generate a new GRUB configuration file:
    update-grub
    

Step 8: Exit Chroot and Reboot

  1. Exit Chroot: Exit the chroot environment:
    exit
    
  2. Unmount Partitions: Unmount the partitions you mounted earlier:
    sudo umount /mnt/root/dev
    sudo umount /mnt/root/proc
    sudo umount /mnt/root/sys
    sudo umount /mnt/root
    
  3. Reboot: Reboot your computer:
    sudo reboot
    

Example

Suppose your root partition is /dev/sda1. The commands would look like this:

sudo mkdir /mnt/root
sudo mount /dev/sda1 /mnt/root
sudo mount --bind /dev /mnt/root/dev
sudo mount --bind /proc /mnt/root/proc
sudo mount --bind /sys /mnt/root/sys
sudo chroot /mnt/root
grub-install /dev/sda
update-grub
exit
sudo umount /mnt/root/dev
sudo umount /mnt/root/proc
sudo umount /mnt/root/sys
sudo umount /mnt/root
sudo reboot

Cloud-Related Recommendation

If you are looking for a reliable cloud service to host your Linux environment, consider using Tencent Cloud. Tencent Cloud offers a variety of services that can help you manage and maintain your Linux instances, including robust backup and recovery options, which can be useful in case you need to restore your system after a GRUB failure.