45

I want to reinstall GRUB 2 and I found these instructions: How to Repair, Restore, or Reinstall Grub 2 with a Ubuntu Live CD or USB. In my case, the boot loader is installed in the EFI partition. If I use the commands provided in this guide, will GRUB be reinstalled to the EFI partition automatically, or will it be installed into the root partition where Ubuntu is installed ? Obviously, I do not want this to happen.

  • 3
    The instructions in the provided link are valid for reinstalling GRUB in legacy BIOS mode only, this will not work in your case. To reinstall GRUB to an Ubuntu installation in EFI BIOS mode, please read my answer. :) – cl-netbox Sep 29 '16 at 16:36
  • Thank you very much! :) I have some questions: when I installed Ubuntu in my UEFI system, I found two entries in the bios. Is there a way to have a unic ubuntu entry? In case of MBR partition table (so no EFI or any other boot partition), can I use the same commands except for: sudo mount /dev/sd** /mnt/boot/efi ? – Generoso Sep 30 '16 at 10:11
  • You can try to remove the second Ubuntu entry with : sudo efibootmgr (lists all entries) | sudo efi bootmgr -b <entry-number> -B .... and to reinstall GRUB in legacy BIOS mode execute these commands : sudo mount /dev/sd** /mnt | sudo grub-install --boot-directory=/mnt/boot /dev/sd* (* = disk | ** = system partition) ! :) – cl-netbox Sep 30 '16 at 10:54
  • Perfect :) Anyway, having a UEFI system and so a EFI partition, is there a way to avoid getting grub overriden when I perform a new Windows installation? – Generoso Sep 30 '16 at 12:18
  • Well, Microsoft doesn't take care for anything else but their own products, so you can install Linux systems without doing any harm to Windows - unfortunately this isn't the case the other way around - so when you install Windows after you have installed Ubuntu, you have to restore the GRUB boot loader afterwards. :) – cl-netbox Sep 30 '16 at 13:23
80

Reinstall the GRUB boot loader to your Ubuntu installation in EFI mode this way ...

Boot from the Ubuntu installation medium and select 'Try Ubuntu without installing'.
(Boot your install medium in EFI mode, select the Ubuntu entry with UEFI in front.)

Once you are on the Live desktop, open a terminal and execute these commands :

sudo mount /dev/sdXY /mnt
sudo mount /dev/sdXX /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/sdX
update-grub  

Note : sdX = disk | sdXX = efi partition | sdXY = system partition

To identify the partitions use GParted, the tool is included in the installation medium.
After having run the commands GRUB will be installed in the separate EFI partition.

| improve this answer | |
  • 2
    +1 but why don't you install from Ubuntu itself? – user595510 Nov 8 '16 at 13:55
  • 2
    @MarkYisri : Thank you very much ! :) Because it is the safest way to reinstall the GRUB boot loader without corrupting things and in case the system does not boot properly ... the only way to do it ! :) – cl-netbox Nov 8 '16 at 13:58
  • 3
    In this solution, how does 'grub-install' know it is supposed to install in EFI mode? – user334639 Oct 12 '17 at 0:38
  • 6
    The important point is to boot the installation media in the correct boot-mode, if we want to reinstall grub-efi we have to boot in UEFI-mode, if we wanty to reinstall grub-pc we have to boot in legacy-mode. – mook765 Mar 6 '18 at 12:20
  • 2
    This worked for me except with one alteration: I did grub-install /dev/sdXX, i.e. not the disk but the EFI partition was used for installing grub and it worked. – Duck Dodgers Mar 12 '19 at 21:56
8

this is the only way that worked for me: (System: sdb8, boot: sdb6, efi: sdb2)

sudo mount /dev/sdb8 /mnt 
sudo mount /dev/sdb6 /mnt/boot 
sudo mount /dev/sdb2 /mnt/boot/efi

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

sudo chroot /mnt

grub-install --target=x86_64-efi /dev/sdb

grub-install --recheck /dev/sdb

exit &&
sudo umount /mnt/sys &&
sudo umount /mnt/proc &&
sudo umount /mnt/dev/pts &&
sudo umount /mnt/dev &&
sudo umount /mnt
| improve this answer | |
  • 1
    What does --recheck do? The manual says delete device map if it already exists? – MrCalvin Mar 27 '19 at 22:45
  • If I call the boot-entry anything different the the default, e.g. --bootloader-id=Ubuntu_02 the boot fails. It just boot in the grub console...any solution? – MrCalvin Mar 27 '19 at 22:47
  • yes. it seems grub-install --recheck is needed. It fixed my non-working USB HDD EFI. – solsTiCe Aug 19 '19 at 12:45
4

Thanks to @cl-netbox for the instructions!

After I upgraded (Linux Mint 18.2 Sonya to 18.3 Sylvia) my system wouldn't boot so I followed the instructions above but still no success. I noticed however that my machine has /boot in a separate partition (possibly because I am using LVM) so my slightly modified process was:

sudo mount /dev/sdXXX /mnt
sudo mount /dev/sdXY /mnt/boot
sudo mount /dev/sdXX /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/sdX
update-grub 

Note : sdX = disk | sdXX = efi partition | sdXY = boot partition | sdXXX = system partition

| improve this answer | |
  • 1
    I had a running Ubuntu Bionic system on which I accidentally did rm -Rf /boot/efi ! My system was still running, so I tried using the last two commands (grub-install /dev/mapper/ubuntu--vg-root AND update-grub). Rebooted, and everything work perfectly. Phewy and thanks :) – Roel Van de Paar Jul 29 '18 at 22:04
3

Also, if booting from live cd to recover it might happen that you are missing grub-efi-amd64-bin package and then line

"grub-install --target=x86_64-efi /dev/sdb" 

fails with error message: "grub-install: error: /usr/lib/grub/x86_64-efi/modinfo.sh doesn't exist. Please specify --target or --directory."

In this case run this outside of chroot

sudo apt get grub-efi-amd64-bin

and then add /usr/lib/grub/x86_64-efi to chroot mounts.

BTW "/dev/sdb" param is obsolete and is being ignored.

| improve this answer | |
3

If you happen to lose your EFI partition, it's easy to get it back. You can use a partitioning tool such as fdisk or parted to create a new partition sdXY (e.g. sda1) with type "EFI partition (1)" and format it with:

sudo mkfs.msdos /dev/sdXY

then mount it with:

sudo mount /dev/sdXY /boot/efi

and you can reinstall GRUB by running:

sudo grub-install --efi-directory=/boot/efi

as mentioned in other solutions.

| improve this answer | |
  • 1
    Surely you format a partition on the disk i.e. /dev/sdX1, and not the whole block device? – Josip Rodin Mar 27 at 21:18
3

This is how I did it on a standard x86_amd64 EFI desktop, without chrooting, assuming you have a partition containing Ubuntu on your hard drive and possibly an EFI partition where GRUB should be installed.

# boot on a live Ubuntu, I used 18.04 but more recent should work

# if you have currently no EFI partition (maybe it was deleted,
# or you are migrating to a new drive):
# sudo gparted
# - create a FAT 32 partition of around 100 MB on the disk of your choice
# (in general the one that host the Ubuntu partition). If you plan to
# move or resize some paritions, anticipate that (for instance by
# creating the EFI partition at the end of the free space).
# - set the flag esp on this partition (the flag boot will also be selected)

# now assuming that the Ubuntu partition is `/dev/sda2` and the (possibly new) EFI partition is `/dev/sda1`
sudo apt install grub-efi
sudo mkdir /media/root && sudo mount /dev/sda2 /media/root
sudo mkdir /media/efi && sudo mount /dev/sda1 /media/efi
sudo grub-install --target=x86_64-efi /dev/sda --efi-directory=/media/efi --boot-directory=/media/root/boot

This should give:

Installing for x86_64-efi platform.

Installation finished. No error reported.

Then reboot and you should be done. You may have to tell your BIOS which drive to use, or which EFI partition to use, or which EFI binary to use.

If you created a new EFI partition, you may have to add it to /etc/fstab to have update-grub working correctly.

For more information : https://wiki.archlinux.org/index.php/Multiboot_USB_drive#Hybrid_UEFI_GPT_+_BIOS_GPT/MBR_boot

| improve this answer | |
2

in addition to ci-netbox answer.
If your pendrive OS version does not match the one that is installed on the disk, grub-install may have difficulties to identify the right grub installation:

$ sudo chroot /mnt
# grub-install /dev/sdX
grub-install: error: /usr/lib/grub/i386-pc/modinfo.sh doesn't exist. 
Please specify --target or --directory.

Try to identify manually the installation to use

# ls /usr/lib/grub/
grub-mkconfig_lib  x86_64-efi  x86_64-efi-signed

Then restart grub-install :

# grub-install --target=x86_64-efi /dev/sdX 
Installing for x86_64-efi platform.
Installation finished. No error reported.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.