How to set default boot kernel on Linux ( CentOS / RHEL 7 )


GRUB2, How To, Linux, Tips and Tricks

How to set default boot kernel in linux? How to change the default boot entry for kernel ? How to boot with old kernel version ? How to revert to previous kernel version ?

How to set default boot kernel on Linux ( CentOS / RHEL 7 )

 

In my last article I had shared the commands to check if server is physical or virtual in Linux or Unix . GRUB2 is the version of GRUB currently in use. The software was rewritten to allow plugins and expand features available in the menu system. GRUB legacy and GRUB2 are otherwise fairly similar.

 

By default, the saved_entry value is set to the name of latest installed kernel of package type kernel. This is defined in /etc/sysconfig/kernel by the UPDATEDEFAULT and DEFAULTKERNEL directives. The file can be viewed by the root user as follows:

# 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

When new kernels are installed, they should include a new stanza in the bootloader configuration file, /boot/grub2/grub.conf. The default stanza is based on the saved_entry directive in the /boot/grub2/grubenv file.

 

For example currently my system is configured is to boot from 3.10.0-957.1.3.el7.x86_64

# grep saved /boot/grub2/grubenv
saved_entry=CentOS Linux (3.10.0-957.1.3.el7.x86_64) 7 (Core)

Now currently I have a different kernel loaded on my system

# uname -r
3.10.0-693.el7.x86_64

This is because I just installed a newer kernel on my system but since I have not rebooted my node, still old kernel is loaded.

So after reboot the kernel version provided in "saved_entry" will be loaded. Now after reboot as expected my loaded kernel has changed

# uname -r
3.10.0-957.1.3.el7.x86_64

 

How to set default boot kernel?

The boot configuration when using GRUB 2 is in the /boot/grub2/grub.cfg file. You can also refer to it by the /etc/grub2.cfg file which is a symbolic link. To force a system to always use a particular menu entry and to set default boot kernel, use the menu entry name as the key to the GRUB_DEFAULT directive in the /etc/default/grub file.

To list the available menu entries, run the following command as root:

# awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
CentOS Linux (3.10.0-957.1.3.el7.x86_64) 7 (Core)				 	<<==== Entry 0
CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)					 	<<==== Entry 1
CentOS Linux (0-rescue-4c3582b97843401fbc3c8e68c07e553c) 7 (Core)

 

GRUB 2 supports using a numeric value as the key for the saved_entry directive to change the default order in which the kernel or operating systems are loaded. To specify which kernel or operating system should be loaded first i.e. to set default boot kernel, pass its number to the grub2-set-default command which sets the default boot menu entry for all subsequent boots.

NOTE:
grub2-set-default command only works for GRUB configuration files created with GRUB_DEFAULT=saved in /etc/default/grub.
# grep GRUB_DEFAULT /etc/default/grub
GRUB_DEFAULT=saved

For example to set default boot kernel let us use numeric value. Now as per our /etc/grub2.cfg we have two menuentry for kernel

# awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
CentOS Linux (3.10.0-957.1.3.el7.x86_64) 7 (Core)				 	<<==== Entry 0
CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)					 	<<==== Entry 1
CentOS Linux (0-rescue-4c3582b97843401fbc3c8e68c07e553c) 7 (Core)

where "entry 0" is loaded currently (3.10.0-957.1.3.el7.x86_64).

Let us change it to old or previous kernel version (3.10.0-693.el7.x86_64)

# grub2-set-default 1

Validate the changes

# grep saved /boot/grub2/grubenv
saved_entry=1

Next rebuild your GRUB2 configuration

# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-957.1.3.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-957.1.3.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-693.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-4c3582b97843401fbc3c8e68c07e553c
Found initrd image: /boot/initramfs-0-rescue-4c3582b97843401fbc3c8e68c07e553c.img
done

Now after rebooting the node, we see the old kernel has successfully loaded

# uname -r
3.10.0-693.el7.x86_64

 

Lastly I hope the steps from the article to set default boot kernel on Linux was helpful. So, let me know your suggestions and feedback using the comment section.
 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

8 thoughts on “How to set default boot kernel on Linux ( CentOS / RHEL 7 )”

  1. I improved the awk command a bit to also give you the number to set (Thanks to the “Effective AWK Programming” book for advice!)

    awk -F\' 'BEGIN {out=0}; $1=="menuentry " {print $2," == " out; ++out}' /etc/grub2.cfg
    Reply
  2. Thanks, very useful.

    I’ve got to follow the procedure twice, I though that at the last reboot (done by sysadmins) the kernel was manually elected, so default entry was wrong.
    After follow the procedure and boot again the “grep menuentry” step was corrected, and I could choose the correct one.

    Thanks again.

    Reply

Leave a Comment