Reinstall Grub

Table of Contents

  1. Intro
  2. Debian and Ubuntu
  3. Arch
  4. Fedora

We’ve all been there. We’re dual booting and Windows does a major upgrade. Suddenly, without warning, in the quiet of night, we can no longer access our Linux partition.

Or, maybe you’re not on Windows. Maybe you’re using Arch (btw…). You upgrade your OS after a week and, boom, SHTF. No more GRUB.

Perhaps you’ve done what I did. You don’t have Windows on your computer. But you decided to use it for something and a VM isn’t cutting it. So, you install Windows on an ENTIRELY DIFFERENT HARD DRIVE and, still, despite your precautions, GRUB is gone -_-;

What do you do? Do you really reinstall Linux, again, and lose your precious data? You made a ton of commits last night, but never pushed your changes! :face_with_symbols_over_mouth::face_with_symbols_over_mouth::face_with_symbols_over_mouth:

Never fear! Trusty Ubuntu is here to save the day! (you can use whatever live environment you want, I am using Ubuntu for ease of use and familiarity)

If you use UEFI, do not use the first set of commands, scroll down please.

Debian and Ubuntu family

The live USB instructions are applicable to all of the distributions below.

Create a live USB with Etcher, Rufus, dd, or whatever your poison.

https://www.ubuntu.com/download/desktop

After that, you’ll want to select “Try Ubuntu without installing” to load the live environment.

Next, fire up the terminal.

Find your drive with linux on it. lsblk -f works, as does gparted or parted -l

Mount the root partition. For me, it was /dev/sda2

sudo mount /dev/sda2 /mnt

Next, you need to mount components of the file system to /mnt:

sudo mount --bind /dev /mnt/dev 
sudo mount --bind /dev/pts /mnt/dev/pts 
sudo mount --bind /proc /mnt/proc 
sudo mount --bind /sys /mnt/sys

If you receive errors on the above, you may need to mount another partition.

Next, chroot into /mnt:

sudo chroot /mnt

Run the following commands:

grub-install /dev/sda
grub-install --recheck /dev/sda
update-grub

Exit chroot and unmount everything.

exit && umount /mnt

Reboot!

sudo shutdown -r now

For UEFI

UEFI is quite a bit different (but much easier).

My EFI partition was on /dev/sda1 and my root partition was on /dev/sda2

sudo mount /dev/sda2 /mnt
sudo mount /dev/sda1 /mnt/boot/efi
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  

The foreach loop above is basically saying "Hey, if you find /dev, /dev/pts, /proc, /sys, and /run, go ahead and mount them (as the superuser) to /mnt/pathYouFound.

The rest of the process is the same as the MBR process.

Thank you @TheCakeIsNaOH for this write up!

For Arch

The Arch live ISO does not have a GUI so you do not have to find a terminal. It also logs in as root, so no sudo is needed for commands.

To find your disk you can use parted -l or lsblk -f as above, or you can use fdisk -l

Mount your disk with mount /dev/sdxX /mnt, replacing the xX with your disk number. Additionally mount the EFI partition with mount /dev/sdxX /mnt/efi/ if you boot with UEFI. The EFI partition may need to be mounted to a different location such as /mnt/boot/efi depending on your setup.

Then you can arch-chroot /mnt. Arch-chroot is a script that mounts /dev, /proc, and /sys so you do not have to manually mount them.

For BIos

When you are in the chroot, then run grub-install /dev/sdx, do not specify a number ie sda1, instead just put sda. It will install for i386-pc even though Arch is 64 bit only.

For UEFI

When you are in the chroot, then run grub-install --target=x86_64-efi --efi-directory=/efi --bootloader-id=GRUB. Adjust efi-directory if needed.

Back to guide for both

Run grub-mkconfig -o /boot/grub/grub.cfg
If you are chrooting into a system that has a Debian based distro installed, then you could alternatively run update-grub.

