How to Set Default Entry in Grub2 and Grub

Posted on In Linux

Linux booting is usually controlled by Grub or the new Grub2. Setting the default booting entry is a frequent operations. Here, we introduce how to set the default entry in Grub2 and Grub.

Setting the default booting entry in grub2

Note1: With some version of grub2, the grub2-set-default method and the script below may not work. You can check the “Setting default boot entry for grub2 in /etc/default/grub” method below.

The quick method: download the grub2-select.bash and run

# wget https://www.systutorials.com/go/grub2-select.bash/ -O grub2-select.bash && \
bash grub2-select.bash

and choose the boot entry.

To view the script, please check it on github.

Following is the manual way and what the grub2-select.bash script does.

First of all, find out whether your system is in UEFI or BIOS mode and use different config files accordingly:

if [ -d /sys/firmware/efi ]; then
    grubcfg="/etc/grub2-efi.cfg"
else
    grubcfg="/etc/grub2.cfg"
fi

List all the entries in the grub.cfg by:

# grep ^menuentry $grubcfg | cut -d "'" -f2

Select the default entry by the entry’s name.

For example, the command to set the default entry to be ‘Fedora, with Xen hypervisor’ is as follows.

# grub2-set-default 'Fedora, with Xen hypervisor'

Regenerate the Grub2 config file by:

# grub2-mkconfig -o $grubcfg

We also provide a script in https://www.systutorials.com/how-to-regenerate-grub2-config-file-on-linux/ with which you can directly use to regenerate the Grub2 config file.

Last, verify the default entry is what we just set.

# grub2-editenv list

This should print out what we set.

Setting default boot entry for grub2 in /etc/default/grub

We may also want to change the boot option which is stored in ‘/etc/default/grub’. For example, to set the above entry, change the line for setting GRUB_DEFAULT to

GRUB_DEFAULT="Fedora, with Xen hypervisor"

After editing the file, we can generate the new configuration file for grub2 following https://www.systutorials.com/how-to-regenerate-grub2-config-file-on-linux/.

Setting the default booting entry in grub

The quick method: download the grub-select.bash and run

# bash grub-select.bash

and choose the boot entry.

Following is the manual way.

In grub (before version 2, actually it is usually version 0.99), the default entry is stored in /boot/grub/grub.conf. The default entry to boot is specified by the ‘default’ variable such as

default=2

The ‘2’ here is to set the default entry to be the 2nd (start from 0) one.

Update on Apr. 29, 2015: updated posts with support to both BIOS and UEFI based Linux systems; suggest the grub2-regen-cfg.bash script for re-generating grub2 config file.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

5 comments

  1. Pingback: 配置Diskful Server | 撤退的逃兵
  2. To select a submenu in a menu, e.g. “Fedora, with Linux 3.8.3-103.fc17.x86_64” under “Advanced options for Fedora”:

    # grub2-set-default ‘Advanced options for Fedora>Fedora, with Linux 3.8.3-103.fc17.x86_64’

    Note the ‘>’.

  3. Note that the “grub2-set-default ‘Fedora, with Xen hypervisor'” method may not work for some versions of grub2, you will need to use the ‘/etc/default/grub’ method.

    1. Nice theory. Does not work on my system. ASUS A88XM-A x64-bit Ubuntu MATE 20.04.3 booted by UEFI.

Leave a Reply

Your email address will not be published. Required fields are marked *