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 ?
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.
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.
I improved the awk command a bit to also give you the number to set (Thanks to the “Effective AWK Programming” book for advice!)
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.
its good enough to manage for booting required kernel from existing kernel images.
Thanks,
Gopala
Thank you, this document explained very well.
Thanks, I helped alot.
Very Well explained. Thank you for this.
Please provide some article on troubleshooting .
Thanks for your feedback. Please let us know more in what area you are looking for troubleshooting tips.
Thanks! Accurate and well presented.