Setting the default kernel in CentOS 7

The default kernel can be set with the command 'grub2-set-default'.

But first we need to update the Grub configuration with 'grub2-mkconfig' (in order to get the list of available kernels).

On my system I am using ZFS and 'grub2-mkconfig' is not working without setting the environment variable 'ZPOOL_VDEV_NAME_PATH' to 'YES'.

[root@localhost ~]# export ZPOOL_VDEV_NAME_PATH=YES
[root@localhost ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-693.5.2.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.5.2.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-693.5.2.bak1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.5.2.bak1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-514.10.2.bak1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-514.10.2.bak1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-514.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-514.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-263ede4315d14b8ba4b3a93abc833aea
Found initrd image: /boot/initramfs-0-rescue-263ede4315d14b8ba4b3a93abc833aea.img
done
[root@localhost ~]# 

We get the list of the installed kernels:

[root@localhost ~]# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux (3.10.0-693.5.2.el7.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-693.5.2.bak1.el7.x86_64) 7 (Core)
2 : CentOS Linux (3.10.0-514.10.2.bak1.el7.x86_64) 7 (Core)
3 : CentOS Linux (3.10.0-514.el7.x86_64) 7 (Core)
4 : CentOS Linux (0-rescue-263ede4315d14b8ba4b3a93abc833aea) 7 (Core)
[root@localhost ~]# 

Another way:

[root@localhost ~]# grep "^menuentry" /boot/grub2/grub.cfg | cut -d "'" -f2
CentOS Linux (3.10.0-693.5.2.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-693.5.2.bak1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-514.10.2.bak1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-514.el7.x86_64) 7 (Core)
CentOS Linux (0-rescue-263ede4315d14b8ba4b3a93abc833aea) 7 (Core)
[root@localhost ~]# 

The default entry is defined by the GRUB_DEFAULT line in the /etc/default/grub file.

[root@localhost ~]# grep DEFAULT  /etc/default/grub 
GRUB_DEFAULT=saved
[root@localhost ~]#

However, if the GRUB_DEFAULT line is set as 'saved' (like in this example), the default kernel is stored in the /boot/grub2/grubenv file (it cannot be manually edited, the 'grub2-set-default' should be used instead). It may be viewed by:

[root@localhost ~]# grub2-editenv list
saved_entry=CentOS Linux (3.10.0-514.10.2.el7.x86_64) 7 (Core)
[root@localhost ~]# 

The command 'grub2-set-default' accepts the number of the kernel (the first kernel in the list is 0), the menu item title or the menu item identifier.

[root@localhost ~]# grub2-editenv list
saved_entry=CentOS Linux (3.10.0-514.10.2.el7.x86_64) 7 (Core)
[root@localhost ~]# grub2-set-default 0
[root@localhost ~]# grub2-editenv list
saved_entry=0
[root@localhost ~]# 

If you prefer to use the menu item title:

[root@localhost ~]# grub2-set-default "CentOS Linux (3.10.0-693.5.2.el7.x86_64) 7 (Core)"

After rebooting I see the correct kernel selected as default:

It's good if you make backups of your working kernels and their initramfs images (especially if you are using additional kernel modules like ZFS).

If you use ZFS you should check if the default kernel is working before reboot. Sometimes after update the initramfs of the kernel is updated without the critical module like zfs.ko and the system will not boot. I have some bad experience with this: When updating your CentOS system with kmod-zfs, please update also your zfs.repo and verify that the zfs.ko is installed on your kernel.

Verify that the kernel module zfs.ko is included in the initramfs image with this bash script [CentOS 7, ZFS]

Comments