Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 19 down vote accepted

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:

    1. Change the line that reads GRUB_DEFAULT=0 to GRUB_DEFAULT=saved

    2. 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:

    1. Set GRUB_DEFAULT=saved in /etc/default/grub.

    2. 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.

share|improve this answer
    
Solution #2 doesn't seem to work for me. I edited /etc/default/grub, edited /etc/grub.d/40_custom, and ran sudo update-grub. I tested by booting into Windows 7, shutting down, then turning my computer back on. Ubuntu 10.04 (the default) was still selected. I also tried putting "saved_entry" in /etc/default/grub, instead of "saved" (not sure how grub config scripts work), but it didn't help. I haven't tried solution #1 yet. –  Matthew May 5 '10 at 17:43
    
@Matthew: double-check that /etc/grub.d/40_custom is executable (sudo chmod +x if it isn't). but yeah, i just tried on my Ubuntu 10.04 machine and it doesn't work there either. checking into why... –  quack quixote May 5 '10 at 17:54
    
@Matthew: OK, figured it out. i'm sorry, i should've known Lucid and Karmic would be that different. (oh. and make sure you run sudo update-grub.) –  quack quixote May 5 '10 at 18:45
    
Yep, works now. Thanks! –  Matthew May 5 '10 at 19:46

This post will be probably be of use to you, it is pretty self-explanatory. Note that 10.04 and 9.10 use the same GRUB version, so things should be exactly the same for you.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.