How can I repair grub? (How to get Ubuntu back after installing Windows?)
I installed Windows 7, which ate Ubuntu's boot file. When starting up the computer, it now goes straight to Windows, without giving me the option of booting Ubuntu.
How can I get Ubuntu back?
514 Answers
When you install Windows, Windows assumes it is the only operating system (OS) on the machine, or at least it does not account for Linux. So it replaces GRUB with its own boot loader. What you have to do is replace the Windows boot loader with GRUB. I've seen various instructions for replacing GRUB by mucking around with GRUB commands or some such, but to me the easiest way is to simply chroot into your install and run update-grub. chroot is great because it allows you to work on your actual install, instead of trying to redirect things here and there. It is really clean.
Here's how:
- Boot from the live CD or live USB, in "Try Ubuntu" mode.
Determine the partition number of your main partition.
sudo fdisk -l,sudo blkidor GParted (which should already be installed, by default, on the live session) can help you here. I'm going to assume in this answer that it's/dev/sda2, but make sure you use the correct partition number for your system!If your main partition is in an LVM, the device will instead be located in
/dev/mapper/, most likely,/dev/mapper/{volume}--{os}-rootwhere{volume}is the LVM volume name and{os}is the operating system. Executels /dev/mapperfor the exact name.Mount your partition:
sudo mount /dev/sda2 /mnt #Replace sda2 with the partition from step 2If you have a separate
/boot,/varor/usrpartitions, repeat steps 2 and 3 to mount these partitions to/mnt/boot,/mnt/varand/mnt/usrrespectively. For example,sudo mount /dev/sdXW /mnt/boot sudo mount /dev/sdXY /mnt/var sudo mount /dev/sdXZ /mnt/usrreplacing
sdXW,sdXY, andsdXZwith the respective partition numbers.Bind mount some other necessary stuff:
for i in /sys /proc /run /dev; do sudo mount --bind "$i" "/mnt$i"; doneIf Ubuntu is installed in EFI mode (see this answer if you're unsure), use
sudo fdisk -l | grep -i efior GParted to find your EFI partition. It will have a label ofEFI. Mount this partition, replacingsdXYwith the actual partition number for your system:sudo mount /dev/sdXY /mnt/boot/efichrootinto your Ubuntu install:sudo chroot /mntAt this point, you're in your install, not the live session, and running as root. Update grub:
update-grubIf you get errors or if going up to step 7 didn't fix your problem, go to step 8. (Otherwise, it is optional.)
Depending on your situation, you might have to reinstall grub:
grub-install /dev/sda update-grub # In order to find and add windows to grub menu.If Ubuntu is installed in EFI mode, and EFI partition UUID has changed, you may need to update it in
/etc/fstab. Compare it:blkid | grep -i efi grep -i efi /etc/fstabIf current EFI partition UUID (from
blkid) differs from the one in/etc/fstab, update/etc/fstabwith current UUID.If everything worked without errors, then you're all set:
exit sudo rebootAt this point, you should be able to boot normally.
If you cannot boot normally, and didn't do step 8 because there were no error messages, try again with step 8.
- Sometimes giving GRUB2 the correct configuration for your partitions is not enough, and you must actually install it (or reinstall it) to the Master Boot Record, which step 8 does. Experience helping users in chat has shown that step 8 is sometimes necessary even when no error messages are shown.
The Windows installer doesn't care about other OS in the system. So it writes own code over the master boot record. Fortunately the solution is easy too.
You need to repair the MBR. Do the following
Boot using a live usb/cd of ubuntu.
Use boot-repair to fix the problem.
After booting with live usb/cd, run following command in terminal:
sudo add-apt-repository ppa:yannubuntu/boot-repair && sudo apt-get update
sudo apt-get install -y boot-repair && boot-repairUse Recomended Repair.
More info -
13Boot from a live Ubuntu USB pendrive or CD
and
Install Boot-Repair on ubuntu by following steps
Open the terminal and run the following commands
sudo add-apt-repository ppa:yannubuntu/boot-repair
sudo apt-get update
sudo apt-get install boot-repairAfter completing the installation you can launch it from System->Administration->Boot-Repair menu if you use Gnome, or search "boot-repair" in the dash if you use Unity. Then follow the following screenshots:
Method 1
- Click on the advanced options
- Tick the options shown below
- Change the tab to Grub Location Tab and Tick The options Shown in the figure
Press Apply and Reboot the system
Method 2
- Select the recommended Boot repair options as shown in the first screenshot
Just install easyBCD in Windows 7 and do
Add New Entry > Linux/BSD > (select ) Grub2 > (push) Add Entry
Then you can choose Ubuntu on the Windows 7 bootloader to go to Grub2 (previous bootloader).
2On EFI-based systems (such as most systems that shipped with Windows 8 or later), Windows will sometimes update its boot loader or reset it to be the default boot loader. This is particularly common when re-installing the OS or performing a major system update (upgrading to the latest Windows release, for instance). Note that Windows is unlikely to actually erase any GRUB files on an EFI-based computer. Everything needed to boot Ubuntu is still in place; it's just being bypassed. In these cases, a complete re-installation of GRUB is overkill, and in fact that carries a (small) chance that it will create new problems.
Thus, instead of re-installing GRUB in these cases, I recommend resetting GRUB (or whatever boot loader or boot manager you prefer) to be the default. There are several ways to do this, including:
- EasyUEFI -- The easiest way to adjust the boot order, if the system is booting straight to Windows, is to use EasyUEFI, which is a free (for the basic version) third-party GUI tool for managing the EFI boot order. It's pretty self-explanatory -- locate the
ubuntuentry in the list of boot options and move it to the top of the list. The next time you reboot, GRUB should come up. (If you use something other than GRUB, you'll need to locate its entry.) bcdedit-- The Windowsbcdedittool can be used to set GRUB to the default boot order. The commandbcdedit /set {bootmgr} path \EFI\ubuntu\grubx64.efi, typed in an Administrator Command Prompt window, will do this; however, if your computer boots with Secure Boot active,bcdedit /set {bootmgr} path \EFI\ubuntu\shimx64.efiwill be required instead. In fact, the latter command will usually work even if Secure Boot is not in use, so I'd use that command first. Note that there's a more advanced Windows shell tool that requires a slightly different syntax than I've presented, but I don't recall the details.- One-time boot to Ubuntu -- Most EFIs provide a built-in boot manager, accessed by hitting a function key, Esc, or Enter early in the system start process. Chances are the
ubuntuentry to boot Ubuntu will show up in this boot manager menu, enabling you to boot to Ubuntu. Alternatively, you could boot to an Ubuntu emergency medium, like the installer booted in "try before installing" mode. Either way, you can then useefibootmgrto adjust the boot order:- Type
sudo efibootmgrto see the boot entries. - Note the current
BootOrderline. - Locate the entry for
ubuntuand note itsBoot####number. - Type
sudo efibootmgr -o xxxx[,yyyy,zzzz,....]to change the boot order, makingxxxxthe number for Ubuntu. What comes after that is most likely not very important, although I've noted that Windows seems to be likely to add itself back to the start of the boot order if it's not in the list. Thus, you should probably ensure that Windows is in the list, and it may be safest to re-order the list so that all the original entries are there, just with theubuntuentry moved to the top of the list.
- Type
- Firmware setup utility -- Some EFIs' setup utilities enable you to adjust the boot order. Details vary greatly from one EFI to another, so I won't go into specifics, but you could look for such an option in your setup utility.
There are other variants on these procedures, such as using bcfg in an EFI shell, using bless in macOS, using my rEFInd to do a one-time boot, etc. I'd start with EasyUEFI, though; it's likely to be the simplest solution. Sometimes Windows insists on making itself the default every time it starts up, though, and reports indicate that bcdedit may do a better job of dealing with that problem.
Note that none of the preceding applies to BIOS-mode installations; however, as most computers that shipped with Windows 8 or later boot in EFI mode, BIOS-mode installations are becoming increasingly rare, so in many cases it's better to deal with the issue in the EFI way rather than by blindly re-installing GRUB.
2There is now a simpler solution:
- Reboot, and enter your computer's BIOS options (F2, or sometimes F11).
- Go to the Boot menu, and select Boot Device Priority
- Check if Windows Boot Manager is above the main boot drive (usually SATA HDD … or IDE HDD …). If it is, move the boot disk priority above that of Windows Boot Manager.
- Save your BIOS options, and exit (usually F10).
This has been tested on a Samsung Series 7 Chronos laptop dual booting Windows 8 and Ubuntu 13.10, secure boot disabled, UEFI and legacy boot enabled.
4Boot-Repair worked for me. It's very very easy to use graphical application, you do not need to use the command line, you only have to click a button :)
All the available repair options are described in the Ubuntu documentation and there is a separate page explaining how to start Boot-Repair (by creating a bootable disk or installing it in an existing Ubuntu live disk) and how to use it.
Just boot a Ubuntu live CD, install Boot-Repair and run it.
1When GRUB is broken, the user generally does not have access to systems, so repair must be performed from a live-session (live-CD or live-USB).
There are many possible causes to a GRUB break: Windows writing on the MBR, DRM preventing GRUB from installing correctly, installer bug, hardware change... Updating GRUB as proposed initially by Scott is generally not sufficient, reinstalling GRUB as proposed by Marco is more efficient, but still there are various situations requiring other tweaks (adding options to kernel, unhiding GRUB menu, changing GRUB options, choosing the right host architecture...). Other difficulties for repairing GRUB is the use of chroot, and the choice of the right partitions /disks.
All of this has been made easy in a little graphical tool: Boot-Repair. It shall be integrated in Ubuntu 12.04 CD for easier use, but for people needing it now, there are already some distros integrating it: Ubuntu-Secured-Remix (Ubuntu CD integrating Boot-Repair), Boot-Repair-Disk (CD running Boot-Repair at start-up), ...
Hope this helps.
Answers given by Scott and Web-E are good enough and have helped a lot many times. But many a times, the boot-repair tool is not able to repair grub due to problems related to i386 and amd64 platform or one where grub can't find efi directory.
What has solved my problem like more than 10 times is to manually purge the old grub installations and install a new one.
So first perform the first 6 steps from Scott's answer where you can skip the 5th step if it gives an error:
- Boot from the live CD or live USB, in "Try Ubuntu" mode.
Determine the partition number of your main partition.
sudo fdisk -l,sudo blkidor GParted (which should already be installed, by default, on the live session) can help you here. I'm going to assume in this answer that it's/dev/sda2, but make sure you use the correct partition number for your system!If your main partition is in an LVM, the device will instead be located in
/dev/mapper/, most likely,/dev/mapper/{volume}--{os}-rootwhere{volume}is the LVM volume name and{os}is the operating system. Executels /dev/mapperfor the exact name.Mount your partition:
sudo mount /dev/sda2 /mnt #Replace sda2 with the partition from step 2If you have a separate
/boot,/varor/usrpartitions, repeat steps 2 and 3 to mount these partitions to/mnt/boot,/mnt/varand/mnt/usrrespectively. For example,sudo mount /dev/sdXW /mnt/boot sudo mount /dev/sdXY /mnt/var sudo mount /dev/sdXZ /mnt/usrreplacing
sdXW,sdXY, andsdXZwith the respective partition numbers.Bind mount some other necessary stuff:
for i in /sys /proc /run /dev; do sudo mount --bind "$i" "/mnt$i"; doneIf Ubuntu is installed in EFI mode ([see this answer if you're unsure][efi]), use
sudo fdisk -l | grep -i efior GParted to find your EFI partition. It will have a label ofEFI. Mount this partition, replacingsdXYwith the actual partition number for your system:sudo mount /dev/sdXY /mnt/boot/efi
chrootinto your Ubuntu install:sudo chroot /mnt
Then do these steps:
- Configure all the pending packages.
sudo dpkg --configure -a - Fix the broken packages.
sudo apt install -fy - Remove the current grub.
sudo apt purge -y grub*-common grub-common:i386 shim-signed
This may give you a warning that your device will have no bootloader and may be unable to boot the next time. Go ahead and accept it. sudo apt install -y grub-pc
After running this command, it will ask you to point the currentsdXYto install the bootloader. Find where your current OS is installed usingsudo fdisk -lcommand. It will be labelled asLinux. Navigate that window using Tab key and select an option using Space key.- If everything goes right, grub will be installed correctly. You can also do
sudo grub-updatefor a double check.
If the problem still persists, you can read Arch's wiki to actually understand about grub to tackle many other varieties of problems. (Yes, also helps for Ubuntu!).
Scott Severance's answer is valid and detailed but there is a resolution that requires no external boot device and so no need to identify and manually mount all of your Ubuntu partitions leading up to the chroot.
In Windows 10, you can use Advanced Recovery to select a device (partition?) to boot from.
Go to Settings and choose Update & Security:
Go to Recovery:
From Advanced Startup choose Restart Now
Then choose Use a Device and all the boot options should be presented to you. Choose the 'ubuntu' partition and your PC should then boot from that partition.
Once booted into my usual Ubuntu environment, I have tried just running grub-update but that made no change.
I then found my /boot/efi partition which was /dev/nvme0n1p1 and ran sudo grub-install /dev/nvme0n1p1 and then sudo update-grub.
This has restored my master boot record multiple times now as there seems to be a new Windows 10 policy of overwriting boot info on minor updates.
2It turns out grub can be fixed from Windows too. Running the following (as administrator) from CMD brought the grub menu back for me.
bcdedit /set {bootmgr} path \EFI\ubuntu\grubx64.efi 1 Windows does not see Linux formatted partitions. You need to use gparted from a liveCD and create a primary partition formatted NTFS with the boot flag.
Some have had issues if the new primary partition is after the extended partition as Windows does not always reset partition table correctly. Best to have good backups and a separate backup of partition table.
Backup partition table to text file & save to external device.
sudo sfdisk -d /dev/sda > PTsda.txtThis is only for MBR (msdos) systems. If your Ubuntu install is in GPT partition drive you can only install Windows in UEFI mode or convert drive back to MBR (msdos).
2Only fsck command fixed the grub-rescue screen for me.
Use Boot-Repair to boot from Live-CD, then open Terminal
Get the correct device:
sudo fdisk -lNeed to find the boot device, the boot device has * under Boot category like here:
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 2000895 1998848 976M 82 Linux swap / Solaris
/dev/sda2 * 2000896 943716351 941715456 449G 83 LinuxNow repair the disk by using:
sudo fsck /dev/sda2 -yNote: /dev/sda2 is the boot device in this example.
Reboot when completed. Done.
I had a different issue, caused (probably) by boot-repair wiping my /etc/grub.d/ templates, outlined in my question here: Ubuntu 18.04 not booting after Windows 10 install
As pointed out by @karels comment, the grub config is generated using the files in /etc/grub.d. My directory only contained /etc/grub.d/25_custom, which contained strange menu entries pointing to non-existent .efi images. It looked like my /etc/grub.d templates were maybe hosed by boot-repair.
I fixed this by:
- Boot with Live CD (probably important to use same Ubuntu version)
sudo mount /dev/sdxx /mntsudo cp /etc/grub.d/* /mnt/etc/grub.d/sudo update-grub- Reboot & relax after 48 hours of pain