While experimenting with various linux distros and dual booting, we often delete or alter linux partitions mistakenly. That's when GRUB rescue is necessary.
At this point, GRUB fails to load a proper configuration file or related modules to boot.
And basically there could be four different types of GRUB rescue mode.
- If the configuration file grub.cfg is missing or corrupted, then it should show the
grub>
prompt to perform further steps. - The entire
/boot/grub/
directory is corrupted or missing and showing thegrub rescue>
prompt, and possibly with unknown filesystem error. Then you've to perform some different rescue steps to boot. - The GRUB 2 bootloader is replaced by another bootloader like of Windows bootmgr or syslinux, then it's another story.
- You don't want to dual boot windows and Linux any more, so formatted the entire Linux partitions. But GRUB is still present, and it's not booting to Windows.
The third situation happens when you install Windows over an existing Linux installation. As the Windows bootloader can't detect the Linux partitions by default.
The fourth point is an entirely different situation, and we're not going to fix that in this tutorial.
This guide is pretty long, so you might want to jump straight to-
Note: It's always good to have a bootable USB ready with at least one linux live distro for a easier rescue, repair or reinstall process.
Contents
GRUB rescue on legacy BIOS systems
So, here we'll deal with 3 different situations of broken GRUB bootloader on older BIOS systems, or the operating systems were installed with BIOS compatibility mode.
When the grub.cfg file is missing or corrupted
To perform grub rescue when the configuration file is deleted or corrupted,
- First run the
ls -l
command to list all available disk partitions and filesystem type. - Now identify your Linux system's root filesystem, in my case it was
(hd0,msdos5)
, highlighted with the red underline. Why msdos? As I was using the older MBR partition table. - To check if everything is all right under the linux partition you can use the ls command again.
ls (hd0,5)/
andls (hd0,5)/boot/
. - If you can see the vmlinuz-xxxx and initrd.img-xxxx files under the /boot directory then start loading the kernel and initrd as instructed below.
- Type the command below one by one, but not the exactly same commands. Your system will be different from mine most probably. Tip: Use the Tab key while typing.
set root=(hd0,5) linux (hd0,5)/boot/vmlinuz-4.10.0-28-generic root=/dev/sda5 initrd (hd0,5)/boot/initrd.img-4.10.0-28-generic boot
- After typing the last command and hitting the Enter key, Linux should start booting.
- When the boot process is completed, login as usual and open up the the terminal application to update the GRUB configuration.
- Just run the
sudo update-grub
command and it should do all the hard work for you.
Basically you need to set the proper path for the kernel and the initrd image file. The good news is the Tab autocomplete works just fine withing the grub rescue shell. So you can hit the Tab
key to get visual feedback and avoid typing long lines.
Note: If you have a backup copy of the grub.cfg file, then you can use the loadconfig command to easily boot to the Linux distro.
grub> loadconfig (hd0,msdos1)/root/backup/grub.cfg
The above command is an example, assuming the system have a backup grub configuration file under the /root/backup
directory.
GRUB repair when the grub folder is missing or corrupted
If the contents of /boot/grub/
directory is accidentally deleted or corrupted somehow, the rescue process is little different.
And there's two possibility in this case, first try to load few modules with the insmod command, like insmod normal
.
If it can load the modules, then it's possible to rescue without a Live CD. But if it can't, the you'll definitely need a live CD or USB, and follow the next step below.
So, if the grub rescue prompt can load the modules, then you can recover it. You've to type the commands listed below one by one, but don't forget to modify them according to your system.
set root=(hd0,5) set prefix=(hd0,5)/boot/grub insmod normal normal
If there is any grub.cfg file under the prefix directory, /boot/grub
in this case, then it will be used. The GRUB selection menu will pop out just after typing the normal command.
If there's no GRUB selection menu, then you've to specify the kernel and initrd files and manually start the boot process. Then the commands should look like below.
set root=(hd0,5) set prefix=(hd0,5)/boot/grub insmod normal insmod linux linux (hd0,5)/vmlinuz root=/dev/sda5 initrd (hd0,5)/initrd.img boot
In Ubuntu or Debian, the linux kernel and initrd files are symlinked to the /
directory, so using the symlinked files here.
Again after booting to the linux system, run the sudo update-grub
command to regenerate the grub configuration file.
When GRUB is overwritten by other bootloader
If Windows is installed after installing Linux, most probably Linux won't boot at all. The windows bootloader can create Linux entries, but that's another long way. This step is also for you if the GRUB installation directory is deleted or corrupted.
So, here we will use a linux live USB to fix this issue. If the installed linux is 64 bit, then any 64 bit live CD should work well.
First get a Live CD or USB and boot your PC/Laptop from the USB or the CD drive. That's should be pretty easy for you, I guess.
After booting to the live session, recognize the linux root partition with fdisk -l
command. In my case, it was /dev/sda5
, but your's could be different.
Now mount the Linux root partition, as we're going to to setup a chroot environment. I'm making a new directory,/mnt/chroot
for easier understanding.
sudo mkdir -p /mnt/chroot sudo mount /dev/sda5 /mnt/chroot/
Again, don't just type the commands above, modify them according to your system. Now check if the the linux root partition is mounted properly or not with the command below.
ls -l /mnt/chroot
If everything is mounted properly, then start the chroot process, commands listed below.
sudo -i # login as root, on the live session
mount -B /dev/ /mnt/chroot/dev/
mount -B /dev/pts/ /mnt/chroot/dev/pts/
mount -B /proc/ /mnt/chroot/proc/
mount -B /sys/ /mnt/chroot/sys/
chroot /mnt/chroot/
At this point you should be inside the chroot shell. Additionally you can run the source /etc/bash_completion
command to enable Tab auto completion.
Finally install GRUB, make sure you know the correct device you want to install. In my case, the targeted device for grub installation was /dev/sda .
grub-install /dev/sda --boot-directory=/boot/ --target=i386-pc # inside the chroot
Grub installation should be complete within a minute, if there's no problem, update the GRUB configuration
update-grub # inside the chroot
Now exit the chroot by running the exit
command, and reboot the Laptop/PC. Though it's better to unmount the linux partitions before reboot, but jsut leave it this time.
GRUB rescue on UEFI systems
In case the operating systems or GRUB is loaded in UEFI mode, few different steps needs to be performed.
So, here's the guide to rescue broken GRUB on UEFI based systems, more precisely if you prefer to boot the operating system in UEFI mode.
Few things to remember -
- If you laptop/desktop supports UEFI, it doesn't mean that you're using UEFI to load the operating systems.
- If the hard drive/SSD has a GPT partition table, then there's a higher chance that you're using UEFI.
- There must be a FAT32 partition of around 100MB or more if the system is using UEFI.
- This FAT32 partition is called EFI System Partition, or ESP.
- In case Windows 10 was pre-installed on your laptop, then most probably it's using UEFI by default.
Here you can check if you're using UEFI at all or not, both on Windows and Linux. However it's not possible to check the same if you're on GRUB rescue mode.
Boot from the grub rescue prompt on UEFI system
At this point, you could have five possibilities.
- GRUB and it's all related files are present, jut the configuration is corrupted or missing.
- Hard drive's partition UUID is changed, rescue prompt is showing no such device or no such partition error.
- All the GRUB related file under the
/boot/grub
directory are missing or corrupted. - The GRUB UEFI executable file is missing or corrupted.
- Entire Linux partition is deleted. In that case it's impossible recover, full re-installation is required.
Note: Please don't be confused here, I'm just telling you the possibilities. If the third and fourth point happened, it's not possible to recover without a Live CD or USB.
The fifth point is out of discussion in this tutorial. Instead you could follow the dual boot tutorial to re-install Ubuntu.
Check if the EFI System partition is present or not
Undoubtedly this is the first thing you should check.
Why I'm stressing this point? May be your motherboard supports UEFI, but you're not using it. And I've seen it a lot.
So one way to be sure about that to check if the ESP partition is present or not.
First use the ls
command on the rescue prompt to list all the disk partitions.
Then again use the ls (hd0,X)
command to check the filesystem type. Replace the X with actual partition number, like 1, 2, 3.
ls (hd0,2)
The above screenshot shows that the 2nd partition is the ESP. It's a real 500 GB hard drive connected to Virtualbox to take better screenshots. However there's no Linux installed, just to show you the ESP.
Note: If there's more than one hard drive installed on your system, then it will also show the other drive as (hd1)
.
No such partition grub rescue
If you don't know what is the Linux root partition yet, then you've to find it out.
Again we'll be using the ls command to check the contents of each partition.
grub rescue> ls (hd0,x)/ # use a trailing slash
Replace the X with actual partition number. The linux root (/) partition should contain bin
, boot
, etc
and other similar directories.
After being sure about the linux root partition, run these commands one by one on the rescue prompt.
set root=(hd0,x) set prefix=(hd0,x)/boot/grub insmod normal normal
Also don't forget to replace the x with the actual hard drive partition number. If there's a proper grub configuration file under the /boot/grub
directory, it will be loaded.
This should take you to the GRUB menu, select the Linux operating system and boot to it.
After successfully booting and logging in, you've to install GRUB and generate new configuration file.
Open up the terminal application, any of them should be fine.
Then mount the ESP partition and install GRUB with UEFI support, example below.
sudo mount /dev/sda2 /boot/efi sudo grub-install /dev/sda --target=x86_64-efi
Here assuming the /dev/sda
is the main boot drive. There should be no error while installing the GRUB bootloader.
Finally update the GRUB configuration with sudo update-grub
, and you should be able to boot normally after next reboot.
Reinstall GRUB on UEFI system with a Live CD
As I've said before, if the GRUB modules under /boot/grub directory or the GRUB UEFI executable files, namely BOOTx64.efi and grubx64.efi are corrupted or missing, it's not possible to repair from the rescue mode.
So, here we've to use a linux live CD or live USB, any 64 bit live linux distro should work.
Assuming you know how to create a live USB and you already have one ready.
So, power off the laptop/PC, plug in the USB drive containing your favorite live distro and boot to it.
After successfully booting into the live session, you've to identify the linux root partition and the EFI system partition.
Use the fdisk
command, example below.
sudo fdisk -l /dev/sda
So, here you can see that the /dev/sda1
is the EFI system partition and the /dev/sda2
is the linux root filesystem.
Now we've to mount those partitions accordingly and create a chroot environment. We'll be using a folder, /mnt/chroot
to mount the partitions.
sudo mkdir -p /mnt/chroot # create the chroot directory
sudo mount /dev/sda2 /mnt/chroot/
sudo mount /dev/sda1 /mnt/chroot/boot/efi
Let's mount other necessary directories to the chroot environment.
sudo -i # login as root, on the live session
mount -B /dev/ /mnt/chroot/dev/
mount -B /dev/pts/ /mnt/chroot/dev/pts/
mount -B /proc/ /mnt/chroot/proc/
mount -B /sys/ /mnt/chroot/sys/
chroot /mnt/chroot/
After chrooting inside the /mnt/chroot
directory, it's time to reinstall the GRUB in UEFI mode and update the configuration.
sudo grub-install /dev/sda --target=x86_64-efi # inside chroot
update-grub
Both of these should run without any error. Finally restart the system to check if the GRUB reinstall is successful or not.
Conclusion
If everything was done correctly you should be able to see the GRUB's OS selection menu, along with other OS entries, if there is any.
I hope this tutorial will help you to deal with varuious grub rescue related situations, and it's simple enough to understand.
But here the real point, did it worked on your system to rescue or repair grub? If not please let me know through the comments, and also don't forget to share your opinions.
Scott Bertilson says
Big help for me too in recovering my UEFI boot after it went out to lunch.
Goutham S Bharadwaj says
This is a great resource. It helped me a lot. Thank you!!
Martin says
Hello,
Probably it's easier to reinstall the whole system at this point for me, but maybe there is something to do. I am using this method: "Reinstall GRUB on UEFI system with a Live CD"
but when i get to the last line "sudo grub-install /dev/sda --target=x86_64-efi " it shows:
grub-install: command not found
if i enter w/o sudo "grub-install /dev/sda --target=x86_64-efi " it shows:
the program 'grub-install' can be found in the following packages:
grub
grub2-common
lupin-support
Try: apt-get install selected packages.
And no success if i even try to install these suggested packages because they have unmet dependencies which cant be installed because unmet dependencies, it just seems like a loop of unmet dependencies and breaks where they point to each other.
apt-get -f install doesnt help...
I dont know what or why... have i deleted too much from my system? p.s. i have a separate /boot partition. Any advice?
Arnab Satapathi says
A separate /boot partition is useless, my opinion, while a separate /home partition is life saver.
As a last resort, try
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install grub2
Martin says
Hmm, saw your response only on 17th, but it didn't seem to do the trick.
Anyway i posted my results, if it's too long or useless here you can delete it.
Maybe you can see the problem, am i even mounting correctly, which i assume i do?
As mentioned before i might have deleted too much, beyond a reasonable restorability time, if that is your conclusion.
mint@mint:~$ sudo mkdir -p /mnt/chroot
mint@mint:~$ sudo mount /dev/sda1 /mnt/chroot/ >>> /boot partition
mint@mint:~$ sudo mount /diopuytrewqasdfghjklmnbvcxz ev/sda4 /mnt/chroot/boot/efi >>> /efi partition
mint@mint:~$ sudo mount /dev/sda5 /mnt/chroot/ >>> /root partition
mint@mint:~$ sudo -i
root@mint:~# mount -B /dev/ /mnt/chroot/dev/
root@mint:~# mount -B /dev/pts/ /mnt/chroot/dev/pts/
root@mint:~# mount -B /proc/ /mnt/chroot/proc/
root@mint:~# mount -B /sys/ /mnt/chroot/sys/
root@mint:~# chroot /mnt/chroot/ho
mint / # sudo apt-get updateh
Fetched 7174 kB in 10s (660 khB/s)
Reading package lists... Done
mint / # sudo apt-get dist-upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these.
The following packages have unmet dependencies:
language-pack-en-base : Depends: locales (>= 2.3.6) but it is not installed
printer-driver-gutenprint : Depends: cups (>= 1.3.0)
printer-driver-splix : Depends: cups (>= 1.5.0-3~)
E: Unmet dependencies. Try using -f.
mint / # sudo apt-get -f install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following packages will be REMOVED:
language-pack-en language-pack-en-base printer-driver-gutenprint
printer-driver-splix
0 upgraded, 0 newly installed, 4 to remove and 5 not upgraded.
12 not fully installed or removed.
After this operation, 5433 kB disk space will be freed.
Do you want to continue? [Y/n] Y
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = "C.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
Can't exec "locale": No such file or directory at /usr/share/perl5/Debconf/Encoding.pm line 16.
Use of uninitialized value $Debconf::Encoding::charmap in scalar chomp at /usr/share/perl5/Debconf/Encoding.pm line 17.
dpkg: warning: 'ldconfig' not found in PATH or not executable
dpkg: error: 1 expected program not found in PATH or not executable
Note: root's PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin
E: Sub-process /usr/bin/dpkg returned an error code (2)
mint / # sudo apt-get install locales
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
libc6 : Breaks: locales (< 2.26) but 2.13+git20120306-12.1 is to be installed
libc6:i386 : Breaks: locales (= 1.3.0)
printer-driver-splix : Depends: cups (>= 1.5.0-3~)
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
Martin says
Sorry for the random letters, my browser froze the minute i tried to post...
Martin says
Hmm, saw your reply only on 17th, but it didn't seem to do the trick still.
Anyway, i posted my results, if it's too long or useless here you can delete it.
Maybe you can see the problem, am i even mounting correctly, which i assume i do?
As mentioned before i might have deleted too much, beyond reasonable restorabillity time.
mint@mint:~$ sudo mkdir -p /mnt/chroot
mint@mint:~$ sudo mount /dev/sda1 /mnt/chroot/
mint@mint:~$ sudo mount /dev/sda4 /mnt/chroot/boot/efi
mint@mint:~$ sudo mount /dev/sda5 /mnt/chroot/
mint@mint:~$ sudo -i
root@mint:~# mount -B /dev/ /mnt/chroot/dev/
root@mint:~# mount -B /dev/pts/ /mnt/chroot/dev/pts/
root@mint:~# mount -B /proc/ /mnt/chroot/proc/
root@mint:~# mount -B /sys/ /mnt/chroot/sys/
root@mint:~# chroot /mnt/chroot/
mint / # sudo apt-get update
Fetched 5684 kB in 7s (796 kB/s)
Reading package lists... Done
mint / # sudo apt-get dist-upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these.
The following packages have unmet dependencies:
language-pack-en-base : Depends: locales (>= 2.3.6) but it is not installed
printer-driver-gutenprint : Depends: cups (>= 1.3.0)
printer-driver-splix : Depends: cups (>= 1.5.0-3~)
E: Unmet dependencies. Try using -f.
mint / # sudo apt-get -f install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following packages will be REMOVED:
language-pack-en language-pack-en-base printer-driver-gutenprint
printer-driver-splix
0 upgraded, 0 newly installed, 4 to remove and 5 not upgraded.
12 not fully installed or removed.
After this operation, 5433 kB disk space will be freed.
Do you want to continue? [Y/n] Y
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = "C.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
Can't exec "locale": No such file or directory at /usr/share/perl5/Debconf/Encoding.pm line 16.
Use of uninitialized value $Debconf::Encoding::charmap in scalar chomp at /usr/share/perl5/Debconf/Encoding.pm line 17.
dpkg: warning: 'ldconfig' not found in PATH or not executable
dpkg: error: 1 expected program not found in PATH or not executable
Note: root's PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin
E: Sub-process /usr/bin/dpkg returned an error code (2)
mint / # sudo apt-get install locales
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
libc6 : Breaks: locales (< 2.26) but 2.13+git20120306-12.1 is to be installed
libc6:i386 : Breaks: locales (= 1.3.0)
printer-driver-splix : Depends: cups (>= 1.5.0-3~)
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
Martin says
It seems a last resort didn't do the trick still...
Do i even mount correctly, which i assume i do because the other way around mount point doesn't exist for later commands?
mint@mint:~$ sudo mount /dev/sda1 /mnt/chroot/ >>> /boot partition
mint@mint:~$ sudo mount /dev/sda4 /mnt/chroot/boot/efi >>> /efi partition
mint@mint:~$ sudo mount /dev/sda5 /mnt/chroot/ >>> /root partition
Posted some of my results, maybe you can see the problem there or you conclude that it is beyond a reasonable restorabillity time and effort? I do have a separate /home too. 🙂
mint / # sudo apt-get update
Fetched 3426 kB in 5s (658 kB/s)
mint / # sudo apt-get dist-upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these.
The following packages have unmet dependencies:
language-pack-en-base : Depends: locales (>= 2.3.6) but it is not installed
printer-driver-gutenprint : Depends: cups (>= 1.3.0)
printer-driver-splix : Depends: cups (>= 1.5.0-3~)
E: Unmet dependencies. Try using -f.
mint / # sudo apt-get install locales
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
libc6 : Breaks: locales (< 2.26) but 2.13+git20120306-12.1 is to be installed
libc6:i386 : Breaks: locales (= 1.3.0)
printer-driver-splix : Depends: cups (>= 1.5.0-3~)
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
Nick Bulka says
how do i boot a usb drive from grub rescue??
Arnab Satapathi says
If the prompt is
grub rescue>
, then it's possible.Set the root device is
(hd1,1)
, or something like that. depending on how many bootable device are connected.Also you've to load proper modules, kernel and initrd.
You may also need partition UUID.
Kaushik Guha says
Arnab a very BIG THANK YOU, for the painstaking effort but an effective and impeccable document/guide you've presented for us.
But unfortunately brother I am facing some problems. PLEASE HELP.
My Desktop-PC runs on UEFI BIOS with Dual mode boot.
Windows-10(64-bit) and Fedora-28(64-bit).
I had to re-install Windows again, due to some problems and issues, as a result, the "grub" got overwritten by Windows boot manager.
I followed your steps in the guide exactly step-by-step with proper and successful "chroorting" of the environment under live USB(Fedora distro).
Now the errors/issues, which I present:--->
[root@localhost-live /]#grub-install /dev/sda --target=x86_64-efi
bash:grub-install:command not found
So, I tried this,
[root@localhost-live /]#grub2-install /dev/sda --target=x86_64-efi
grub2-install:error:/usr/lib/grub/x86_64-efi/modinfo.sh doesn't exist. Please specify --target or --directory
My Desktop system's "efi" resides on "/dev/sda1" and root of Fedora linux resides on "/dev/sdc1"
PLEASE HELP ME, BROTHER.
Arnab Satapathi says
Hey Koushik, a bit late though.
Make sure that /dev/sda1 under the /boot/efi directory.
Add few more parameters to the grub install command, inside the chroot.
The directory
/usr/lib/grub/x86_64-efi/
should exist, if not then reinstall GRUB.Graham says
Run theefi procediure exactly as per your instructions. Still get grub rescue ! Situation was i had 2 linux versions. Mint 19 anf ubuntu. I stipidly removed the ubuntu version where grub was orinally install. I can boot the sysyem by fiddling with grub rescue but have not found a way of fixing the boot lol. Might be easier to reinstall after somebackup. Thanks for any comments
Arnab Satapathi says
May be because I forgot to add the
sudo update-grub
at the end of the command list, you too.BTW, what the error screen is actually showing? The grub rescue prompt of the EFI shell?
David says
Wonderful writeup!
You might add a bit about what targets are common for
grub-install.
Also, I see systems with a separate boot partition. Some readers
might appreciate seeing an example of the "set root", "linux ..."
and "initrd ..." commands were root ("/") and boot ("/boot") are on
different partitions.
Again! This is GREAT!
Thank you!
Arnab Satapathi says
You're most welcome, and also thanks for both the suggestion.
Surely I'll add some paragraphs related to GRUB target, and if there's separate boot partition.
Suyash says
Sir please tell me the solution of 4 th point i stuck here and feeling like hell ...please sir help.me
Arnab Satapathi says
You mean dual boot?
If it's so, then could be fixed exactly like described below.
Just find out the correct Linux root partition on the GRUB rescue prompt.
"feeling like hell", I know, same happened with me a lot. 🙂