Grub 2 Tips & Tricks

Jump to: navigation, search


Grub v2 is a radical departure fron grub v1 (aka "grub legacy"); at first it seems daunting, especially if you've spent years working with lilo and grub v1 and their simplistic, single-file index based configuration. These are some of the frequent tasks encountered when working with the new grub v2 environment; be sure to refer to the References links for more detailed and in-depth information. Grub2 is used in RHEL / CentOS 7 and above, Fedora 19 and above, and Ubuntu 14 and above.

Default Menu Entry

Choose the default boot target via the GRUB_DEFAULT setting in /etc/default/grub:

  • GRUB_DEFAULT=n : specify the nth entry in /boot/grub2/grub.cfg - first entry is 0
  • GRUB_DEFAULT="string" : choose an entry by name
  • GRUB_DEFAULT=saved : use the boot target specified in /boot/grub/grubenv

Vendor Defaults

Vendors are using different out of the box defaults for the kernel to boot:

Distro Default

Note that Ubuntu by default is using the 0-index method to boot a kernel; in real terms, this means it always tries to boot the top-level entry named "Ubuntu" that has no discrete kernel name listed. You must first set GRUB_DEFAULT=saved in /etc/default/grub and run update-grub to convert it over to using named kernels as outlined below. Failure to do these steps will result in an outcome you did not expect.

First, make sure that your setup is using a special setting in /etc/default/grub - add or replace as needed:


Rebuild your grub config file after making a backup. You will notice we create a backup in the boot directory as the Grub2 bootloader has the capability to use an alternate backup file! So if your changes end up with a mistake, at a "grub>" menu you can tell it to boot your backup file out of the /boot directory. This provides an emergency rollback scenario should things go south during the reboot; see the "Use Backup Config" section in this wiki.

# cp -a /boot/grub2/grub.cfg{,.bak}
# grub2-mkconfig -o /boot/grub2/grub.cfg

# cp -a /boot/grub/grub.cfg{,.bak}
# update-grub

Get a list of the boot menu items with a simple grep - note that RHEL style systems use a flat top-level menu, whereas Ubuntu uses a top-level single menu "Ubuntu" with several submenus of named kernels; this is just a different implementation by the vendors, but it does require a slightly different grep to find the menu entry names.

# grep "^menuentry" /boot/grub2/grub.cfg | cut -d "'" -f2

Red Hat Enterprise Linux Server (3.10.0-229.1.2.el7.x86_64) 7.1 (Maipo)
Red Hat Enterprise Linux Server (3.10.0-229.1.2.el7.x86_64) 7.1 (Maipo) with debugging
Red Hat Enterprise Linux Server (3.10.0-229.4.2.el7.x86_64) 7.1 (Maipo)
Red Hat Enterprise Linux Server (3.10.0-229.4.2.el7.x86_64) 7.1 (Maipo) with debugging
Red Hat Enterprise Linux Server 7.1 (Maipo), with Linux 3.10.0-229.el7.x86_64
Red Hat Enterprise Linux Server 7.1 (Maipo), with Linux 0-rescue-2c9acae4aae44399a33ff8405cdfda12

# egrep "^[[:space:]]?(submenu|menuentry)" /boot/grub/grub.cfg | cut -d "'" -f2

Advanced options for Ubuntu
Ubuntu, with Linux 4.4.0-75-generic
Ubuntu, with Linux 4.4.0-72-generic
Ubuntu, with Linux 4.4.0-43-generic

Ubuntu uses submenus. This means that the line above "Advanced options for Ubuntu" is the top level item, then the lines below it are children; visually: Ubuntu Advanced options for Ubuntu Ubuntu, with Linux 4.4.0-75-generic Ubuntu, with Linux 4.4.0-72-generic Ubuntu, with Linux 4.4.0-43-generic Below you will prepend "Advanced options for Ubuntu", a '>' symbol, then the name to make it work correctly.

Use one of these lines to set the default with grub2-set-default / grub-set-default - a 0-based index can be used instead of a name, however on RHEL grubby uses the long name so stick to that. grub(2)-set-default is just a fancy shell script that runs grub(2)-editenv to unset old entries and set the saved_entry with your new choice. It updates the /boot/grub(2)/grubenv file:

