Linux – Make grub keep its default boot under kernel updates

grubkernellinuxUbuntu

I dual-boot my machine and I want it to default to boot into windows so that whenever I restart the machine remotely from my home it will be able to get back into Windows (instead of Ubuntu).

The problem is that every time Ubuntu upgrades the kernel, I have to reset the default boot item of grub back to windows. This is because the grub menu loader uses positions i.e. 6 for default OS to boot. And when Ubuntu installs a new kernel it changes that order.

I am looking for a way to configure grub to remember its default boot item under kernel updates.

Best Answer

Grub 1 (Ubuntu 9.04 and earlier)

I find the easiest way to do this is to move the Windows boot entry above the Ubuntu entries in /boot/grub/menu.lst. By default, the Grub configuration file is laid out like this:

  1. basic options
  2. debian auto-magic kernel config (including utilities like memtest86+ by default)
  3. other detected OS's

Section 2 is demarcated by these lines in the config file:

### BEGIN AUTOMAGIC KERNELS LIST
(lots of stuff here)
### END DEBIAN AUTOMAGIC KERNELS LIST

Entries for autodetected OS's (eg, Windows) are placed after this section. When you install a new kernel, section 2 is the only section that gets changed. So moving the autodetected OS to somewhere before the BEGIN line will place it where the entry number won't change when a new kernel gets installed.


Grub 2 (Ubuntu 9.10 and later)

With Grub2, the entire configuration file is created anew anytime update-grub is run, so editing /boot/grub/grub.cfg won't be a permament fix. How that file is generated is handled by the files /etc/default/grub and the scripts in /etc/grub.d/*. So we modify the configuration by modifing the files which control individual sections of the configfile, then running update-grub again.

My /etc/grub.d includes these files:

  • 00_header
  • 05_debian_theme
  • 10_hurd
  • 10_linux
  • 20_memtest86+
  • 21_memtest86
  • 30_os-prober
  • 40_custom

These are shellscripts that get run in order to generate the config file. The Windows boot entry is generated by 30_os-prober, which is run after the linux boot entries are generated in 10_linux. So just rename 30_os-prober to something less than 10, and rerun update-grub

mv /etc/grub.d/30_os-prober /etc/grub.d/09_os-prober
update-grub

Now your Windows entry should be the first entry, and you can set that as the default boot entry (GRUB_DEFAULT=) in /etc/default/grub.

Related Question