Skip to content

Instantly share code, notes, and snippets.

@zargony
Last active December 11, 2025 16:27
Show Gist options
  • Select an option

  • Save zargony/dd0fc80269369fb602cfe291eca8e2fe to your computer and use it in GitHub Desktop.

Select an option

Save zargony/dd0fc80269369fb602cfe291eca8e2fe to your computer and use it in GitHub Desktop.
GRUB bootloader workaround to select OS without a keyboard
#! /bin/sh
set -e
# Install this script to /etc/grub.d/01_switch-default, chmod +x it and
# run update-grub2.
cat << EOF
# Switch default boot entry when rebooting while GRUB menu is shown. This
# workaround allows to select the OS without a keyboard attached (while GRUB
# menu is shown: wait to boot current selection, reboot to select a different
# entry)
#
# Use in conjunction with GRUB_DEFAULT=saved, GRUB_SAVEDEFAULT=true and
# GRUB_TIMEOUT=x in /etc/default/grub.
#
# Info/updates: https://gist.github.com/zargony/dd0fc80269369fb602cfe291eca8e2fe
#
# How it works: switches the default boot entry when GRUB runs on power-up. If
# the GRUB menu timeout is interrupted by rebooting, next time the new default
# will be active. If the GRUB menu timeout boots the selected entry, GRUB will
# save it as the default for next time.
#
if [ "\${default}" = "\${saved_entry}" ] ; then
# Switch default entry on next boot between 0 (Ubuntu) and 4 (Windows). You
# may want to adjust this to your needs.
if [ "\${saved_entry}" = 0 ] ; then
set saved_entry=4
#elif [ "\${saved_entry}" = 4 ] ; then
# set saved_entry=5
else
set saved_entry=0
fi
save_env saved_entry
fi
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment