Change The Default Boot To Previous Linux Kernel Version In Grub (RHEL/CentOs 7&8)

In this tutorial you will learn how to change the default boot to previous Linux kernel version.

UNDERSTANDING THE SUBJECT MATTER

The kernel is the core engine of the Operating system. After the installation of the kernel, the Operating system needs to be booted and the program that does the booting is the boot loader.

In Linux, there are various boot loaders depending on the Linux version. We have the Lilo, grub and grub2 boot loaders.

The grub2 boot loader is the recent boot loader that is being shipped with the recent Linux operating systems. This tutorial will be using a RHEL 7. Definitely, the boot loader for this study will be the grub2.

Command To Check For A Boot Loader

The grubby tool can be used to confirm the boot loader the OS is using. The command to display the boot loader is

[root@lab01 ~]# grubby –bootloader-probe

grub2
[root@lab01 ~]#

You can see that the system is using the grub2 boot loader program, the program to boot the kernel.

how to revert to previous kernel version

Explaining How to Change Default Linux Kernel Version & What It Entails To Boot The Old Kernel Version

There are files that must be read or rather instructions integrated into the boot loader that must to be read before the operating system (kernel) can boot. For the sake of understanding, let’s call these instructions “table entries”

The “table entries” is found in “/etc/default/grub”

[root@lab01 ~]# cat /etc/default/grub

GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=”$(sed ‘s, release .*$,,g’ /etc/system-release)”
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT=”console”
GRUB_CMDLINE_LINUX=”crashkernel=auto rhgb quiet”
GRUB_DISABLE_RECOVERY=”true”
[root@lab01 ~]#

The boot loader embodies this “table entries” and the “table entries” needs to be read before the the system can boot.

Note that the “table entries” file (etc/default/grub) is different from the boot loader (grub2) configuration file.(/etc/grub2.cfg) or (/boot/grub2/grub.cfg) which is a symlink to the latter as shown in the command below.

[root@lab01 ~]# ll /etc/grub2.cfg

lrwxrwxrwx. 1 root root 22 Dec 30 09:18 /etc/grub2.cfg -> ../boot/grub2/grub.cfg

It is also very Important to note that anytime a kernel is installed, they always include a new line or menu entry in the “/etc/grub2.cfg” file.

For example, every kernel installed on the system have their own line or menu entry in the file below.

[root@lab01 ~]# cat /etc/grub2.cfg

#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
set pager=1

if [ -s $prefix/grubenv ]; then
load_env
fi
if [ “${next_entry}” ] ; then
set default=”${next_entry}”
set next_entry=
save_env next_entry
set boot_once=true
else
set default=”${saved_entry}”
fi

if [ x”${feature_menuentry_id}” = xy ]; then
menuentry_id_option=”–id”
else
menuentry_id_option=””
fi

export menuentry_id_option

if [ “${prev_saved_entry}” ]; then
set saved_entry=”${prev_saved_entry}”
save_env saved_entry
set prev_saved_entry=
save_env prev_saved_entry
set boot_once=true
fi

function savedefault {
if [ -z “${boot_once}” ]; then
saved_entry=”${chosen}”
save_env saved_entry
fi
}

function load_video {
if [ x$feature_all_video_module = xy ]; then
insmod all_video
else
insmod efi_gop
insmod efi_uga
insmod ieee1275_fb
insmod vbe
insmod vga
insmod video_bochs
insmod video_cirrus
fi
}

terminal_output console
if [ x$feature_timeout_style = xy ] ; then
set timeout_style=menu
set timeout=5
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
else
set timeout=5
fi
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/00_tuned ###
set tuned_params=””
set tuned_initrd=””
### END /etc/grub.d/00_tuned ###

### BEGIN /etc/grub.d/01_users ###
if [ -f ${prefix}/user.cfg ]; then
source ${prefix}/user.cfg
if [ -n “${GRUB2_PASSWORD}” ]; then
set superusers=”root”
export superusers
password_pbkdf2 root ${GRUB2_PASSWORD}
fi
fi
### END /etc/grub.d/01_users ###

