Kernel

 Upgrade to Kernel 5.0 in CentOS 7

Step 1: Checking Installed Kernel Version

When we install a distribution it includes a certain version of the Linux kernel. To show the current version installed on our system we can do:

# uname -sr

The following image shows the output of the above command in a CentOS 7 server:

Check Kernel Version in CentOS 7

If we now go to https://www.kernel.org/, we will see that the latest kernel version is 5.0 at the time of this writing (other versions are available from the same site).

This new Kernel 5.0 version is a long-term release and will be supported for 6 years, earlier all Linux Kernel versions were supported for 2 years only.

One important thing to consider is the life cycle of a kernel version – if the version you are currently using is approaching its end of life, no more bug fixes will be provided after that date. For more info, refer to the kernel Releases page.

Step 2: Upgrading Kernel in CentOS 7

Most modern distributions provide a way to upgrade the kernel using a package management system such as yum and an officially-supported repository.

Important: If you looking to run custom compiled Kernel, then you should read our article that explains How to Compile Linux Kernel on CentOS 7 from sources.

However, this will only perform the upgrade to the most recent version available from the distribution’s repositories – not the latest one available in the https://www.kernel.org/. Unfortunately, Red Hat only allows to upgrade the kernel using the former option.

As opposed to Red Hat, CentOS allows the use of ELRepo, a third-party repository that makes the upgrade to a recent version a kernel.

To enable the ELRepo repository on CentOS 7, do:

# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org # rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm 

Enable ELRepo in CentOS 7

Once the repository has been enabled, you can use the following command to list the available kernel.related packages:

# yum --disablerepo="*" --enablerepo="elrepo-kernel" list available

Yum – Find Available Kernel Versions

Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile  * elrepo-kernel: mirror-hk.koddos.net Available Packages kernel-lt.x86_64                        4.4.176-1.el7.elrepo        elrepo-kernel kernel-lt-devel.x86_64                  4.4.176-1.el7.elrepo        elrepo-kernel kernel-lt-doc.noarch                    4.4.176-1.el7.elrepo        elrepo-kernel kernel-lt-headers.x86_64                4.4.176-1.el7.elrepo        elrepo-kernel kernel-lt-tools.x86_64                  4.4.176-1.el7.elrepo        elrepo-kernel kernel-lt-tools-libs.x86_64             4.4.176-1.el7.elrepo        elrepo-kernel kernel-lt-tools-libs-devel.x86_64       4.4.176-1.el7.elrepo        elrepo-kernel kernel-ml.x86_64                        5.0.0-1.el7.elrepo          elrepo-kernel kernel-ml-devel.x86_64                  5.0.0-1.el7.elrepo          elrepo-kernel kernel-ml-doc.noarch                    5.0.0-1.el7.elrepo          elrepo-kernel kernel-ml-headers.x86_64                5.0.0-1.el7.elrepo          elrepo-kernel kernel-ml-tools.x86_64                  5.0.0-1.el7.elrepo          elrepo-kernel kernel-ml-tools-libs.x86_64             5.0.0-1.el7.elrepo          elrepo-kernel kernel-ml-tools-libs-devel.x86_64       5.0.0-1.el7.elrepo          elrepo-kernel perf.x86_64                             5.0.0-1.el7.elrepo          elrepo-kernel python-perf.x86_64                      5.0.0-1.el7.elrepo          elrepo-kernel

Next, install the latest mainline stable kernel:

# yum --enablerepo=elrepo-kernel install kernel-ml

