Edit grub from Windows?

The title of the question is what the OP proposed solution to the problem is. The title could be changed to "How can I have Grub boot to the last used OS choice automatically?".

This should arguably be default grub behavior. For example, I might be using Windows for a week playing Mass Effect IV and never come into Ubuntu. Or I might be testing something in Ubuntu 14.04 with a specific Kernel and I don't like having to pick it every time over 50 reboots.

There is a link in various comments above to a Stack Exchange answer proposed as a duplicate question. We can reference an AskUbuntu Q&A too: How to get grub2 to remember last choice?

In summary edit /etc/default/grub, comment out one line and insert two lines below it:

#GRUB_DEFAULT=0 # Rather than first menu option, we'll default to last  OS.
GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true

Then save the changes and run sudo update-grub.

Voila! Whilst working in Windows and automatic updates happen at 3 am the system reboots to Windows and applies Stage 3 of 3.

OP is proposing "Edit Grub from Windows?" in the title as a solution to the problem. However, after stating the reason why, it becomes clear the title should be changed to something like: "How do I ensure Windows automatic update reboots go to Windows instead of Ubuntu?".

If the question was "How do I reboot to the last OS choice?" it's an obvious duplicate of the link. The proposed new title (or something shorter) has merit because others may search on the same problem. I know I've had the Windows update-reboot-update problem before and never would have imagined to search on the link here.

PS You can edit Grub from Windows but, why would you want to?


Update: Resolved (mostly) - The reason this was so tricky was because Windows had flagged the ESP as hidden, so WSL was getting Access denied errors. Due to a MBR2GPT conversion I believe. Changing this wasn't straight forward, if anyone is curious I'll explain if you can't find your own solution.

I've added the final steps to achieve the windows half of this setup below, I'll omit the procedure to run WSL, it's easy (hint: search Linux in windows store).

My proposed answer requires UEFI and GPT boot, more common these days. The way I avoided the problem of Windows being unaware of Linux partitions was inspired by the way Windows handles booting.

The traditional way of booting in MBR was to hand off to /boot/grub (stage 2) and use the extra space to do things the MBR couldn't. The EFI partition is essentially a giant MBR on any disk you care to put it. The eureka moment was when I noticed that Windows basically put the stage 2 of Windows boot manager on EFI partition in the same directory as bootmgr.efi and bootmgfw.efi. So why not put Grub stage 2 on there too?

TL;DR

Step-by-step explaining how this answers the OP.

  1. ESP (EFI System Partition) is formatted FAT32 - perfect for cross-platform!

  2. On Linux, mkdir /boot/efi and then mount ESP on this;

    grub-install --boot-directory=boot/efi/EFI/grub /dev/nvme0n1
    

    and so forth. (Hopefully I'm not oversimplifying.)

  3. reiterating the solution proposed by others here to the main issue

    GRUB_DEFAULT=saved
    GRUB_SAVEDEFAULT=true
    
  4. The master reveal - #Grub Environment Block - grub/grubenv is on a FAT32 partition and is trivial to modify in Linux:

    grub-editenv /boot/efi/EFI/grub/grubenv set next_entry=X
    

    where X is the menuentry numbered from 0.

  5. Ok the commands are:

    mountvol b: /S
    wsl sudo mount -t drvfs b: /mnt/b
    wsl sudo grub-editenv /mnt/b/EFI/grub/grubenv set next_entry=0
    wsl sudo umount /mnt/b
    mountvol b: /D
    

See SU link at the bottom re: script

The final issue is I'm too tired to write a script to do this right now, the wsl commands don't work separately, the state doesn't carry across. I'm not a coder, I just need some time to work out how these three Linux commands can be strung together. This is enough to satisfy the "does this answer work" requirements for now. I'll update with a final version soon. Eg. You must launch the WSL terminal to use this solution at present.

I'll update this question to ask for help writing the batch script - How to script in Windows Subsystem for Linux (WSL) and call it from a batch file?


I don't know how to do this from Windows, but you can do most of what you described with a GUI in Ubuntu.

This is how you can avoid having restart Windows Always Boot to Ubuntu

Perform the steps below. After those steps restarting windows will always boot to windows automatically. Restarting Ubuntu will always reboot to ubuntu automatically. You won't have to edit every time. Just configure Grub to remember the last choice. The last choice will always be the automatic default with not further manual changes each time.

Install the GUI Grub Customizer.

$ sudo add-apt-repository ppa:danielrichter2007/grub-customizer
$ sudo apt-get update
$ sudo apt-get install grub-customizer

From the GUI you can choose which Boot will be on top by selecting it then click on the Up/Down arrows to position how it's listed.

Under the general tab you can further customize Grub's behavior. You can also click on Advance to change enable, disable, add, remove, or change the lines of the grub file.

The temporary booting to Windows part can be achieved by:

1) Select the GRUB_DEFAULT entry -> Click the Value Column.  Replace the "0"
   with "saved"
2) Add an entry named "GRUB_SAVEDDEFAULT" -> Set the value to "true"
3) Now click Close -> Save (When you click save Grub will be updated to your new
   configuration)

Now after Windows is rebooted it will not go back to Ubuntu

Now when you are performing Windows Updates and other Maintenance the system will always boot to Windows (once you have booted Windows) until you Choose Ubuntu to go back into.