A discussion on upgrading the kernel in CentOS 7

Publish: 2021-11-20 | Modify: 2021-11-20

Upgrading the Kernel in CentOS 7

The default kernel version in CentOS 7 is still at 3.x. If certain software requires a higher Linux kernel version, it becomes necessary to upgrade the kernel to meet the requirements. For example, Google's BBR acceleration requires a Linux kernel version greater than 4.9. In this article, we will discuss the topic of upgrading the kernel in CentOS 7.

Linux Kernel

Choosing the Kernel Version

We can download the specific kernel source code from https://www.kernel.org/ and compile it for kernel upgrade, but this step is more complicated and beyond the scope of this discussion.

If we choose to upgrade using rpm packages, we can use pre-compiled packages provided by others. The kernel upgrade packages provided by elrepo are quite reliable. The kernel address is as follows: https://elrepo.org/linux/kernel/el7/x86_64/RPMS/

However, when we open the elrepo website, we can only see two versions, 5.15.x and 5.4.x (this may change over time), labeled as ml and lt.

Kernel Versions

  • ml represents the mainline version, which always keeps up with the latest kernel.
  • lt represents the long-term support version, which has a longer support cycle.

By comparing the lifecycles of these two versions on the https://www.kernel.org official website, we can see that the 5.15 version is supported until 2023, while the 5.4 version is supported until 2025.

Kernel Lifecycles

If you want to pursue the latest version, simply choose the rpm package with ml. If you want a stable version with a longer support cycle, choose the lt version.

Online Upgrade

# Import the ELRepo GPG key
wget https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm --import RPM-GPG-KEY-elrepo.org
# Install ELRepo
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
# Upgrade to the latest kernel
yum --enablerepo=elrepo-kernel install kernel-ml -y
# Upgrade to the long-term support lt kernel (choose either one)
yum --enablerepo=elrepo-kernel install kernel-lt -y

Offline Upgrade

You can also manually download the kernel rpm package for offline upgrade. Here is an example of upgrading to the lt kernel:

# Download the kernel (the link may become invalid over time)
wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-lt-5.4.160-1.el7.elrepo.x86_64.rpm
# Alternative download link
wget https://wget.ovh/linux/kernel/kernel-lt-5.4.160-1.el7.elrepo.x86_64.rpm
# Upgrade the kernel
rpm -ivh kernel-lt-5.4.160-1.el7.elrepo.x86_64.rpm

Switching the Kernel

Use the command awk -F\' '$1=="menuentry " {print i++ " : " $2}' /boot/grub2/grub.cfg to view the grub2 boot menu entries.

[root@test ~]# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /boot/grub2/grub.cfg
0 : CentOS Linux (5.4.160-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-1160.el7.x86_64) 7 (Core)
2 : CentOS Linux (0-rescue-face32e3e9d448e2a0403204f18369d9) 7 (Core)

We can see that entry 0 is the kernel we just installed. Let's set it as the default boot kernel:

# Set the default boot kernel
grub2-set-default 0
# Reboot the server for the changes to take effect
reboot

Some service providers may encounter issues where the setting does not take effect. In that case, we can delete the old kernel and keep the new one. Restart the server and use VNC to connect (consult your service provider) and select the new kernel (5.4) at the boot menu. Use the following method to remove the old kernel:

# Check the currently running kernel to ensure that the new kernel is being used
uname -a
# List all installed kernels
rpm -qa | grep kernel
# Remove the unwanted kernel
yum remove kernel-3.10.0-229.4.2.el7.x86_64

If there is only one kernel in the system, it will be automatically selected as the default boot kernel the next time the system starts.

Note

Upgrading the kernel carries some risks. The above methods do not guarantee a successful kernel upgrade. Exercise caution when performing kernel upgrades in a production environment. If the kernel fails to boot after the upgrade, you can use VNC to connect to the server (consult your service provider), select a working kernel, and delete the problematic kernel to restore the system.

Summary

  • ml represents the mainline version, which always keeps up with the latest kernel.
  • lt represents the long-term support version, which has a longer support cycle.
  • If the kernel upgrade fails, you can use VNC to select a working kernel and delete the problematic kernel for recovery.
  • Elrepo regularly removes old kernels, so it is recommended to periodically download and save the kernel from the elrepo website for future use.

The above content is referenced from: Linux Kernel 5.14.x Compilation Edition


Comments