Install Kernel 5.0 in CentOS 7

Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile  * base: centos.mirror.net.in  * elrepo: mirror-hk.koddos.net  * elrepo-kernel: mirror-hk.koddos.net  * epel: repos.del.extreme-ix.org  * extras: centos.mirror.net.in  * updates: centos.mirror.net.in Resolving Dependencies --> Running transaction check ---> Package kernel-ml.x86_64 0:5.0.0-1.el7.elrepo will be installed --> Finished Dependency Resolution  Dependencies Resolved  ====================================================================================  Package                Arch        Version                 Repository        Size ==================================================================================== Installing:  kernel-ml              x86_64      5.0.0-1.el7.elrepo      elrepo-kernel     47 M  Transaction Summary ==================================================================================== Install  1 Package  Total download size: 47 M Installed size: 215 M Is this ok [y/d/N]: y Downloading packages: kernel-ml-5.0.0-1.el7.elrepo.x86_64.rpm                           |  47 MB  00:01:21      Running transaction check Running transaction test Transaction test succeeded Running transaction   Installing : kernel-ml-5.0.0-1.el7.elrepo.x86_64                1/1    Verifying  : kernel-ml-5.0.0-1.el7.elrepo.x86_64                1/1   Installed:   kernel-ml.x86_64 0:5.0.0-1.el7.elrepo                                                                                                                                                                             Complete!

Finally, reboot your machine to apply the latest kernel, and then select latest kernel from the menu as shown.

Select Latest Kernel Version

Login as root, and run following command to check the kernel version:

# uname -sr

Verify Kernel Version

Step 3: Set Default Kernel Version in GRUB

To make the newly-installed version the default boot option, you will have to modify the GRUB configuration as follows:

Open and edit the file # vi /etc/default/grub and set GRUB_DEFAULT=0. This means that the first kernel in the GRUB initial screen will be used as default.

GRUB_TIMEOUT=5 GRUB_DEFAULT=0 GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap crashkernel=auto rhgb quiet" GRUB_DISABLE_RECOVERY="true"

Next, run the following command to recreate the kernel configuration.

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

Set Default Kernel Version in Grub

Generating grub configuration file ... Found linux image: /boot/vmlinuz-5.0.0-1.el7.elrepo.x86_64 Found initrd image: /boot/initramfs-5.0.0-1.el7.elrepo.x86_64.img Found linux image: /boot/vmlinuz-4.20.0-1.el7.elrepo.x86_64 Found initrd image: /boot/initramfs-4.20.0-1.el7.elrepo.x86_64.img Found linux image: /boot/vmlinuz-4.19.11-1.el7.elrepo.x86_64 Found initrd image: /boot/initramfs-4.19.11-1.el7.elrepo.x86_64.img Found linux image: /boot/vmlinuz-4.19.0-1.el7.elrepo.x86_64 Found initrd image: /boot/initramfs-4.19.0-1.el7.elrepo.x86_64.img 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-1e2b46dbc0c04b05b592c837c366bb76 Found initrd image: /boot/initramfs-0-rescue-1e2b46dbc0c04b05b592c837c366bb76.img done

**The path on some OSs has been changed to  E F I [ EFI BOOT ] ** 

# /boot/efi/EFI/centos/grub.cfg  

 [root@mail ~]#   cp /boot/efi/EFI/centos/grub.cfg /boot/efi/EFI/centos/grub.cfg.org

 [root@mail ~]#   grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg

#  reboot

Reboot and verify that the latest kernel is now being used by default.

Booting Default Kernel Version in CentOS 7

Congratulations! You have upgraded your kernel in CentOS 7!

Summary

                                     ------------------------X--------------------------------------

Check all available kernel versions in the Grub2 with awk command below.

#awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg

You see that we have two kernel versions — 3.10 and 5.0.11.

Now we want to configure the default kernel that will run when the system is starting. We will use kernel 5.0 as our default, so you can use the command below to set it up.

#sudo grub2-set-default 0

0 — it’s from the awk command on the top. Kernel 5.0.11 = 0, and Kernel 3.10 = 1. When you want to back to the old kernel, you can change the value of the grub2-set-default command to 1.

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

Install Linux Kernel 5.0 on CentOS 7

Step 1. First, let’s start by ensuring your system is up-to-date.

yum clean all yum -y update

Step 2. Installing Linux Kernel 5.0 on CentOS.

ElRepo is a third-party repository for CentOS that allows upgrades to the latest kernel version from kernel.org. Add ELRepo repository to your CentOS 7 by running the commands below:

sudo yum install https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm sudo rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

Once the repository has been enabled, you can use the following command to list the available kernel.related packages:

