How to install or reinstall Grub

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
pbear
Level 16
Level 16
Posts: 6569
Joined: Wed Jun 21, 2017 12:25 pm
Location: San Francisco

How to install or reinstall Grub

Post by pbear »

Most of this information is readily available somewhere, but I'm not aware of anywhere with all of it in one place. Which method to use depends on the situation. If able to boot into the system, the first and simplest method usually will be sufficient. If unable, e.g., because a Windows update has overwritten the boot loader, boot a live session and try the second method. Usually this works. If not, escalate to chroot. If all else fails, try purge-and-reinstall. Notice I don't mention the Boot Repair app. Have seen too many cases where it made the situation worse, but you have that option.

References: grub-install man page; GNU Grub Manual; Ubuntu Help; Arch Wiki.

Simple method. Run sudo grub-install /dev/sdx, where x = device to which installing the boot loader. Works for both BIOS and UEFI. For example, returns control of Grub to the primary system after installing a second as a test box (by default, the last system installed controls Grub). As mentioned, for this to work, you have to be booted into the system you want to control Grub.

Live session method. Works with both BIOS and UEFI, but the commands are a bit different. First you mount the system partition to /mnt. If there is a boot partition, this must be mounted also; insert a new second step: sudo mount sdxn /mnt/boot, where sdxn is the boot partition's ID. Then, in UEFI, you mount the EFI partition. The command to install Grub is very simple because it invokes a script which does most of the work behind the scenes. Notice the target for grub always is the device, for illustration sda. This is true in UEFI as well as BIOS. See GNU Grub Manual.

For BIOS, assuming the system/root partition is sda1. Modify as appropriate, of course, if the system partition is sda5, sdb1, etc.

Code: Select all

sudo mount /dev/sda1 /mnt 
sudo grub-install /dev/sda --boot-directory=/mnt/boot
For UEFI, assuming the system partition is sda2 and the EFI partition is sda1:

Code: Select all

apt install grub-efi-amd64-signed shim-signed
sudo mount /dev/sda2 /mnt
sudo mount /dev/sda1 /mnt/boot/efi 
sudo grub-install /dev/sda --boot-directory=/mnt/boot --uefi-secure-boot
Notes: For reasons unknown, grub-install only installs the secure boot loaders if the live session is connected to the internet. If secure boot is disabled, will get a squawk to that effect, but the command will complete without issue. If you want to install Grub without secure boot, change the first line to apt install grub-efi-amd64 (no -signed or shim-signed) and omit --uefi-secure-boot from the last line; internet access not needed, but doesn't hurt.

chroot. Mainly useful if for some reason you need to run update-grub, as well as install/reinstall Grub. For that matter, if all you need to do is update Grub from the live session, you do that the same way; just leave out the grub-install line. As with the regular live session method, if you have a boot partition, you need to insert a second step to mount it.

For BIOS, still assuming the system partition is sda1:

Code: Select all

sudo mount /dev/sda1 /mnt
for i in /dev /dev/pts /proc /sys; do sudo mount -B $i /mnt$i; done
sudo chroot /mnt
grub-install /dev/sda
update-grub
exit
sudo umount -R /mnt
FYI, the second line is a compact way of doing bind mounts (sudo mount --bind /dev /mnt/dev, etc.), so those directories are available in the chroot environment. Per Arch Wiki, adding -R (--recursive) to the unmount command also releases the bind mounts.

For UEFI, assuming the system partition is sda2 and the EFI partition is sda1:

Code: Select all

sudo mount /dev/sda2 /mnt
sudo mount /dev/sda1 /mnt/boot/efi
for i in /dev /dev/pts /proc /sys; do sudo mount -B $i /mnt$i; done
sudo cp /etc/resolv.conf /mnt/etc
modprobe efivars
sudo chroot /mnt
apt install grub-efi-amd64-signed shim-signed 
grub-install /dev/sda --uefi-secure-boot
update-grub
exit
sudo umount /mnt/boot/efi 
sudo umount -R /mnt
As mentioned above, grub-install needs access to the internet to install the secure boot loader. That's what the fourth and fifth lines do. Be sure to set up a connection in the live session before running the commands. As with the regular method, you'll get a squawk if secure boot isn't enabled. And you can install without secure boot; modify the apt install and grub-install lines as described above.

