How to remotely control which OS Grub boots?
We can reboot to a given Grub menu boot entry using grub-reboot. This can also be done from a remote session e.g. via SSH.
To boot into another OS choose the position it is represented in the Grub menu starting from 0
:
sudo grub-reboot <num>
In this case my Windows is on position 4
. So when issueing
sudo grub-reboot 4 && sudo reboot
The machine will reboot to Windows after the timeout we defined in the Grub settings.
After rebooting from Windows the machine will boot into the default OS. This can be defined with
sudo grub-set-default <num>
To make this work we may have to define GRUB_DEFAULT=saved
in our /etc/default/grub
but on my system it also worked with the default settings.
Sadly rebooting Windows from Windows can not be done by this.
Another idea thanks to takkat's answer:
- Default boot is Ubuntu
grub-reboot N
whereN
is the Windows boot entry- Rebooting Windows will bring you back to default boot: Ubuntu.
Disadvantage: to reboot Windows you have to reboot twice: Reboot Windows, let Ubuntu boot and grub-reboot N
again! :-(
You can set the default OS with grub depending on time, see Can GRUB be scheduled? This means: changing default 'entry' (auto login) at defined periods of time automatically?. So, for example, you can configure grub to boot to Windows when $MINUTE is odd and boot to Linux when $MINUTE is even.
insmod datehook
if [ $MINUTE -eq 0 -o $MINUTE -eq 2 -o $MINUTE -eq 4 -o $MINUTE -eq 6 -o $MINUTE -eq 8]; then
set default="Ubuntu"
else
set default="Windows"
fi
If you want to a specific OS ; just wait until its time comes.
Using this method should take into account the time it takes to reach Grub. Using time frame of 5 minutes is probably more reliable.
I think it's also possible to alternate between the boot entries. Something like :
if [ $default = "Ubuntu" ]; then
set default="Windows"
else
set default="Ubuntu"
fi
when grub is configured to remember the last OS booted (How to get grub2 to remember last choice?)
If your PC doesn't boot into the OS you want, just reboot.