Monday, 3 August 2015


      Earlier, life was so simple. After Ubuntu installation GRUB was booted by default as soon as the PC was turned on. From there, the user was able to select the OS of his choice - be it Windows or Ubuntu.

     But nowadays I find many PCs(mostly HP) that are hard coded to boot from Microsoft's boot loader. As a result of which, even after installing Ubuntu, the PC directly boots into Windows by default without giving the user a choice to boot into Ubuntu.

     The only way to load GRUB is to press f9 as soon as the PC starts and then choose Ubuntu. This is so not cool. Here are quick steps which will make your PC to BOOT INTO GRUB BY DEFAULT. 



Step 1: Firstly, boot into Ubuntu using the key used to change the boot options.(ex: f9 for hp, f12 for Dell)

Step 2: Open the terminal(Ctrl+Alt+t) and type the command: 

sudo su

And enter the password when prompted for it.

Step 3: Make a backup of the boot directory(/boot) by copying and saving it onto the hdd by typing the following command:

cp -r /boot boot.bkp

(Note : This command will create a backup of the boot directory as boot.bkp on the current working directory.)
Step 4:  Now move the file /boot/efi/EFI/Microsoft/Boot/bootmgfw.efi to the path /boot/efi/EFI/Microsoft by typing in the following command:

mv /boot/efi/EFI/Microsoft/Boot/bootmgfw.efi /boot/efi/EFI/Microsoft

(Note : If you don't find this file in the location mentioned in the command, refer to the reason given for this step below in the blog.)

Step 5: Copy the files grubx64.efi and shimx64.efi from /boot/efi/EFI/Ubuntu and paste it into /boot/efi/EFI/Microsoft/Boot folder by typing the following command:

cp /boot/efi/EFI/Ubuntu/grubx64.efi /boot/efi/EFI/Ubuntu/shimx64.efi /boot/efi/EFI/Microsoft/Boot

Step 6: Rename the file shimx64.efi to bootmgfw.efi by typing the following command:

mv /boot/efi/EFI/Microsoft/Boot/shimx64.efi /boot/efi/EFI/Microsoft/Boot/bootmgfw.efi

Step 7: Now open the file /boot/grub/grub.cfg in gedit by typing the following command:

gedit /boot/grub/grub.cfg

Find(Ctrl+f) "chainloader" in the file.

(Note : You will be directed to the line: "chainloader /EFI/Microsoft/Boot/bootmgfw.efi".)

Step 8: Edit that line to "chainloader /EFI/Microsoft/bootmgfw.efi" and save the changes.

After following these steps, when you turn on your PC the next time, you will be shown GRUB by default from where you can choose the OS of your choice.



          The reason why these certain PC's boot into Windows by default is because the boot order of Microsoft's boot loader is set first in the list.
So when PC is turned ON, the Microsoft's boot loader(bootmgfw.efi) boots and Windows is loaded.

By executing these steps, we are fooling the PC to boot GRUB(by loading shimx64.efi, which is required when Secure Boot is active, which in turn loads grubx64.efi).

We are fooling the PC by :
  •  Moving the Microsoft's boot loader (bootmgfw.efi) to some other location.
  •  Copying the shimx64.efi and grubx64.efi files to the original location of bootmgfw.efi(i.e before moving it).
  • Renaming the copied shimx64.efi file as "bootmgfw.efi" (so that binary behind the bootmgfw.efi name is of shimx64).
So from now on, when the PC is turned ON it will boot the bootmgfw.efi file but this file now has the binary of shimx64. Thus grubx64 will be loaded and the PC will boot into grub by default.


Reason for Step 1 : We need to boot into Ubuntu first because all the changes we are going to make are through Ubuntu.

Reason for Step 2 : In order to make these changes, one must have superuser privileges. So second step is undertaken to become superuser.

Reason for Step 3 : Changes we are going to make are in the boot directory. A backup of this boot directory is to be created before we proceed so that if anything goes wrong, we have a backup.
I have created the backup by copying and pasting /boot as boot.bkp in the current working directory; you can have a destination of your own :
    cp -r /boot <destination path>

Reason for Step 4
: The idea behind this step is to move the Microsoft's bootmgfw.efi file (which is needed to boot windows) to some other location(but within the EFI partition only). In my PC, the location of this file is as mentioned in the command. If the location of this file is somewhat different in your PC, then change the path in the command accordingly :
    mv <file path> <destination path>

Reason for Step 5 : shimx64 is a simple program that provides a way to boot a computer with Secure Boot active.
grubx64 is the grub binary.
We need to copy these files to the bootmgfw.efi's original location(i.e. the location before moving it).

Reason for Step 6 : Rename the shimx64.efi file as bootmgfw.efi
What we have done so far is that we have fooled the PC(which boots bootmgfw.efi by default to load windows) to boot shimx64.efi by renaming shimx64.efi as bootmgfw.efi
So now, shimx64 will be loaded by default as bootmgfw which in turn will boot grub by loading grubx64.efi present in the same location.

Reason for Step 7 : Note that till now we have made the provision of booting into grub by default. Now the grub will identify both Windows and Ubuntu OS. So we need to configure grub such that when Windows is selected, it loads the Microsoft's bootmgfw.efi file which we moved to some location in Step -2.
open the file  /boot/grub/grub.cfg which is used to configure grub.

Reason for Step 8 : Find the line which says : chainloader <old path of Microsoft's bootmgfw.efi>
This line instructs the grub to load the file mentioned.
We need to change the filepath to the new path as per Step-2 so that when Windows is selected, the Microsoft's bootmgfw.efi is loaded(which is the binary for Windows OS).



    Whenever there is an operation like update-grub or some updates in Ubuntu which leads to the regeneration of  grub.cfg file then the changes we made in Step 8 is undone. As a result of which, in GRUB menu, when Windows is selected then the GRUB is loaded again.

   So simply undergoing Step 8 solves the issue.

Hope this would solve the issue faced by many HP computer users and more, and make booting into the desired OS simpler.

Disclaimer : Above steps have been tested on hp - Pavillion 15-p073TX with Windows 8.1 and Ubuntu 14.04 as the installed OS.

No comments:

Post a Comment