5

I'm using both a Bluetooth mouse and keyboard, and frankly it's really annoying having to go grab a physical keyboard in order to boot into another kernel version or into Windows on another partition. Is there a way for me to tell GRUB that when it reboots, to boot directly into a specific kernel or Windows?

Is there a command I can run along the lines of grub-reboot "Windows 7" which would cause my machine to reboot and when starting up, boot into the specified OS or kernel?

Naftuli Kay
  • 39,676

2 Answers2

7

(I'm adapting this answer from https://unix.stackexchange.com/a/11431/73, because it happens to work the same even though the question is different)

First you need to do some prep work in /boot/grub/grub.conf. Change default to saved. In every OS block, add savedefault 0, where 0 is the index of whichever kernel you want it to default to unless told otherwise. Write the value 0 to the file /boot/grub/default, so GRUB will know which default to use on your next reboot (before any of those savedefault directives have executed).

Now at any time before you reboot, you can change the value in /boot/grub/default to change what the default will be on the next boot. If you set it to 1, the second entry in the GRUB list will be the default. As soon as it starts booting, GRUB will execute the savedefault 0 directive and change the default back to 0, so on the subsequent boot you'll switch back to defaulting to 0.

To change the default, you can use grub-set-default, which takes the index number and writes it to that file. So, make an alias/script that does:

grub-set-default 1
shutdown -hr now

And when you run it, GRUB will reboot into the second entry in the list. The next time you reboot, it'll switch back to the first (or whichever you specified)

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
  • 4
    In OSs I use (Debian/Ubuntu-like) /boot/grub/ gets overwritten by Grub scripts running over the user configuration files in /etc/grub and /etc/default/grub. This is triggered often on updates and would make changes non-persistent. – gertvdijk Dec 06 '12 at 23:14
1

In CentOS 7 and probably in other distributions as well, you can use grub2-reboot to select a kernel only for the next reboot, without changing the defaults.

List available kernels:

awk -F\' /^menuentry/{print\$2} /etc/grub2.cfg

Select the one you want by number (zero-based). For example to reboot into the second system listed (number "1"):

grub2-reboot 1 && reboot
mivk
  • 3,596