yum --disablerepo="*" --enablerepo="elrepo-kernel" list available

kernel-ml.x86_64                        5.0.0-1.el7.elrepo         elrepo-kernel  kernel-ml-devel.x86_64                  5.0.0-1.el7.elrepo         elrepo-kernel  kernel-ml-doc.noarch                    5.0.0-1.el7.elrepo         elrepo-kernel  kernel-ml-headers.x86_64                5.0.0-1.el7.elrepo         elrepo-kernel  kernel-ml-tools.x86_64                  5.0.0-1.el7.elrepo         elrepo-kernel  kernel-ml-tools-libs.x86_64             5.0.0-1.el7.elrepo         elrepo-kernel  kernel-ml-tools-libs-devel.x86_64       5.0.0-1.el7.elrepo         elrepo-kernel

Now that we have confirmed availability of Linux Kernel 5.0, we can proceed to install it:

sudo yum --enablerepo=elrepo-kernel install kernel-ml

Also install kernel-ml-devel,kernel-ml-headers,kernel-ml-tools,perf:

sudo yum -y --enablerepo=elrepo-kernel install kernel-ml-{devel,headers,perf}

Finally, reboot your machine to apply the latest kernel, and then select latest kernel from the menu as shown:

sudo reboot

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

How to properly update kernel in RHEL/CentOS 7/8 Linux

Updating kernel is a critical change so it is always recommended to keep the old kernel before removing it. Let me show you what I mean with this. Here currently 3.10.0-957.21.3.el7 version of kernel is loaded on my RHEL Linux node

# uname -r 3.10.0-957.21.3.el7.x86_64  # rpm -q kernel kernel-3.10.0-957.21.3.el7.x86_64

To list all the installed kernel rpms. (The main one is kernel-3.10.0-957.21.3.el7.x86_64)

# rpm -qa| grep kernel kernel-tools-libs-3.10.0-957.21.3.el7.x86_64 kernel-3.10.0-957.21.3.el7.x86_64 kernel-tools-3.10.0-957.21.3.el7.x86_64

Now I have downloaded an updated version of kernel rpm and the dependency rpm from RHEL online portal.

# rpm -Uvh /tmp/kernel-3.10.0-1062.el7.x86_64.rpm /tmp/linux-firmware-20190429-72.gitddde598.el7.noarch.rpm warning: /tmp/kernel-3.10.0-1062.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Preparing...                          ################################# [100%] Updating / installing...    1:linux-firmware-20190429-72.gitddd################################# [ 25%]    2:kernel-3.10.0-1062.el7           ################################# [ 50%] Cleaning up / removing...    3:kernel-3.10.0-957.21.3.el7       ################################# [ 75%]    4:linux-firmware-20180911-69.git85c################################# [100%]

As you see from the message, the older kernel is being removed. Now rpm is unable to identify the difference between kernel rpm and other system rpms hence it treats kernel the same way.

# rpm -qa| grep kernel kernel-3.10.0-1062.el7.x86_64 kernel-tools-libs-3.10.0-957.21.3.el7.x86_64 kernel-tools-3.10.0-957.21.3.el7.x86_64

Now here after boot if the system halts due to some problem and fails to come up then you have no way to recover your Linux node. The only option left for you would be to go into rescue mode and then try recovering your system which is a lot of trouble (believe me when I say this)

 

Update kernel using yum

Now let us try to perform the same task using yum command. With yum you can either use “yum install” or “yum update” to update kernel, either way yum will understand that you are trying to update kernel hence it will perform “install” action. Hence the older kernel will not be removed while the newer one will still be installed.

To check the list of kernel available in your repository

# yum list kernel Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager Installed Packages kernel.x86_64                     3.10.0-957.el7                           @anaconda/7.6 Available Packages kernel.x86_64                     3.10.0-1062.1.1.el7                      rhel-7-server-rpms

NOTE:

If you are not subscribed to RHN then you can manually download the kernel rpm along with it’s dependencies and then perform yum install by giving the absolute path of the rpm.

Next let us try to install the new kernel. For the sake of this example I will update kernel and all other relative packages

