I typically have 2-3 OSes in my boot menu:
- Ubuntu 10.04
- Windows 7
- [Sometimes, a third option, such as Ubuntu 10.04 Netbook]
If I am in an OS other than the default choice (Windows 7 in this case), and I reboot, Grub boots into Ubuntu 10.04. This shouldn’t happen–when I reboot in Windows 7, I want to stay in Windows 7. Is there any way to configure Grub to remember my last choice, and boot into that?
Alternatively, it would be even better if Grub only did this when I rebooted (not when I first turn the computer on). I realize this may not be possible, so I’d be happy with simple last-choice-remembering.
Solution:
In the original Grub you’d do this by setting, in /boot/grub/menu.lst, the default keyword to “saved” and then using the savedefault keyword in each boot menu entry.
Updated:
Grub2 is a little more complicated. It’s also still evolving, so even though Ubuntu 9.10 and 10.04 both use Grub2, there are enough differences in how things are packaged and configured to make things interesting.
Here’s how to do the savedefault trick in both:
Ubuntu 10.04:
The 9.10 directions are a trick; Lucid is much simpler. See Ubuntu’s help for more gory details. You need to make two changes in /etc/default/grub:Change the line that reads
GRUB_DEFAULT=0
toGRUB_DEFAULT=saved
Add a line with
GRUB_SAVEDEFAULT=true
That’s it. Actually isn’t that bad, is it? Don’t forget to run
sudo update-grub
.Ubuntu 9.10:
There are several hacks on the Ubuntu forums for getting this to work; apparently the savedefault keyword was buggy in Grub v1.97. This post on Ubuntu Forums provides one method. Another post suggests a slightly simpler method:Set GRUB_DEFAULT=saved in /etc/default/grub.
Put these two lines into /etc/grub.d/40_custom (near the bottom, after the “exec tail” line):
saved_entry=${chosen}save_env saved_entry
I’ve tested this method in Ubuntu 9.10 and it works; the next boot will automatically highlight the last entry chosen. Combined with a timeout, this will automatically boot into the last OS you booted.
And don’t forget to run sudo update-grub
. After you’re finished with your changes. It’s important and embarrassingly easy to forget….
Grub has no way to differentiate a reboot from a cold boot, so Grub can’t do this on reboot by itself. However, if you’re booted into Linux, you can use sudo grub-set-default N
(where N is the number of a boot menu entry, starting from 0) to set entry N to be chosen at next boot (instead of whatever entry was saved).
In theory, you could hack together something that hooked into the shutdown routines (an Upstart script, perhaps) that ran grub-set-default for you when you told Linux to shutdown (but not when you told it to reboot). You wouldn’t get the same functionality when shutting down Windows, however.
I did hear about a Grub4DOS or Grub1 technique that involved installing Grub to a FAT32 or NTFS /boot partition — that would allow you to also run grub-set-default from Windows, so you could call it from a Windows logoff script. Last time I saw anything about that, this was not possible on Grub2. That may have changed by now.