Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ekawahyu/f8b6d7c3c840117e43e08fa85585a1ae to your computer and use it in GitHub Desktop.
Save ekawahyu/f8b6d7c3c840117e43e08fa85585a1ae to your computer and use it in GitHub Desktop.
How to switch GRUB on headless machine

Description

Typical GRUB boot menu looks like the following:

+-----------------------------+
|*Debian GNU/Linux            |
| Devuan GNU/Linux            |
| Other ...                   |
|                             |
+-----------------------------+
The highlighted entry will be executed automatically in Xs

In this example, Debian GNU/Linux is on index #1, Devuan GNU/Linux is on index #2, Other ... is on index #3, and so on. The star ( * ) preceeding Debian GNU/Linux entry means that it's the default selection. You can use arrow keys on keyboard to navigate the menu and press ENTER to boot the selection. If you don't select anything, the default selection gets executed automatically in X seconds.

For headless setup scenario, you mostly access this machine over SSH. You can't see GRUB menu over SSH. This gist guides you to setup your machine so that you can switch between OSes remotely over command line.

This method is only tested to switch between Linux OSes, it may or may not work with Windows. Your mileage may vary and make sure you know what you are doing with GRUB.

Modify GRUB Default Setting

When Linux machine boots, Debian GNU/Linux (default, highlighted, with * ) for example. Edit etc/default/grub and add the following lines:

GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true

then, run:

$ update-grub

and reboot the machine.

What we did just now is to activate GRUB feature to remember and save the last OS selection. Try to boot different OSes and notice where GRUB highlights your last booted OS.

How to Temporary Switch OS

Boot into Debian GNU/Linux (default, highlighted, with * ). On terminal, do cat /boot/grub/grub.cfg. You should see something similar to the following:

menuentry 'Debian GNU/Linux' {
    set root=(hd0,1)
    linux /boot/vmlinuz root=/dev/sda1 quiet
    initrd /boot/initrd.img
}

menuentry 'Devuan GNU/Linux' {
    set root=(hd0,2)
    linux /boot/vmlinuz root=/dev/sda2 quiet
    initrd /boot/initrd.img
}

menuentry 'Oher ...' {
    ...
}

Let's boot into Devuan (index #2) just once:

$ grub-reboot 2

then, reboot the machine. Notice that it switches into Devuan GNU/Linux. Once you are done with Devuan and reboot the machine, it will automatically enter Debian GNU/Linux as default selection.

How to Permanently Switch OS

Let's say you want to boot Devuan GNU/Linux permanently, we make it as default OS selection:

$ grub-set-default 2
$ grub-reboot 2

then, reboot the machine. Notice that it switches into Devuan GNU/Linux. Once you are done with Devuan and reboot the machine, it stays with Devuan GNU/Linux as default selection.

Troubleshooting

Use the following command for troubleshooting in case your machine boots to the wrong index:

$ grub-editenv list
next_entry = x
saved_entry = y

Make sure you select the correct index, it is relative to active OS you are in when issue GRUB command. The next_entry is vaild only for the next reboot. The saved_entry points to default entry.

You can confirm index by looking into /boot/grub/grub.cfg and look at the menuentry property:

$ cat /boot/grub/grub.cfg | grep menuentry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment