GliderJan Varho

Reboot to Another Kernel (or Windows) with grub2

  • grub
  • grub2
  • Ubuntu
  • kernel
  • Windows

Update: Updated below for 11.04/Natty.

So I had some trouble getting grub-reboot to work, since it only seems to like numbers, but I figured it out. I made a script to reboot to another kernel or OS (Windows for me) once, leaving the default unchanged.

First one must set GRUB_DEFAULT=saved in /etc/default/grub and run update-grub and grub-set-default to set the default kernel (usually '0' for the most recent Linux kernel).

Next, here's the script to immediately reboot to another menu entry from those normally displayed on reboot. It shows a list of available entries and asks which to reboot to. It's for Ubuntu, but may work with other sudo-using distros.


# Read lines as fields IFS=" "

# Find the grub entries hack select opt in `grep menuentry /boot/grub/grub.cfg | sed "s/[^\"']*[\"']\([^\"']*\).*/\1/"` do # Numbering starts with 0 sudo grub-reboot $((REPLY - 1)) # Remove the next to only set the kernel, not actually reboot sudo reboot break done</code>

Running it from a terminal reboots to the chosen kernel or OS, while leaving the default as it was.

Unfortunately grub seems to sometimes discard the old default if one reboots twice in a row to the secondary OS, but I haven't been able to debug enough to report it yet.

Update: In Ubuntu 11.04 a grub2 feature called submenus is enabled by default and breaks the script. Editing /etc/grub.d/10_linux and commenting out the lines regarding submenus is a workaround.