Linux GRUB legacy and GRUB2 essentials
A simple guide to help you quickly find your way when working on Operating Systems boot topics with GRUB as bootloader, on Debian and Red Hat based Linux distributions.

Introduction
For an introduction to GRUB (overview, history, changes from GRUB legacy, role, features, etc), run the following command on your favorite Linux distribution:
info -f grub -n 'introduction'
Grub legacy
Grub legacy configuration files
For Debian based distributions (Debian, Ubuntu...), the main configuration file is /boot/grub/menu.lst. Red Hat based distributions (RHEL, Centos, Fedora...) use /boot/grub/grub.cfg instead of /boot/grub/menu.lst. The configuration file consists of two sections: global definitions and operating systems boot menu definitions.
#---------------------------------------------------------------------#
# Global definitions (all available configuration directives setting) #
#---------------------------------------------------------------------#
default 0 # Use the first OS boot menu definition by default, to boot the system
# 0 = first menu entry , 1 = second menu entry , etc...
color white/blue yellow/blue # white/blue = foreground/background colors for the normal menu entries ;
# yellow/blue = foreground/background colors for the selected menu entries
timeout 5 # wait 5 seconds after the boot menu entries displays, before using the default
# menu entry to boot the system
fallback 3 # Use the fourth boot menu entry as fallback (in case booting using the default menu entry fails)
# 0 = first menu entry... 3 = fourth menu entry
splashimage path_to_the_image # points to an image to use as background image for the boot menu
hiddenmenu # hide the menu section options
#-------------------------------------------------------------------------------------------------#
# Operating systems boot menu definitions (essential configuration directives, not all available) #
#-------------------------------------------------------------------------------------------------#
title My_Menu_entry_Title
root (hdX,Y) # specify the root device drive and partition
# Examples : (hd0,0) = first partition of the first hard drive device
(hd0,1) = second partition of the first hard drive device
kernel (hdX,Y)/boot/vmlinuz # specify the kernel binary file path
initrd /boot/initrd # specify the initial RAM disk image path. / = root device defined with "root (hdX,Y)"
rootnoverify (hdX,Y) # define the root device drive and partition (/boot) for booting into a non-Linux system like windows
Grub legacy sample configuration file
default 0
timeout 10
color white/blue yellow/blue
title CentOS Linux
root (hd1,0)
kernel (hd1,0)/boot/vmlinuz
initrd /boot/initrd
title Windows
rootnoverify (hd0,0)
See GRUB legacy configuration files section for info about the directives used in this sample GRUB legacy configuration file.
Grub legacy and Grub2 installation
GRUB can be installed on the MBR (Master Boot Record) of an entire disk using the grub-install command followed by the disk device path:
# Linux format
grub-install /dev/sda
# GRUB legacy format
grub-install '(hd0)'
GRUB can also be installed on the boot sector (/boot) of a disk partition instead of the MBR of a hard drive using the grub-install command followed by the destination disk device partition:
# Linux format
grub-install /dev/sda1
# GRUB legacy format
grub-install '(hd0,0)'
Grub2
Grub2 configuration files
For Debian based distributions (Debian, Ubuntu...), the main configuration file is /boot/grub/grub.cfg. Red Hat based distributions (RHEL, Centos, Fedora...) use /boot/grub2/grub.cfg. The /etc/grub.d/ directory contains separate configuration files that are put together to create the main Grub2 configuration file.
To update the main Grub2 configuration file with configurations from the /etc/grub.d directory, the following commands can be used:
# Commands to generate/update the main Grub2 configuration file
# Debian based distributions
# Main Grub2 configuration file: /boot/grub/grub.cfg
update-grub # this is a stub command to the next one:
grub-mkconfig -o /boot/grub/grub.cfg
# Red Hat based distributions
# Main Grub2 configuration file: /boot/grub2/grub.cfg
grub2-mkconfig -o /boot/grub2/grub.cfg
Never edit the main Grub2 configuration file directly. Instead, add new bootloader entries in new files inside the /etc/grub.d directory or inside the /etc/grub.d/40_custom file, then run the configuration update commands as shown above.
The /etc/default/grub file contains key/value configuration parameters that can be used to change the behavior of the Grub bootloader (set the bootloader entry that will be loaded by default, change the bootloader menu background color/image, set bootloader entry load timeout, etc). Configurations from that file are also retrieved during the generation of the main Grub2 configration file, with the commands shown previously. Here is a sample content of the /etc/default/grub configuration file:
# Sample content of /etc/default/grub on Ubuntu 20.04.6 LTS
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
# info -f grub -n 'Simple configuration'
GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""
(...)
Grub2 sample configuration file
menuentry "CentOS Linux" {
set root=(hd1,1) # specify the root device drive and partition. Unlike GRUB legacy
# GRUB2 uses environment variables to configure settings instead of
# commands. Also, unlike GRUB legacy, GRUB2 uses 1 for the first partition
# instead of 0. GRUB2 still uses 0 for the first hard drive
# Examples : (hd0,1) = first partition of the first hard drive device
(hd1,2) = second partition of the second hard drive device
linux /boot/vmlinuz # specify the kernel binary file path. Unlike GRUB legacy, GRUB2 uses 'linux'
# instead of 'kernel' command
initrd /initrd # specify the initial RAM disk image path. / = root device defined with "set root=(hdX,Y)"
}
menuentry "Windows" {
set root=(hd0,1) # same syntax to set the root device for non-Linux systems
}
Grub interfaces and how to get additional infos
Accessing Grub full manual
To open Grub full manual from your favorite Linux distribution, use the following command:
# Open Grub full manual
info -f grub
All config parameters for /etc/default/grub
To get info about all available parameters for the /etc/default/grub configuration file, use the following command:
# All available config parameters for /etc/default/grub
info -f grub -n 'Simple configuration'
Accessing Grub menu before boot
The Esc key can be used to access the grub2 bootloader menu before startup. Use the Shift key for grub legacy bootloader.
Accessing Grub CLI before boot
Grub comes with a built-in command-line interface that can be used at boot time to alter Grub behavior (update boot options, udapte Grub environment variables, etc). The c key can be used to enter the Grub built-in shell at boot time, after the Grub bootloader menu appears. The help command can be used inside the shell to get info about all available commands.
Editing Grub menu entries before boot
The e key can be used at boot time, on a specific boot menu entry, to edit it. After edition, CTRL+O can be used to save the changes. The changes will be available for the current Grub menu session only. A reboot will load the preceding Grub menu entries configuration.
Editing Grub menu entries at boot time can be useful to:
- boot in single mode for SysV init systems
- boot in rescue mode for Systemd systems (launches a shell after the root filesystem in mounted read/write)
- boot in emergency mode for Systemd systems (launches a shell before most filesystems are mounted)
- boot in a shell as last resort (when the init system is broken for instance)
Update boot modes (single, rescue, emergency or shell)
For that, the single, rescue, emergency or init=/bin/sh key words have to be passed as kernel parmameter when editing a boot entry:
# For Grub legacy bootloader
kernel ... single
kernel ... rescue
kernel ... emergency
kernel ... init=/bin/sh
# For Grub2 bootloader
linux ... single
linux ... rescue
linux ... emergency
linux ... init=/bin/sh
After saving the changes and booting using the edited menu entry, you will be asked for the root password to start the session but it may not be necessary to enter it.
Info about Grub interfaces
To get info about Grub interfaces (menu interface and command-line) and what you can do inside those interfaces and with which commands, use the following command:
info -f grub -n 'interface'
Info about Grub environment variables
To get info about Grub environment variables, use the following command:
info -f grub -n 'environment'
Info about all Grub commands
To get info about all available Grub commands, their scopes (menu, menu entry, command-line, etc) and their descriptions, use the following command:
info -f grub -n 'commands'