### BEGIN /etc/grub.d/10_linux ###
menuentry ‘Red Hat Enterprise Linux Server (3.10.0-862.2.3.el7.x86_64) 7.5 (Maipo)’ –class red –class gnu-linux –class gnu –class os –unrestricted $menuentry_id_option ‘gnulinux-3.10.0-862.el7.x86_64-advanced-dc5cd239-cfa2-4977-ac4d-e03e481b40be’ {
load_video
set gfxpayload=keep
insmod gzio
insmod part_msdos
insmod xfs
set root=’hd0,msdos1′
if [ x$feature_platform_search_hint = xy ]; then
search –no-floppy –fs-uuid –set=root –hint-bios=hd0,msdos1 –hint-efi=hd0,msdos1 –hint-baremetal=ahci0,msdos1 –hint=’hd0,msdos1′ ed0ea8f9-e39d-41c8-8818-4db1800aa8bd
else
search –no-floppy –fs-uuid –set=root ed0ea8f9-e39d-41c8-8818-4db1800aa8bd
fi
linux16 /vmlinuz-3.10.0-862.2.3.el7.x86_64 root=UUID=dc5cd239-cfa2-4977-ac4d-e03e481b40be ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8
initrd16 /initramfs-3.10.0-862.2.3.el7.x86_64.img
}
menuentry ‘Red Hat Enterprise Linux Server (3.10.0-862.el7.x86_64) 7.5 (Maipo)’ –class red –class gnu-linux –class gnu –class os –unrestricted $menuentry_id_option ‘gnulinux-3.10.0-862.el7.x86_64-advanced-dc5cd239-cfa2-4977-ac4d-e03e481b40be’ {
load_video
set gfxpayload=keep
insmod gzio
insmod part_msdos
insmod xfs
set root=’hd0,msdos1′
if [ x$feature_platform_search_hint = xy ]; then
search –no-floppy –fs-uuid –set=root –hint-bios=hd0,msdos1 –hint-efi=hd0,msdos1 –hint-baremetal=ahci0,msdos1 –hint=’hd0,msdos1′ ed0ea8f9-e39d-41c8-8818-4db1800aa8bd
else
search –no-floppy –fs-uuid –set=root ed0ea8f9-e39d-41c8-8818-4db1800aa8bd
fi
linux16 /vmlinuz-3.10.0-862.el7.x86_64 root=UUID=dc5cd239-cfa2-4977-ac4d-e03e481b40be ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8
initrd16 /initramfs-3.10.0-862.el7.x86_64.img
}
menuentry ‘Red Hat Enterprise Linux Server (0-rescue-1a030f37ce5440afb23d0499301ecde6) 7.5 (Maipo)’ –class red –class gnu-linux –class gnu –class os –unrestricted $menuentry_id_option ‘gnulinux-0-rescue-1a030f37ce5440afb23d0499301ecde6-advanced-dc5cd239-cfa2-4977-ac4d-e03e481b40be’ {
load_video
insmod gzio
insmod part_msdos
insmod xfs
set root=’hd0,msdos1′
if [ x$feature_platform_search_hint = xy ]; then
search –no-floppy –fs-uuid –set=root –hint-bios=hd0,msdos1 –hint-efi=hd0,msdos1 –hint-baremetal=ahci0,msdos1 –hint=’hd0,msdos1′ ed0ea8f9-e39d-41c8-8818-4db1800aa8bd
else
search –no-floppy –fs-uuid –set=root ed0ea8f9-e39d-41c8-8818-4db1800aa8bd
fi
linux16 /vmlinuz-0-rescue-1a030f37ce5440afb23d0499301ecde6 root=UUID=dc5cd239-cfa2-4977-ac4d-e03e481b40be ro crashkernel=auto rhgb quiet
initrd16 /initramfs-0-rescue-1a030f37ce5440afb23d0499301ecde6.img
}

### END /etc/grub.d/10_linux ###

### BEGIN /etc/grub.d/20_linux_xen ###
### END /etc/grub.d/20_linux_xen ###

### BEGIN /etc/grub.d/20_ppc_terminfo ###
### END /etc/grub.d/20_ppc_terminfo ###

### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###

### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the ‘exec tail’ line above.
### END /etc/grub.d/40_custom ###

### BEGIN /etc/grub.d/41_custom ###
if [ -f ${config_directory}/custom.cfg ]; then
source ${config_directory}/custom.cfg
elif [ -z “${config_directory}” -a -f $prefix/custom.cfg ]; then
source $prefix/custom.cfg;
fi
### END /etc/grub.d/41_custom ###
[root@lab01 ~]#

Moving forward,

From the command, “cat /etc/default/grub, in my case as shown above, the third line (“GRUB_DEFAULT=saved) in the /etc/default/grub file means that when the system is booting, it should use the parameters in the “saved”. that is ‘saved entry”

Note again: we have used three entries in this article, “table entries” , menu entry and saved entry.

The saved entry parameter can be found in “/etc/sysconfig/kernel” which apparently means that in the line “GRUB_DEFAULT=saved” in the table entries, the value saved is a directive of the file “/etc/sysconfig/kernel”

[root@lab01 ~]# cat /etc/sysconfig/kernel

# UPDATEDEFAULT specifies if new-kernel-pkg should make
# new kernels the default
UPDATEDEFAULT=yes

# DEFAULTKERNEL specifies the default kernel package type
DEFAULTKERNEL=kernel
[root@lab01 ~]#

Heads up,

The line UPDATEDEFAULT=yes and DEFAULTKERNEL=kernel parameter simply means that the saved entry value is set to the latest kernel installed.

In other words, whenever the system is booting up, the saved entry value will be read in the “table entries” (/etc/default/grub). So the newest kernel installed will be the default kernel in grub that will be loaded at startup.

The saved entry value, (which is simply set to the latest kernel installed) is also a directive of the “/boot/grub2/grubenv” file .

It can be confirmed by checking the “/boot/grub2/grubenv” file where the saved value is actually defined but unfortunately, the file can’t be read except you grep exactly for the word “saved”

[root@lab01 ~]# cat /boot/grub2/grubenv

# GRUB Environment Block
saved_entry=Red Hat Enterprise Linux Server (3.10.0-862.2.3.el7.x86_64) 7.5 (Maipo)
##################################
##################################
##################################
##################################
##################################
##################################
##################################
[root@lab01 ~]#

[root@lab01 ~]# cat /boot/grub2/grubenv |grep saved

saved_entry=Red Hat Enterprise Linux Server (3.10.0-862.2.3.el7.x86_64) 7.5 (Maipo)
[root@lab01 ~]#

You can now see the saved entry value.

The saved entry value can be defined numerically or by a name. Hence, we need to be able to define the saved entry value.

How To Define Or Change The Saved Entry Value In Grub

To define the saved entry value, you need to do the following,

1. check for the available menu entries by using the command below.

[root@lab01 ~]# awk -F\’ /^menuentry/{print\$2} /etc/grub2.cfg

Red Hat Enterprise Linux Server (3.10.0-862.2.3.el7.x86_64) 7.5 (Maipo)
Red Hat Enterprise Linux Server (3.10.0-862.el7.x86_64) 7.5 (Maipo)
Red Hat Enterprise Linux Server (0-rescue-087f315720a84471a22c1beb7ad209fa) 7.5 (Maipo)
[root@lab01 ~]#

2. define the saved entry value

To define by name you use the following command

[root@lab01 ~]# grub2-set-default ‘Red Hat Enterprise Linux Server, with Linux 3.10.0-862.el7.x86_64’

NOTE: To define by value, by the program instruction, the first menu (line) in the menu entry has a numerical value of 0, while the second menu has 1, the third has 2 and that is how it goes consecutively.

To define by value, you use the following command,

[root@lab01 ~]# grub2-set-default 1

3. confirm,

[root@lab01 ~]# cat /boot/grub2/grubenv |grep saved

saved_entry=1

The Chart below also summarizes the explanation.

Previous Linux kernel version
Chart Explaining The Linux Kernel Directives (RHEL7/8)


ACTION TIME