# grub2-set-default "Red Hat Enterprise Linux Server (3.10.0-229.1.2.el7.x86_64) 7.1 (Maipo)"

# grub2-editenv list
saved_entry=Red Hat Enterprise Linux Server (3.10.0-229.1.2.el7.x86_64) 7.1 (Maipo) 

# grep saved_entry /boot/grub2/grubenv 
saved_entry=Red Hat Enterprise Linux Server (3.10.0-229.1.2.el7.x86_64) 7.1 (Maipo)


# grub-set-default 'Advanced options for Ubuntu>Ubuntu, with Linux 4.4.0-72-generic'

# grub-editenv list
saved_entry=Advanced options for Ubuntu>Ubuntu, with Linux 4.4.0-72-generic

# grep saved_entry /boot/grub2/grubenv 
saved_entry=Advanced options for Ubuntu>Ubuntu, with Linux 4.4.0-72-generic

RHEL Kernels

A note about how Red Hat builds their kernel packages; in the above examples grub2-mkconfig is used to build new config files; be aware that the RHEL kernel package installs do not use it. Instead, they use a shim /sbin/new-kernel-pkg which in turn uses a program called grubby. The critical differences:

  • grubby does not use /etc/default/grub – it uses the as-booted kernel parameters to build a new commandline for the new kernel. If you have custom kernel arguments, be sure to first update /etc/default/grub, use grub2-mkconfig, reboot to activate the new options then upgrade the new kernel package. This is most commonly done on a freshly installed server before a yum update is issued.
  • the grubby menu entries are different – the Title used in the menu entries is of a different text string than the one produced by grub2-mkconfig; this means you should always check and if required set the default kernel to boot when manipulating the grub config file. A quick example:
    # Build the new config
    grub2-mkconfig -o /boot/grub2/grub.cfg
    # Get the name of the one you want, let's call it "Red Hat FOOBAR" here
    grep "^menuentry" /boot/grub2/grub.cfg | cut -d "'" -f2
    # Update the grub environment file /boot/grub2/grubenv saved_entry
    grub2-set-default "Red Hat FOOBAR"
    grub2-editenv list

The Red Hat kernel packages will automatically reconfigure the default kernel to boot as part of the grubby process; if simply doing a standard kernel package upgrade or downgrade the environment file will be updated to that package version.

Simple Text Mode

This is handy for servers - set up grub2 to use basic text mode and show all kernels by default:

# vi /etc/default/grub


Comment this out if present:

# cp -a /boot/grub2/grub.cfg{,.bak}
# grub2-mkconfig -o /boot/grub2/grub.cfg

Boot Options

The config variable GRUB_CMDLINE_LINUX will be applied to all the auto-detected linux kernels when you rebuild the config; typically the config already has some options, just add to the end as needed. Some common options might be disabling IPv6, setting the LANG variable or blacklisting initrd modules (such as SAN/DAS HBAs).

# vi /etc/default/grub

GRUB_CMDLINE_LINUX="crashkernel=auto net.ifnames=0 biosdevname=0 rdblacklist=bfa nomodeset"

# cp -a /boot/grub2/grub.cfg{,.bak}
# grub2-mkconfig -o /boot/grub2/grub.cfg

Disable Recovery Menus

You may wish to disable the "(recovery mode)" (single-user mode) menu entries by setting GRUB_DISABLE_RECOVERY and rebuilding your config. In reality all the menu entry does is add the boot option single so there's no real value in cluttering up your grub menus.

# vi /etc/default/grub


# cp -a /boot/grub2/grub.cfg{,.bak}
# grub2-mkconfig -o /boot/grub2/grub.cfg

Use Backup Config

It's possible something might go wrong during grub2-mkconfig and leave you with a corrupt config file, but you won't notice it until you've rebooted. Typically grub will drop you to the shell with an error (or just empty menus). If that happens and you've made a backup use the configfile command to use it - as soon as the command is issued the menu should appear.

grub2> ls
(hd0) (hd0,msdos3) (hd0,msdos2) (hd0,msdos1)
grub2> configfile (hd0,1)/boot/grub2/grub.cfg.bak