# yum install kernel kernel-tools kernel-tools-libs Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager Resolving Dependencies --> Running transaction check ---> Package kernel.x86_64 0:3.10.0-1062.1.1.el7 will be installed --> Processing Dependency: linux-firmware >= 20190429-72 for package: kernel-3.10.0-1062.1.1.el7.x86_64 ---> Package kernel-tools.x86_64 0:3.10.0-957.el7 will be updated ---> Package kernel-tools.x86_64 0:3.10.0-1062.1.1.el7 will be an update ---> Package kernel-tools-libs.x86_64 0:3.10.0-957.el7 will be updated ---> Package kernel-tools-libs.x86_64 0:3.10.0-1062.1.1.el7 will be an update --> Running transaction check ---> Package linux-firmware.noarch 0:20180911-69.git85c5d90.el7 will be updated ---> Package linux-firmware.noarch 0:20190429-72.gitddde598.el7 will be an update --> Finished Dependency Resolution  Dependencies Resolved  ===================================================================================================================================  Package                        Arch                Version                                  Repository                       Size =================================================================================================================================== Installing:  kernel                         x86_64              3.10.0-1062.1.1.el7                      rhel-7-server-rpms               50 M Updating:  kernel-tools                   x86_64              3.10.0-1062.1.1.el7                      rhel-7-server-rpms              7.8 M  kernel-tools-libs              x86_64              3.10.0-1062.1.1.el7                      rhel-7-server-rpms              7.7 M Updating for dependencies:  linux-firmware                 noarch              20190429-72.gitddde598.el7               rhel-7-server-rpms               73 M  Transaction Summary =================================================================================================================================== Install  1 Package Upgrade  2 Packages (+1 Dependent package)  Total size: 139 M Total download size: 65 M Is this ok [y/d/N]: y Downloading packages: No Presto metadata available for rhel-7-server-rpms (1/3): kernel-tools-3.10.0-1062.1.1.el7.x86_64.rpm                                                          | 7.8 MB  00:00:01 (2/3): kernel-tools-libs-3.10.0-1062.1.1.el7.x86_64.rpm                                                     | 7.7 MB  00:00:01 (3/3): kernel-3.10.0-1062.1.1.el7.x86_64.rpm                                                                |  50 MB  00:00:07 ----------------------------------------------------------------------------------------------------------------------------------- Total                                                                                              9.1 MB/s |  65 MB  00:00:07 Running transaction check Running transaction test Transaction test succeeded Running transaction   Updating   : linux-firmware-20190429-72.gitddde598.el7.noarch                                                                1/7   Updating   : kernel-tools-libs-3.10.0-1062.1.1.el7.x86_64                                                                    2/7   Updating   : kernel-tools-3.10.0-1062.1.1.el7.x86_64                                                                         3/7   Installing : kernel-3.10.0-1062.1.1.el7.x86_64                                                                               4/7   Cleanup    : kernel-tools-3.10.0-957.el7.x86_64                                                                              5/7   Cleanup    : linux-firmware-20180911-69.git85c5d90.el7.noarch                                                                6/7   Cleanup    : kernel-tools-libs-3.10.0-957.el7.x86_64                                                                         7/7   Verifying  : kernel-3.10.0-1062.1.1.el7.x86_64                                                                               1/7   Verifying  : kernel-tools-3.10.0-1062.1.1.el7.x86_64                                                                         2/7   Verifying  : kernel-tools-libs-3.10.0-1062.1.1.el7.x86_64                                                                    3/7   Verifying  : linux-firmware-20190429-72.gitddde598.el7.noarch                                                                4/7   Verifying  : kernel-tools-libs-3.10.0-957.el7.x86_64                                                                         5/7   Verifying  : linux-firmware-20180911-69.git85c5d90.el7.noarch                                                                6/7   Verifying  : kernel-tools-3.10.0-957.el7.x86_64                                                                              7/7  Installed:   kernel.x86_64 0:3.10.0-1062.1.1.el7  Updated:   kernel-tools.x86_64 0:3.10.0-1062.1.1.el7                     kernel-tools-libs.x86_64 0:3.10.0-1062.1.1.el7  Dependency Updated:   linux-firmware.noarch 0:20190429-72.gitddde598.el7  Complete!