Having understood the subject matter, we will go through the step by step process on how to change/revert/set the kernel to the desired version we want the system to boot with by default.

Step By Step On How To Change The Linux Kernel To The Previous/Desired Version You Want The System To Boot With By Default

1. confirm the default kernel the system is using.

[root@lab01 ~]# uname -r

3.10.0-862.2.3.el7.x86_64
[root@lab01 ~]#

2. confirm the kernels installed on the system

[root@lab01 ~]# rpm -qa kernel

kernel-3.10.0-862.el7.x86_64
kernel-3.10.0-862.2.3.el7.x86_64
[root@lab01 ~]#

N:B: we want to change the default startup kernel to “kernel-3.10.0-862.2.3.el7.x86_64”

3. check for the available menu entries, just a way to be sure of what you are doing.

[root@lab01 ~]# awk -F\’ /^menuentry/{print\$2} /etc/grub2.cfg

Red Hat Enterprise Linux Server (3.10.0-862.2.3.el7.x86_64) 7.5 (Maipo)
Red Hat Enterprise Linux Server (3.10.0-862.el7.x86_64) 7.5 (Maipo)
Red Hat Enterprise Linux Server (0-rescue-087f315720a84471a22c1beb7ad209fa) 7.5 (Maipo)
[root@lab01 ~]#

On RHEL 8, the menu entries may not show with the command.

But note that just I have explained in the “UNDERSTANDING THE SUBJECT MATTER” above, the kernel which the system is running on will have a numerical value or index of 0, the next kernel will have 1, followed by 2, and consecutively from latest to the older depending on the number of kernels installed on the system.

For example,

[root@lab01 ~]# rpm -qa kernel

kernel-3.10.0-862.el7.x86_64  ---------------0
kernel-3.10.0-862.2.3.el7.x86_64-------------1
kernel-3.10.0-864.2.8.el7.x86_64-------------2

To verify the mapped index, use the command,

[root@DRQAS1 ~]# grubby --default-index

0

4. make a backup of /boot/grub2/grub.cfg

[root@lab01 ~]# cp /boot/grub2/grub.cfg /boot/grub2/grub.cfg_bkup
[root@lab01 ~]#

5. set the saved entry to the desired startup kernel from the menu entries

[root@lab01 ~]# grub2-set-default ‘Red Hat Enterprise Linux Server, with Linux 3.10.0-862.el7.x86_64

[root@lab01 ~]# cat /boot/grub2/grubenv |grep saved

saved_entry=Red Hat Enterprise Linux Server, with Linux 3.10.0-862.el7.x86_64
[root@lab01 ~]#

or   

[root@lab01 ~]# grub2-set-default 1

[root@lab01 ~]# cat /boot/grub2/grubenv |grep saved

saved_entry=1

or
[root@lab01 ~]# grubby --set-default-index=1

6. confirm the default value that is set

[root@lab01 ~]# cat /boot/grub2/grubenv |grep saved

saved_entry=1

or
[root@DRQAS1 ~]# grubby --default-index

1

7. rebuild the boot loader (grub2) configuration file.

For BIOS based system, do

[root@lab01 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg

Generating grub configuration file …
Warning: Requested serial terminal but GRUB_SERIAL_COMMAND is unspecified. Default parameters will be used.
Found linux image: /boot/vmlinuz-3.10.0-862.2.3.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-862.2.3.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-862.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-862.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-087f315720a84471a22c1beb7ad209fa
Found initrd image: /boot/initramfs-0-rescue-087f315720a84471a22c1beb7ad209fa.img
done
[root@lab01 ~]#

For UEFI based system,do

[root@lab01 ~]# grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg

8. reboot the system.

[root@lab01 ~]# reboot

9. confirm the new default boot-up kernel after the system is booted up.

[root@lab01 ~]# uname -r

3.10.0-862.2.3.el7.x86_64
[root@lab01 ~]#


Removing The Old Kernel

You may want to remove the old kernel on your system, to do that you use the command

< rpm -e <old-kernel > 

Cheers!!!

Your feedback is welcomed. If you love others, you will share with others

Be the first to comment

Leave a Reply

Your email address will not be published.


*