GRUB2

------------------

How to change default boot kernel permanently or temporarily on CentOS

 #  vi /etc/grub2.cfg         : this is a symbolic link to the /boot/grub2/grub.cfg file,

Change the Default Kernel Version Permanently on CentOS 7

First, list all available kernel images added to GRUB2 by running the following command.

$ grep '^menuentry' /boot/grub2/grub.cfg

Identify the GRUB menu entry for the kernel that you want to set as default. Each menu entry is assigned by GRUB2 a numeric value starting from 0 in an increasing order. That is, the first menu entry assigned 0, the second entry assigned 1, etc. For example, the menu entry value for the kernel 3.10.0-327 is '2' in the above screenshot.

Open /etc/default/grub with a text editor, and set GRUB_DEFAULT to the numeric entry value for the kernel you chose as the default. In this example, I choose the kernel 3.10.0-327 as the default kernel.

GRUB_DEFAULT=1

                          -----------------------------------------------------------------------------------------------------

# grub2-editenv list

saved_entry=CentOS Linux (3.10.0-229.11.1.el7.x86_64) 7 (Core)

# grep ^menuentry /boot/grub2/grub.cfg

menuentry 'CentOS Linux (3.10.0-229.20.1.el7.x86_64) 7 (Core)' ...

menuentry 'CentOS Linux (3.10.0-229.14.1.el7.x86_64) 7 (Core)' ...

menuentry 'CentOS Linux 7 (Core), with Linux 0-rescue-f19b719117b44bf3a3fb777bd4127' ...caf

To permanently define the kernel to execute at boot time (here 0 for the first entry), type:

# grub2-set-default 1

** (Check whether it is added to the default entry)

# grub2-editenv list

Sample Output

saved_entry=1

To display the GRUB2 variables, type:

# 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="serial console"

GRUB_SERIAL_COMMAND="serial --speed=115200"

GRUB_CMDLINE_LINUX="rd.lvm.lv=rhel/swap crashkernel=auto rd.lvm.lv=rhel/root console=ttyS0,115200"

GRUB_DISABLE_RECOVERY="true"

 ** (Save all the changes in GRUB2)

grub2-mkconfig -o /boot/grub2/grub.cfg

Sample output

Generating grub configuration file ...

Found linux image: /boot/vmlinuz-3.10.0-229.el7.x86_64

Found initrd image: /boot/initramfs-3.10.0-229.el7.x86_64.img

Found linux image: /boot/vmlinuz-0-rescue-7e890c03ebb940a9a67163adaef14894

Found initrd image: /boot/initramfs-0-rescue-7e890c03ebb940a9a67163adaef14894.img

Found Windows 7 (loader) on /dev/sda1

done

#  reboot 

                             --------------------------------------------------------------------------------------------------------------------

Finally, re-generate GRUB configuration.

$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg

Reboot and confirm that the specified kernel is selected by GRUB2 by default.

                                                                                                                                                                       

GRUB2 is the most common bootloader for RHEL 7 systems. A symlink to the GRUB2 config file should be present at /etc/grub2.cfg. The post describes changing the default kernel to a old kernel.

How GRUB2 selects which kernel to boot from

By default, the value for the directive GRUB_DEFAULT in the #  vi /etc/default/grub file is “saved”.

# cat /etc/default/grub GRUB_TIMEOUT=5 GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="nomodeset crashkernel=auto rd.lvm.lv=vg_os/lv_root rd.lvm.lv=vg_os/lv_swap rhgb quiet" GRUB_DISABLE_RECOVERY="true"

This instructs GRUB 2 to load the kernel specified by the saved_entry directive in the GRUB 2 environment file, located at /boot/grub2/grubenv.

# cat /boot/grub2/grubenv # GRUB Environment Block saved_entry=Red Hat Enterprise Linux Server (3.10.0-327.10.1.el7.x86_64) 7.2 (Maipo)

One can set another GRUB record to be the default, using the grub2-set-default command, which will update the GRUB 2 environment file. 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.

# 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  # MAKEDEBUG specifies if new-kernel-pkg should create non-default # "debug" entries for new kernels. MAKEDEBUG=yes

Change default kernel

To force a system to always use a particular menu entry, use the menu entry name as the key to the GRUB_DEFAULT directive in the /etc/default/grub file. The following command will print a list of the menu entries present in GRUB2’s configuration.

# awk -F\' /^menuentry/{print\$2} /etc/grub2.cfg Red Hat Enterprise Linux Server (3.10.0-327.10.1.el7.x86_64) 7.2 (Maipo)                          ===> entry 0 Red Hat Enterprise Linux Server (3.10.0-229.el7.x86_64) 7.2 (Maipo)                               ===> entry 1 Red Hat Enterprise Linux Server (0-rescue-0cb6313ed65e4b36ba5daace11f3ad50) 7.2 (Maipo)           ===> entry 2

[root@ ~]# awk -F\' /^menuentry/{print\$2} /etc/grub2-efi.cfg             CentOS Linux (3.10.0-1062.1.2.el7.x86_64) 7 (Core)                ===> entry 0

            CentOS Linux (3.10.0-693.11.6.el7.x86_64) 7 (Core)                ===> entry 1             CentOS Linux (3.10.0-514.16.1.el7.x86_64) 7 (Core)                ===> entry 2             CentOS Linux (3.10.0-327.el7.x86_64) 7 (Core)             CentOS Linux (0-rescue-8c544ce90ebf4157ae4f1d8b12493c92) 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 should be loaded first, pass its number to the grub2-set-default command. The IDs are assigned in order the menu entries appear in the /etc/grub2.cfg file starting with 0. So the kernel 3.10.0-229.el7.x86_64 get an ID of 1.

