How to repair the GRUB2 boot loader on Linux

Jack Wallen shows you how easy it is to repair the GRUB2 boot loader in Linux.

Diverse IT colleagues setting server hardware

Image: iStockphoto/EvgeniyShkolenko

It’s probably going to be a very rare occasion when you need to repair or reinstall the GRUB2 bootloader on one of your data center Linux servers, but when that time comes, you will be glad for the knowledge. Because nothing can toast a sysadmin’s day like having to do a full OS reinstall and data restore on a mission-critical server.

I’m going to walk you through the process of doing just that–repairing and reinstalling GRUB2, with the help of a Ubuntu Live USB device.

SEE: Linux service control commands (TechRepublic Premium)

What you’ll need

Don’t wait until you have a failed instance of GRUB2 before you master this task. To that end, we’ll just use a working (test) instance of Ubuntu Server for this purpose. You’ll also want to have a bootable USB device, with an instance of Ubuntu Desktop ready to boot. For that, you can use a tool like Pop!_OS Popsicle or the always faithful Unetbootin. I’m going to highly recommend you use the desktop version of Ubuntu–there’s a very handy graphical tool that can fix the problem. Either way, make sure you’ve created your bootable USB device before you attempt this. 

Note: The tool we are going to use will work on any system that uses GRUB2. Although we’ll be using a Ubuntu Desktop live instance, the process will work, even if you’re repairing, say, Fedora or CentOS. As the boot loader is GRUB2, you should be good to go–GRUB2 is the default boot loader in most Linux distributions.

How to repair GRUB2

Boot the affected or test machine from the USB device and click Try Ubuntu. Once the desktop appears, make sure there’s a working network connection and open the terminal window. What we’re going to do now is install a tool called boot-repair. To do that, add the necessary repository with the command:

sudo apt-add-repository ppa:yannubuntu/boot-repair

Once the repository is added, update apt with the command:

sudo apt-get update


After the apt update completes, install boot-repair with the command:

sudo apt-get install boot-repair -y


Start the newly installed app with the command:

boot-repair


Click Recommended Repair (Figure A).

Figure A

bootrepaira.jpg

Running boot-repair via a Ubuntu live session.

The tool will run through the standard fixes for GRUB2. When the process completes, boot-repair will ask you if you want to upload the report to a pastebin (Figure B).

Figure B

bootrepairb.jpg

The boot-repair application has finished running it’s repairing process.

When the upload completes, boot-repair will present to you a URL that you can use to view the report (Figure C). If you want to see what boot-repair did to your system, copy and paste the URL into a browser and give the report a read.

Figure C

bootrepairc.jpg

Boot-repair has presented me with a URL so I can view the report.

Remember, that the Pastebin post will expire in a couple of days, so make sure you give it a read soon, otherwise you’ll miss out on seeing what the tool did to fix GRUB2. In my case, what boot-repair did was re-install the GRUB2 boot loader with the commands:

grub-install --efi-directory=/boot/efi --target=x86_64-efi --uefi-secure-boot
update-grub


If boot-repair fails to fix GRUB2, you might have to run through the same process, but click the Advanced Options button to access some of the extra features (Figure D). 

Note: You should not use the Advanced Options unless you know what you’re doing.

Figure D

bootrepaird.jpg

The boot-repair Advanced Options offer plenty of extra features.

The advanced features option gives you access to the likes of:

  • Backup and rename Windows EFI files

  • Repair file systems

  • Alternative location for GRUB2

  • Purge GRUB before reinstalling

  • Upgrade GRUB to the most recent version

Again, do not attempt to use the advanced features, unless you know exactly what you’re doing.

Once boot-repair has completed the process, reboot your machine, remove the USB drive, and cross your fingers that the boot loader has been successfully repaired. The good news is that boot-repair is really good at fixing the most common problems that cause GRUB2 systems to not boot. With this handy GUI tool, you won’t have to bother with the more complicated command line process of repairing your Linux boot loader.




Also see