Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

In this article, let’s talk about GRUB 2 and how we can reinstall and fix it. GNU GRUB is a multiboot bootloader, capable of loading several operating systems. GRUB 2 is the current released version and replaced the legacy GRUB, from version 0.9x. It can be installed or run from any device, including a hard drive, CD-ROM, or USB drive.

2. Computer Startup Process

When we press the power button of the system, there are many background processes that run as the computer starts up. The system passes through four distinct stages to completely turn on and boot.

In the first stage, it goes through BIOS where it performs an integrity check, called POST. During this diagnostic check, POST ensures that all the hardware components are functioning. In case a single one fails, it throws an error message.

After POST, the BIOS loads and executes the MBR (Master Boot Record) containing the GRUB 2 bootloader, which eventually brings up the kernel. The kernel initializes the devices and their drivers and mounts the primary filesystem. It then starts the init process, which sets the remainder of the system startup process in motion.

3. Grand Unified Bootloader Version 2

Linux normal installation automatically integrates GRUB 2 installation within it. When we’re not dual booting, the installation uses the entire disk. Otherwise, we must select the partition to which the Linux installation process embeds the bootloader information.

After completing the installation, it becomes the default bootloader for the system. If the system has multiple operating systems, GRUB 2 takes over the boot process and its menu will contain the other operating systems detected.

Under some circumstances, we may want to reinstall GRUB 2. This might be due to any of the following reasons:

  • First, when we want to repair a broken system where a user gets a screen with no prompt or cannot boot
  • Second, when we want to return control to Linux bootloader after adding a second operating system, which added its own
  • Third, when we want to create a backup by adding boot information to an additional drive
  • Fourth, when we want to restore GRUB 2’s missing files, which are either corrupted or deleted intentionally
  • Finally, when we want to upgrade the Linux bootloader to the most recent

4. Reinstalling GRUB 2 on a Live System

When we use the grub-install command, it does not generate a new GRUB 2 menu config. Instead, it updates and writes to the designated drive. If files are missing or intentionally deleted, it will restore them. The files restored and updated are core.img, grubenv, and device map. To reinstall GRUB 2 in a working system, we run the command:

$ sudo grub-install /dev/sdX

This writes the MBR information on the current drive (X). If we specify a partition instead of the drive, it will throw an error. After running grub-install, we update the GRUB 2 menu by running:

$ sudo update-grub

5. Fixing a Corrupted or Broken System

A corrupted boot sector or a failed GRUB 2 can cause a system to break. We can repair this using the boot-repair graphical tool or a live CD/USB.

5.1. Using boot-repair

boot-repair is a graphical tool that fixes several GRUB 2 issues. We can use it from a live CD/USB or a running system. To start reinstalling GRUB 2 with boot-repair, we first create a live USB/CD and boot from it. During this process, we should ensure that our system has internet access. Next, we open the terminal and execute the following commands sequentially:

$ sudo add-apt-repository ppa:yannubuntu/boot-repair && sudo apt update
$ sudo apt install -y boot-repair && boot-repair
$ sudo reboot

5.2. Using a Live CD/USB

When we’re using a live CD/USB to boot, we need to be aware of the disk or partition where we have installed the system. In case we’re not sure, we can run a couple of commands to find out. To identify the Linux partition, we look for the one that has ext4 format:

$ sudo fdisk -l
$ sudo blkid

After this, we run grub-install. This restores the missing files. Often, this does not restore all the files, so we need to expunge GRUB 2 and reinstall it. This will restore all the files:

$ sudo mount /dev/sdXY /mnt
$ sudo grub-install --boot-directory=/mnt/boot /dev/sdX
$ sudo update-grub
$ sudo reboot

In these examples, X is the drive letter while Y is the partition number. For older versions with BIOS, we run:

$ sudo grub-install --root-directory=/mnt /dev/sdX

5.3. Reinstall GRUB 2 for Kali Linux

To reinstall GRUB 2 for kali, we need to open a terminal and run a series of commands after booting from either a live USB or CD:

$ mount /dev/sdaY /mnt
$ mount --bind /dev /mnt/dev
$ mount --bind /dev/pts /mnt/dev/pts
$ mount --bind /proc /mnt/proc
$ mount --bind /sys /mnt/sys
$ chroot /mnt
$ grub-install /dev/sdX
$ update-grub
$ exit
$ umount /mnt/dev/pts
$ umount /mnt/dev
$ umount /mnt/proc
$ umount /mnt/sys
$ umount /mnt

5.4. Using chroot

We can also reinstall GRUB 2 using the chroot command. Once we have live-booted from a CD/USB and issued the chroot command, the live CD/USB treats the broken system as its own. After opening the terminal, we run:

$ sudo mount /dev/sdXY /mnt
$ sudo mount /dev/sda1/mnt/boot/efi          #use only if /boot is on a different partition
$ for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done
$ sudo chroot /mnt
$ grub-install /dev/sda
$ update-grub

Be sure to exit chroot with Ctrl+d after the update is complete. Then, reboot the system:

$ sudo reboot

6. Conclusion

This article looks at the various ways we can reinstall and repair our GRUB 2. Up until recently, most Linux distros that use GRUB 2 still referred to it as GRUB. When creating a bootable CD/USB it is recommended that we use the same version of the system we’re running. Finally, we should know where the system or /boot partition is.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!