Home / Alt manpages / grub-set-default(8)

  • grub-set-default(8)
  • Admin command
  • linux

Set GRUB's saved default entry without losing the old choice

You will configure GRUB to honour a saved menu entry, set that entry with grub-set-default, and check the environment block afterwards. Allow 15 minutes if you already know the menu entry you want. The command changes persistent boot selection, so keep a recovery value before you run it and schedule a reboot only when you can reach the machine's console.

This guide covers the installed Ubuntu package grub2-common 2.12-1ubuntu7.3. The examples use the GRUB files under /boot. A machine with a separate boot mount or a non-standard layout needs the corresponding paths and a deliberate test first.

1. Check the command and current configuration

Run the inspection commands as your normal user. They do not change the boot configuration:

$ command -v grub-set-default
/usr/sbin/grub-set-default
$ grub-set-default --version
grub-set-default (GRUB) 2.12-1ubuntu7.3
$ grep '^GRUB_DEFAULT=' /etc/default/grub
GRUB_DEFAULT=0

The local manual accepts a menu-entry number, title or identifier. A number is counted from zero. The important configuration boundary is GRUB_DEFAULT=saved: without that setting, writing a saved entry does not make the generated GRUB menu use it as its normal default.

Checkpoint: if the last command prints GRUB_DEFAULT=0 or another fixed value, do not assume that grub-set-default alone will alter the next normal boot. Continue to the configuration step below.

2. Record the current saved value

The saved value lives in GRUB's environment block, normally /boot/grub/grubenv. Read it before changing anything:

$ sudo grub-editenv list
saved_entry=old-entry-id

The value is host-specific. It may be a number, a menu title or an identifier, and an empty result can simply mean that no saved entry is currently recorded. Copy the exact saved_entry value somewhere safe if one is shown. That is the quickest undo value if the new entry is unsuitable.

If grub-editenv reports that the environment block cannot be read or updated, stop here. Do not create a replacement file by hand. GRUB's environment block is subject to bootloader and filesystem limitations, and a failed write needs to be investigated against this machine's layout.

3. Make the generated menu use saved entries

Back up the configuration file, then edit only the GRUB_DEFAULT assignment. The backup is a recovery aid, not a substitute for regenerating the menu:

$ sudo cp --preserve=all /etc/default/grub /etc/default/grub.before-saved-entry
$ sudoedit /etc/default/grub

Set this line exactly:

GRUB_DEFAULT=saved

Do not add GRUB_SAVEDEFAULT merely to make grub-set-default work. The command's documented prerequisite is GRUB_DEFAULT=saved. Save the file, then confirm the assignment and regenerate the generated configuration:

$ grep '^GRUB_DEFAULT=' /etc/default/grub
GRUB_DEFAULT=saved
$ sudo grub-mkconfig -o /boot/grub/grub.cfg

Read the output from grub-mkconfig. It should identify the configuration scripts it runs and finish without an error. This command updates /boot/grub/grub.cfg; it does not reboot the machine.

Checkpoint: if the file still contains GRUB_DEFAULT=0, or if regeneration failed, fix that before selecting a new entry. Do not treat a successful text edit as proof that GRUB will use it.

4. Identify the entry you intend to select

Use the GRUB menu at boot to identify a title or identifier, or use your distribution's generated configuration as a reference. Prefer a stable menu identifier when you have one. Titles can change with kernel versions, localisation or device details.

A numeric entry is simpler but fragile: the first menu item is 0, the second is 1, and a kernel update can change the ordering. Do not copy the examples below without replacing the placeholder with an entry that exists on your machine.

# Example values only. Replace one with an exact local entry.
MENU_ENTRY='2'
# or:
MENU_ENTRY='example-gnu-linux'

Shell variables are ordinary user-space preparation. They do not affect GRUB until the command in the next step runs.

5. Set the saved default

This is the state-changing step. It writes the saved default in the GRUB environment block, so use elevated privileges and keep the old value available:

$ sudo grub-set-default "$MENU_ENTRY"

On success, the command normally prints nothing and returns status zero. Check both the status and the value that was written:

$ printf 'exit status: %s\n' "$?"
exit status: 0
$ sudo grub-editenv list
saved_entry=example-gnu-linux

The displayed value must match the entry you selected. If it does not, stop before rebooting. A successful write proves that the environment block was updated, not that the named entry will boot correctly.

6. Use the alternate boot directory only when required

The installed command has one operational path option, --boot-directory=DIR. It tells the command to expect GRUB images under DIR/grub instead of /boot/grub. Use it when the target system's boot directory is mounted elsewhere, and verify the path first:

$ sudo grub-set-default --boot-directory=/mnt/target-boot "$MENU_ENTRY"

This does not mean "use a different root filesystem". The option's value is the boot directory itself. Do not add it to a normal installation just because the command is being run from a rescue shell. In a rescue environment, mount the target boot filesystem at the path you intend to pass, confirm that DIR/grub is the target GRUB directory, and then verify it with grub-editenv using the matching environment block.

7. Recover before rebooting if the choice is wrong

To restore the previous choice, run the same command with the exact value recorded in step 2:

$ sudo grub-set-default 'old-entry-id'
$ sudo grub-editenv list
saved_entry=old-entry-id

If there was no previous saved_entry, a common temporary recovery choice is the first menu item:

$ sudo grub-set-default 0

That selects entry zero only after the generated configuration is using saved defaults. It is not a guarantee that entry zero is a known-good operating system. If grub-mkconfig introduced an unrelated configuration problem, restore the backup and regenerate the menu:

$ sudo cp --preserve=all /etc/default/grub.before-saved-entry /etc/default/grub
$ sudo grub-mkconfig -o /boot/grub/grub.cfg

Do not remove the backup until you have completed a controlled boot. Rebooting is the only part of this procedure that interrupts the running system, and it should be planned separately from changing the saved value.

Done means

  • grub2-common and grub-set-default were checked on the target host.
  • The old saved_entry value, or its absence, was recorded before the change.
  • /etc/default/grub contains GRUB_DEFAULT=saved and grub-mkconfig completed successfully.
  • The selected number, title or identifier is an exact entry from this machine's GRUB menu.
  • grub-editenv list shows the intended saved_entry.
  • You have a tested recovery value and have not rebooted until console access and rollback are available.