# grub2-set-default 1

This will make 3.10.0-229.el7.x86_64 as defaul kernel which was the old kernel in the system.

Verify the new default kernel

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/grub file.

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

Rebuild GRUB2

Changes to /etc/default/grub require rebuilding the grub.cfg file as follows:

# grub2-mkconfig -o /boot/grub2/grub.cfg

Reboot

Once you have verified everything and rebuilt the GRUB2 configuration file, you can go ahead an reboot the server for changes to take effect.

# shutdown -r now

                                                                                                                                                   

Change the Default Kernel Version Permanently on CentOS 6 or earlier

CentOS 6 or earlier does not ship with GRUB2, but GRUB as a default boot loader.

The first step to change the default boot kernel on CentOS 6 is to get a list of all available kernel images added to GRUB. The following command will show you the list.

$ sudo grep '^[[:space:]]*kernel' /boot/grub/grub.conf

From the list, identify the kernel that you want to set as default. Each kernel in the list is assigned by GRUB a numeric entry value starting from 0 in an increasing order. That is, the first entry assigned 0, the second entry assigned 1, etc. For example, the kernel 2.6.32-431 is assigned GRUB entry value '1' in the above screenshot.

Open /boot/grub/grub.conf with a text editor, look for 'default=N', and change N to the GRUB entry value for the default kernel to use. For example, to set the kernel 2.6.32-431 as the default kernel:

default=1

Reboot and confirm that the chosen kernel has been booted by default.

Change the Default Boot Kernel Temporarily on CentOS

Often times you may want to switch to a different kernel just temporarily, not permanently. Even if you do want to change the default kernel permanently, it is a good idea to try a temporary boot to a new kernel before making a permanent transition from the old kernel to a new one. That way, in case there is any kernel crash with the new kernel, you can easily fall back to the old kernel simply by rebooting.

Of course you can choose a kernel to boot interactively via GRUB menu. However, such interactive control is not always available, for example, when you are booting over SSH remotely.

Here is how to change the default boot kernel "temporarily" from the command line. I assume that '2' is the numeric GRUB menu entry value of a kernel that you want to boot into during the next boot session. Replace the number with your own.

On CentOS 7:

[root~]# grub2-reboot 2

[root~]# reboot

On CentOS 6:

[root~]# echo "savedefault --default=2 --once" | grub --batch

[root~]# reboot

Once you reboot, you will boot into a specified kernel "just once". Next time you reboot, you will revert to the default kernel.

 

------ Centos 6 ----------------------------------

[root@mail ~]#   vi /etc/grub.conf

# grub.conf generated by anaconda

#

# Note that you do not have to rerun grub after making changes to this file

# NOTICE:  You have a /boot partition.  This means that

#          all kernel and initrd paths are relative to /boot/, eg.

#          root (hd0,1)

#          kernel /vmlinuz-version ro root=/dev/mapper/vg_mail-lv_root

#          initrd /initrd-[generic-]version.img

#boot=/dev/sda1

device (hd0) HD(1,800,64000,6527d478-09aa-4476-b95d-51567caca214)

default=1              # [ defult is 0 change it 1 ]

timeout=5

splashimage=(hd0,1)/grub/splash.xpm.gz

hiddenmenu

title CentOS (2.6.32-642.6.2.el6.x86_64)

        root (hd0,1)

        kernel /vmlinuz-2.6.32-642.6.2.el6.x86_64 ro root=/dev/mapper/vg_mail-lv_root nomodeset rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us LANG=en_US.UTF-8 rd_LVM_LV=vg_mail/lv_root rd_NO_MD rd_LVM_LV=vg_mail/lv_swap crashkernel=128M SYSFONT=latarcyrheb-sun16 rd_NO_DM rhgb quiet

title CentOS (2.6.32-573.18.1.el6.x86_64)

        root (hd0,1)

        kernel /vmlinuz-2.6.32-573.18.1.el6.x86_64 ro root=/dev/mapper/vg_mail-lv_root nomodeset rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us LANG=en_US.UTF-8 rd_LVM_LV=vg_mail/lv_root rd_NO_MD rd_LVM_LV=vg_mail/lv_swap crashkernel=128M SYSFONT=latarcyrheb-sun16 rd_NO_DM rhgb quiet

        initrd /initramfs-2.6.32-573.18.1.el6.x86_64.img

title CentOS (2.6.32-573.12.1.el6.x86_64)

        root (hd0,1)

        kernel /vmlinuz-2.6.32-573.12.1.el6.x86_64 ro root=/dev/mapper/vg_mail-lv_root nomodeset rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us LANG=en_US.UTF-8 rd_LVM_LV=vg_mail/lv_root rd_NO_MD rd_LVM_LV=vg_mail/lv_swap crashkernel=128M SYSFONT=latarcyrheb-sun16 rd_NO_DM rhgb quiet

        initrd /initramfs-2.6.32-573.12.1.el6.x86_64.img

title CentOS (2.6.32-431.el6.x86_64)

        root (hd0,1)

        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/vg_mail-lv_root nomodeset rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us LANG=en_US.UTF-8 rd_LVM_LV=vg_mail/lv_root rd_NO_MD rd_LVM_LV=vg_mail/lv_swap crashkernel=128M SYSFONT=latarcyrheb-sun16 rd_NO_DM rhgb quiet

        initrd /initramfs-2.6.32-431.el6.x86_64.img

-----------