Now our task of update kernel was successful. We can verify the same using below command. As you see now we have two versions of kernel rpm installed.

# rpm -qa | grep kernel kernel-tools-3.10.0-1062.1.1.el7.x86_64 kernel-tools-libs-3.10.0-1062.1.1.el7.x86_64 abrt-addon-kerneloops-2.1.11-52.el7.x86_64 kernel-3.10.0-1062.1.1.el7.x86_64 kernel-3.10.0-957.el7.x86_64

Currently my system is still running with older kernel version

# uname  -r 3.10.0-957.el7.x86_64

But as you see post reboot the RHEL Linux node is planned to come up with newly installed kernel (3.10.0-1062.1.1.el7.x86_64). If you have installed multiple version of kernel then you can also switch between different kernel version to change the boot order in RHEL 7 and RHEL 8 both.

# grep saved /boot/grub2/grubenv saved_entry=Red Hat Enterprise Linux Server (3.10.0-1062.1.1.el7.x86_64) 7.6 (Maipo)

ALSO READ:

2 ways to boot with old kernel version in RHEL 8 using grubby

Now we will reboot our RHEL 7 Linux host. As expected the node has come up with new kernel

# uname -r 3.10.0-1062.1.1.el7.x86_64

And we still have both version of kernel installed on our node.

# rpm -qa | grep kernel kernel-tools-3.10.0-1062.1.1.el7.x86_64 kernel-tools-libs-3.10.0-1062.1.1.el7.x86_64 abrt-addon-kerneloops-2.1.11-52.el7.x86_64 kernel-3.10.0-1062.1.1.el7.x86_64 kernel-3.10.0-957.el7.x86_64

For all the available kernel, we will also have mapped initramfs and vmlinuz file under /boot

# ls -l /boot/ total 163328 -rw-r--r--. 1 root root   152980 Aug 14 00:14 config-3.10.0-1062.1.1.el7.x86_64 -rw-r--r--. 1 root root   151918 Oct  5  2018 config-3.10.0-957.el7.x86_64 drwx------. 3 root root     4096 Jun 28  2018 efi drwx------. 5 root root     4096 Sep 12 23:13 grub2 -rw-------. 1 root root 74302690 Jul 11 12:25 initramfs-0-rescue-22ee5ba282da4d6ca9bfead578a8fe21.img -rw-------. 1 root root 32195401 Sep 12 23:13 initramfs-3.10.0-1062.1.1.el7.x86_64.img -rw-------. 1 root root 32608707 Jul 11 12:28 initramfs-3.10.0-957.el7.x86_64.img drwx------. 2 root root    16384 Jul 11 12:09 lost+found -rw-r--r--. 1 root root   318717 Aug 14 00:15 symvers-3.10.0-1062.1.1.el7.x86_64.gz -rw-r--r--. 1 root root   314036 Oct  5  2018 symvers-3.10.0-957.el7.x86_64.gz -rw-------. 1 root root  3595191 Aug 14 00:14 System.map-3.10.0-1062.1.1.el7.x86_64 -rw-------. 1 root root  3543471 Oct  5  2018 System.map-3.10.0-957.el7.x86_64 -rwxr-xr-x. 1 root root  6635920 Jul 11 12:25 vmlinuz-0-rescue-22ee5ba282da4d6ca9bfead578a8fe21 -rwxr-xr-x. 1 root root  6734128 Aug 14 00:14 vmlinuz-3.10.0-1062.1.1.el7.x86_64 -rwxr-xr-x. 1 root root  6635920 Oct  5  2018 vmlinuz-3.10.0-957.el7.x86_64

Now since we know our system has installed successfully with newer kernel, so now we can safely remove the old kernel (if required or you can plan to keep it)

# rpm -e kernel-3.10.0-957.el7.x86_64

 

Lastly I hope the steps from the article to update kernel on RHEL/CentOS 7 and 8 Linux was helpful. So, let me know your suggestions and feedback using the comment section.

---