Purge-and-Reinstall. It's rare to need to escalate this far. The few cases I've noticed entailed someone or something having bollixed the Grub scripts in /etc/grub.d (yes, I'm looking at you, Grub Customizer). If still able to boot, the commands are simple:

For BIOS: Run apt purge grub-common; will be prompted to confirm removal of boot loader. Then run apt install grub-pc os-prober. Select destination for boot loader when prompted (tap space key to select). Unless you have a very good reason, install to the device, not the system partition.

os-prober is removed by purge but only a recommended package on reinstall, so specifying additionally. Same with shim-signed for UEFI.

For UEFI: Similar, but the packages to install are different. Run apt purge grub-common, then apt install grub-efi-amd64-signed os-prober shim-signed. Select destination for boot loader if prompted (probably won't be).

Purge-and-Reinstall in chroot. This is for the scenario where can't boot. Remember, if you have a boot partition, insert a second step to mount it.

For BIOS, assuming the system partition is sda1:

Code: Select all

sudo mount /dev/sda1 /mnt
for i in /dev /dev/pts /proc /sys; do sudo mount -B $i /mnt$i; done
sudo cp /etc/resolv.conf /mnt/etc
sudo chroot /mnt
apt purge grub-common
apt install grub-pc os-prober
exit
sudo umount -R /mnt
For UEFI, assuming the system partition is sda2 and the EFI partition is sda1. Again, you need to set up an internet connection.

Code: Select all

sudo mount /dev/sda2 /mnt
sudo mount /dev/sda1 /mnt/boot/efi
for i in /dev /dev/pts /proc /sys; do sudo mount -B $i /mnt$i; done
sudo cp /etc/resolv.conf /mnt/etc
modprobe efivars
sudo chroot /mnt
apt purge grub-common
apt install grub-efi-amd64-signed os-prober shim-signed
exit
sudo umount /mnt/boot/efi 
sudo umount -R /mnt
Frankly, if you've gotten this far and still no joy, probably no alternative except reinstall of the whole system.
Last edited by pbear on Mon Oct 05, 2020 11:57 pm, edited 1 time in total.
User avatar
AndyMH
Level 21
Level 21
Posts: 13579
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: How to install or reinstall Grub

Post by AndyMH »

I can never remember how, so I've bookmarked this :)
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
okalexm
Level 2
Level 2
Posts: 59
Joined: Wed Feb 05, 2020 4:46 pm
Location: CA

Re: How to install or reinstall Grub

Post by okalexm »

Fabulous consolidation of information.
Thank you very much!
Only after spending many hours in postings and websites touching on these issues will one appreciate just how great this summary is!
Bookmarked for sure.
If I may, you might want to review this periodically and add a line indicating it still seems correct with current Mint versions and UEFI "bioses."
Thanks!
Jan 2023:
LM21.0 on: MSI X470 G.P.C,Ryzen 5700X,16GB, MSI RX6700XT, 970 Evo+;
LM21.0 on: Dell 7490 laptop, 16GB, 1TB. LM19.3 2X Thinkpad T430 i5-3230M,8GB MSata SSD,860 Evo SSD.
LM19.3 Dell D830m laptop, T7500,6GB, Nvidia NVS140M, 860 Evo.
Wendy-J
Level 1
Level 1
Posts: 29
Joined: Thu Nov 18, 2021 10:30 pm

Re: How to install or reinstall Grub

Post by Wendy-J »

Code: Select all

sudo mount /dev/sda1 /mnt
for i in /dev /dev/pts /proc /sys; do sudo mount -B $i /mnt$i; done
sudo cp /etc/resolv.conf /mnt/etc
sudo chroot /mnt
apt purge grub-common
apt install grub-pc os-prober
exit
sudo umount -R /mnt
This gives a bash error for the line

Code: Select all

for i in /dev /dev/pts /proc /sys; do sudo mount -B $i /mnt$i; done
I have sda1 [/boot] plus sda2 [/] and last, sda4 [/home] with sda3 as swap. Each is a discrete partition. Do i need to do something else? Once the machine goes through POST, grub hangs before it finishes loading. I get a black screen that says, Attempting Boot to USB Device then Attempting Boot to Hard Drive and nothing. I do NOT get the error No Boot Manager Found.
User avatar
Moem
Level 22
Level 22
Posts: 16226
Joined: Tue Nov 17, 2015 9:14 am
Location: The Netherlands
Contact:

Re: How to install or reinstall Grub

Post by Moem »

Wendy-J wrote: Mon Feb 21, 2022 11:23 am Do i need to do something else?
As per the top of this page, in red:
Forum rules
Please don't add support questions to tutorials, start your own topic in the appropriate sub-forum instead.
Image

If your issue is solved, kindly indicate that by editing the first post in the topic, and adding [SOLVED] to the title. Thanks!
thaiiceland
Level 1
Level 1
Posts: 43
Joined: Mon Jul 02, 2012 12:10 pm
Contact:

Re: How to install or reinstall Grub

Post by thaiiceland »

Thanks...

I sometimes want to try other distros, and then I always get stuck because that newly installed distro will take control of the GRUB. But whenever I try other distros, I always find out that I already have the best distro, Linux-Mint Cinnamon (my feeling).
I have always wander how I can get my Linux-Mint back to control the GRUB.

This very easy steps helped a lot.
thaiiceland
https://thaiiceland.com
http://thaiiceland.is

ASUS Vivobook M3500QC
AMD Ryzen 9 5900HX 8-cores (16 Threats)
with Radeon NVIDIA GA107M [GeForce RTX 3050 Mobile] Graphics
Linux-Mint Cinnamon (usually the latest version)
andymath
Level 1
Level 1
Posts: 1
Joined: Sat Nov 12, 2022 12:31 pm

Re: How to install or reinstall Grub

Post by andymath »

I cannot thank you enough for this! After hours searching for a solution to no avail, I FINALLY found it! I'll bookmark this post in case my damn Lenovo laptop UEFI overrides Grub again.
May your life be full of blessings, good sir.
Post Reply

Return to “Tutorials”