How do I change the default kernel in GRUB that is loaded at startup?
Environment
- Red Hat Enterprise Linux 4
- Red Hat Enterprise Linux 5
- Red Hat Enterprise Linux 6
- Red Hat Enterprise Linux 7
- Red Hat Enterprise Linux 8
- Red Hat Enterprise Linux 9
- Red Hat Enterprise Linux 10
Issue
- The kernel booted by default (using GRUB) is different from the one you would like to be automatically selected on startup. How do I change the default kernel in GRUB that is loaded at startup?
Resolution
- Use the command
grubby --bootloader-probeto find out which bootloader you have installed.
For RHEL 4, 5, 6 Systems:
-
GRUB is the most common bootloader for RHEL 4, 5, 6 systems
Below is an example GRUB configuration file:default=0 <=== timeout=10 splashimage=(hd0,0)/grub/splash.xpm.gz title Red Hat Enterprise Linux ES (kernel number zero) root (hd0,0) kernel /vmlinuz-zero ro root=/dev/hard_drive initrd /initrd-zero.img title Red Hat Enterprise Linux ES (kernel number one) root (hd0,0) kernel /vmlinuz-one ro root=/dev/hard_drive initrd /initrd-one.img -
In this example file, notice the line at the top that reads
default=0. The number 0 (zero) indicates which stanza to select by default. A stanza is the indented portion after the line starting withtitle. GRUB will then use this default stanza to boot, after a number of seconds has passed (specified in the linetimeout=10). The 0 (zero) in this case is referring to the first stanza that starts with "title Red Hat Enterprise Linux ES (kernel number zero)". It includes all of the indented lines up to but not including the next "title" line. -
For example, to instead set the second stanza, "
title Red Hat Enterprise Linux ES (kernel number one)", to be the default, change the default line to:default=1 -
The effect of changes will be seen on the next boot.
For RHEL 7 Systems:
-
GRUB2 is the supported bootloader for x86 RHEL 7 systems
-
Note that ordering of menu entries means very little in GRUB2. By default, each new kernel package install changes the default kernel regardless of its location in the ordering of kernel menu entries. To override this, use
grub2-set-default 'ID', whereIDis either a number or a name, e.g.:# grub2-set-default 'Red Hat Enterprise Linux Server, with Linux 3.10.0-123.el7.x86_64'
-
By default, the key for the
GRUB_DEFAULTdirective in the/etc/default/grubfile is the wordsaved. This instructs GRUB 2 to load the kernel specified by thesaved_entrydirective in the GRUB 2 environment file, located at/boot/grub2/grubenv. One can set another GRUB record to be the default, using thegrub2-set-defaultcommand, which will update the GRUB 2 environment file. -
By default, the
saved_entryvalue is set to the name of latest installed kernel of package type kernel. This is defined in/etc/sysconfig/kernelby theUPDATEDEFAULTandDEFAULTKERNELdirectives. 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 -
To force a system to always use a particular menu entry, use the menu entry name as the key to the
GRUB_DEFAULTdirective in the/etc/default/grubfile. To list the available menu entries, run the following command as root:# awk -F\' /^menuentry/{print\$2} /etc/grub2.cfg Red Hat Enterprise Linux Server, with Linux 3.10.0-123.el7.x86_64 <=== Entry 0 Red Hat Enterprise Linux Server, with Linux 3.10.0-123.9.2.el7.x86_64 <=== Entry 1 Red Hat Enterprise Linux Server, with Linux 3.10.0-123.6.3.el7.x86_64 <=== Entry 2 Red Hat Enterprise Linux Server, with Linux 0-rescue-b03fd2e1e769493994c88 b8da4257178 <=== Entry 3 -
GRUB 2 supports using a numeric value as the key for the
saved_entrydirective 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, pass its number to thegrub2-set-defaultcommand. For example:
# grub2-set-default 1
- Check the below file to see the kernel which will be loaded at next boot, crosscheck the numeric value with the menuentry in the
/etc/default/grubfile.
# cat /boot/grub2/grubenv |grep saved
Eg:
# cat /boot/grub2/grubenv |grep saved
saved_entry=1
-
Changes to
/etc/default/grubrequire rebuilding thegrub.cfgfile as follows: -
Rebuild the
/boot/grub2/grub.cfgfile by running thegrub2-mkconfig -ocommand as follows:-
On BIOS-based machines:
~]# grub2-mkconfig -o /boot/grub2/grub.cfg -
On UEFI-based machines:
~]# grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
-
- For changing default kernel on s390 systems, see: How to set default kernel on s390 systems?
For RHEL 8, 9 & 10 Systems:
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.