The rest of the guide is the same as for Debian.
exit && umount /mnt
shutdown -r now

Fedora

@SgtAwesomesauce Bah, I knew that! Thanks for the reminder! :grin:

For Fedora, the process is a little different, and a bit more streamlined.

mkdir /mnt/sda

Mount the partition that has grub:

sudo mount /dev/sda1 /mnt/sda

Install GRUB:

grub2-install --boot-directory=/mnt/sda/boot/ /dev/sda

Save the config:
grub2-mkconfig -o /boot/grub2/grub.cfg

Reboot and you should be in business.

I hope this helps. I know this question gets asked sometimes, especially those that like to dual boot. The answer is sometimes painful to find, but hopefully this is immortalized in the Level1 community and expounded upon.

10 Likes

I don’t use GRUB2, I use systemd-boot. So the steps are totally different :stuck_out_tongue:

Windows is a joke when it comes to this. It didn’t just delete my EFI partition where my /boot lived. It deleted its own EFI partition, and then write itself over the Linux EFI partition on A DIFFERENT DISK. FML.

Not only I had to reinstall systemd-boot (which is the easy part) I had to reinstall WIndows’s EFI. Had to boot into their recovery console and do arcane cmd magic to create that.

/rant

1 Like

Great wiki/how to, I actually had to do this last night and could have used this guide!

1 Like

For Arch

The Arch live ISO does not have a GUI so you do not have to find a terminal. It also logs in as root, so no sudo is needed for commands.

To find your disk you can use parted -l or lsblk -f as above, or you can use fdisk -l

Mount your disk with mount /dev/sdxX /mnt, replacing the xX with your disk number. Additionally mount the EFI partition with mount /dev/sdxX /mnt/efi/ if you boot with UEFI. The EFI partition may need to be mounted to a different location such as /mnt/boot/efi depending on your setup.

Then you can arch-chroot /mnt. Arch-chroot is a script that mounts /dev, /proc, and /sys so you do not have to manually mount them.

For BIos
When you are in the chroot, then run grub-install /dev/sdx, do not specify a number ie sda1, instead just put sda. It will install for i386-pc even though Arch is 64 bit only.

For UEFI
When you are in the chroot, then run grub-install --target=x86_64-efi --efi-directory=/efi --bootloader-id=GRUB. Adjust efi-directory if needed.

Back to guide for both

Run grub-mkconfig -o /boot/grub/grub.cfg
If you are chrooting into a system that has a Debian based distro installed, then you could alternatively run update-grub.

The rest of the guide is the same as for Debian.
exit && umount /mnt
shutdown -r now

2 Likes

Fedora doesn’t have update-grub. You’ll need to run the mkconfig command instead.

Aside from that, it should work.

3 Likes

Problems like that is why I started installing grub on more than one HDD. When I started doing that I had a half dead motherboard that would randomly have SATA working or not but usually IDE would work and the usb 1.1 always worked but not always be visible to the OS. On the bright side the floppy drive always worked even when the IDE HDD wasn’t being detected.

2 Likes

@SgtAwesomesauce Thanks :grin: I freaking knew that (brb I update CentOS and Amazon Linux every day :expressionless: ).

@TheCakeIsNaOH thanks for your contribution, I added it to the wiki.

I also created a table of contents so people can jump down to their distro of choice.

2 Likes

On BIOS systems this is correct, but may also be /boot/grub2/grub.cfg

On UEFI systems this will likely be /boot/efi/EFI/fedora/grub.cfg (on Fedora for example).

It might be worth adding that you can more easily tell where your grub config needs to go by looking for it.

Easily get the grub.cfg path with the following

find /boot/ -name grub.cfg

You could also one line it

grub-mkconfig -o `find /boot/ -name grub.cfg`

Also keep in mind. On Fedora at least (and other systems) grub 2 commands are prefixed grub2- not grub- so it would be grub2-mkconfig (noticed you already mentioned this, I missed it somehow :smiley: )

2 Likes