On Ubuntu 22.04, and possibly other distros & versions, you can configure grub
to remember the last selected entry made at boot time, and use this as the default in subsequent boots.
To enable this, edit /etc/default/grub
and add the following lines:
GRUB_SAVEDEFAULT=true
GRUB_DEFAULT=saved
Then run sudo update-grub
to regenerate the boot files, and then reboot.
Now in grub
, when you select a kernel, e.g. from the "Advanced options for Ubuntu" submenu, grub
will automatically remember the selection for the next time, and then boot the selected kernel.
This selection is stored in /boot/grub/grubenv
, and it should not be edited directly. If you need to modify it from a booted system, you can use sudo grub-editenv
:
$ sudo grub-editenv list
saved_entry=gnulinux-advanced-20795ce5-2ede-4ea5-994d-c665c5037760>gnulinux-5.15.0-76-generic-advanced-20795ce5-2ede-4ea5-994d-c665c5037760
$ sudo grub-editenv set 'saved_entry=...'
The entry names can be copy/pasted from /boot/grub/grub.cfg
, and note in particular the submenu's menuentry_od_option
, followed by >
, followed by the menuentry_id_option
value of the selected item. Use quotes around the set string to prevent the shell seeing >
as a redirect.
For example, this:
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-20795ce5-2ede-4ea5-994d-c665c5037760' {
menuentry 'Ubuntu, with Linux 5.19.0-46-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.19.0-46-generic-advanced-20795ce5-2ede-4ea5-994d-c665c5037760' {
...
Would result in the command:
$ sudo grub-editenv set 'saved_entry=gnulinux-advanced-20795ce5-2ede-4ea5-994d-c665c5037760>gnulinux-5.19.0-46-generic-advanced-20795ce5-2ede-4ea5-994d-c665c5037760'
The net effect of this feature is that grub
, if left to boot automatically, will always pick the last selected boot option, which is a kind of default behaviour. Just be aware that temporarily selecting another kernel will change the default, but you can disable this by removing GRUB_SAVEDEFAULT
from /etc/default/grub
and regenerating the grub files with sudo update-grub
, once you have your default kernel saved.