Now I'm not sure if this is on topic or not, but if not, I have no idea where to post it, superuser does not seem appropriate because this relates to grub, but I regress.
When I restart Windows it boots to grub, and then the default grub option is Ubuntu, causing some very annoying times trying to install updates, so what I want to do is be able to edit the grub boot configuration temporarily to put Windows at the top after a restart, but after Windows was booted, it would go back to Ubuntu.
How would I do this?
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: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 asbootmgr.efi
andbootmgfw.efi
. So why not put Grub stage 2 on there too?TL;DR
Step-by-step explaining how this answers the OP.
ESP (EFI System Partition) is formatted FAT32 - perfect for cross-platform!
On Linux,
mkdir /boot/efi
and then mount ESP on this;and so forth. (Hopefully I'm not oversimplifying.)
reiterating the solution proposed by others here to the main issue
The master reveal - #Grub Environment Block -
grub/grubenv
is on a FAT32 partition and is trivial to modify in Linux:where
X
is themenuentry
numbered from 0.Ok the commands are:
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.
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:
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.
While editing a Windows partition content from Linux is doable, the other way around is not.
Windows doesn't natively support Linux FSs. A few tools can allow you to do it, though.But those wouldn't help.
I haven't test it myself, but it seems that recently someone ported GRUB2 to Windows 10, 8, 7, Vista and XP, both in BIOS and in EFI mode.
I suspect that, even in case everything works as expected it would'n be as easy as drinking a glass of water.