Can Grub be configured to remember the last OS you booted into?

13,397

Solution 1

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.

Solution 2

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:
13,397
cukabeka
Author by

cukabeka

Updated on September 17, 2022

Comments

  • cukabeka
    cukabeka over 1 year

    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.

  • cukabeka
    cukabeka about 14 years
    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.
  • quack quixote
    quack quixote about 14 years
    @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
    quack quixote about 14 years
    @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.)