How to Upgrade the Linux Kernel on CentOS 7

The kernel is the core of an operating system. The Linux kernel is the monolithic Unix-like kernel of the Linux computer operating system. It was created by Linux Torvalds, and all Linux distributions including Ubuntu, CentOS and Debian are based on this kernel - the Linux kernel.

In this tutorial, I will show you how to upgrade the CentOS 7 kernel to the latest version. We will use a precompiled kernel from the ELRepo repository. By default CentOS 7 uses the kernel 3.10. In this manual we will install the latest stable kernel version 5.0.11.

What is the ELRepo

ELRepo is a community-based repository for Enterprise Linux and supports for RedHat Enterprise (RHEL) and other distribution based on it (CentOS, Scientific, and Fedora).

ELRepo has the focus on packages related to hardware, including filesystem drivers, graphic drivers, network drivers, sound card drivers, webcam, and others.

What we will do:

  1. Update and Upgrade CentOS 7
  2. Checking the Kernel Version
  3. Add ELRepo Repository
  4. Install New Kernel Version
  5. Configure Grub2
  6. Remove Old Kernel

Step 1 - Update and Upgrade CentOS 7

The first thing we must do before upgrading the kernel is to upgrade all packages to the latest version. Update the repository and all packages to their latest versions with yum command below.

yum -y update

Now install yum plugin to get faster when installing and updating packages.

yum -y install yum-plugin-fastestmirror

CentOS 7 System updated and all packages upgraded to latest versions.

Update all packages

Step 2 - Checking the Kernel Version

In this tutorial, we will use CentOS 7.6 with default kernel 3.10. Check your CentOS version with commands below.

cat /etc/redhat-release
cat /etc/os-release

You will get the system info as below.

Check CentOS release and Kernel version

For checking the kernel version, you can also use the uname command.

uname -snr

And you get the result about Linux kernel version and the system architecture.

Check the CentOS Kernel version

Step 3 - Add ELRepo Repository

Before installing the new kernel version, we need to add a new repository - the ELRepo repository.

Add ELRepo gpg key to the system.

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

Now add new ELRepo repository with rpm command.

rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

When it's complete, check all repository enabled on the system, and make sure ELRepo is on the list.

yum repolist

Add ELRepo Repository

ELRepo repository has been added to CentOS 7 server.

Step 4 - Install New CentOS Kernel Version

In this step, we will install a new latest kernel from ELRepo repository, kernel version 5.0.11 - the Latest stable version on kernel.org.

Install ELRepo kernel version with yum command below.

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

--enablerepo is an option to enable specific repository on CentOS system. By default, 'elrepo' repository is enabled, but not for the 'elrepo-kernel' repository.

Install Linux Kernel 5.0 on CentOS 7

You can check all of the available repositories on the system, enabled and disabled repository with the command below.

yum repolist all

List CentOS 7 repositories, enabled and disabled repositories.

List CentOS Repositories

Step 5 - Configure Grub2 on CentOS 7

At step 4, we've already installed a new kernel 5.0.11 to the system. Now, I will show you how to configure the default kernel version to load when the system is starting.

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

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

You see that we have two kernel version - 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.

Next, generate the grub2 config with 'gurb2-mkconfig' command, then reboot the server.

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

Configure default Kernel in Grub2

Please login to the server again, and check currently kernel usage.

uname -snr

Check the Kernel Version

You get a result kernel version 5.0.11 is being used on your system.

Step 6 - Remove an Old Kernel (Optional)

This is an optional step that is useful to get more free space. In this step, I will show you how to remove an old kernel from your CentOS 7 system. This should be done when you have a more than 3 or 5 kernel versions installed on the server.

For this purpose, we need to install the yum-utils utility from the repository.

yum install yum-utils

Now clean your old kernel with command below.

package-cleanup --oldkernels

If you get the result below.

Securely remove old Kernel on CentOS

That means you've only 2 or 3 Kernel versions installed. If you have more than 3 versions installed, the command will automatically remove the old kernel from your system.

CentOS 7 Kernel has been updated to the latest stable using ELRepo Kernel Version.

Reference

    Share this page:

    Suggested articles

    17 Comment(s)

    Add comment

    Comments

    By: Andy at: 2018-08-22 13:59:54

    How to install headers and devel for Kernel 4.18? How to uninstall 3.10 headers?

    By: Andy at: 2018-08-22 18:47:15

    Considering this URL to answer my question: https://qiita.com/tkprof/items/2ec4ed58790efba3778d

    By: Andrew at: 2019-01-19 01:10:37

    Thanks!!!! that's the other part I needed for my install.

     

    And thanks Muhammad Arul for the origional writeup!!!

    By: asysliasa at: 2019-04-05 23:07:10

    With this everything is done :D Thank you

    By: scorpio at: 2018-09-09 15:28:34

    Thanks for the above instruction, you did a good job telling us how, but for what reason would we want to upgrade?

    By: till at: 2018-09-10 10:04:20

    A Kernel upgrade might be required when you want to use new hardware that the old kernel does not support or when you need functions that are not available in the old Kernel.

    By: chris at: 2018-11-30 13:11:59

    Thanks for the above instructions.

     

    By: Michael at: 2018-12-05 21:47:01

    Thank you! Very useful information and steps. In Step 3, where it has the following, check first in http://www.elrepo.org for more updated release

    rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rp

    By: Rea at: 2019-02-17 18:51:51

    Thank you for the writeup ... I tried but I get the error: [[email protected] ~]# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfgawk: fatal: cannot open file `/etc/grub2.cfg' for reading (No such file or directory)#

    /boot is empty except for the ElRepo files and the two empty folders grub2 and efi

    I thought in /boot there should be a config file?

    Can you help?

    By: Pedro Salazar at: 2019-03-01 05:50:38

    Thanks my friend, Hello from Venezuela

    By: raiden at: 2019-04-21 09:40:25

    I am stuck at step 5, my linux doesn't have any boot loader intalled

    By: Ralph at: 2019-04-25 12:16:35

    Thanks for the instructions. Slight type here though: "Next, generate the grub2 config with 'gurb2-mkconfig' command, and then reboot the server."

    By: Hasan at: 2019-05-16 18:15:35

    Hi Guys,

    Need your help urgently, I tried following the instruction given above and  the server is not booting now. The grub.cfg file is corrputed. 

     

    [[email protected] bin]# grub2-mkconfig -o /boot/grub2/grub.cfg

    No path or device is specified. Usage: grub2-probe [OPTION...] [OPTION]... [PATH|DEVICE] Try 'grub2-probe --help' or 'grub2-probe --usage' for more information. No path or device is specified. Usage: grub2-probe [OPTION...] [OPTION]... [PATH|DEVICE] Try 'grub2-probe --help' or 'grub2-probe --usage' for more information. No path or device is specified. Usage: grub2-probe [OPTION...] [OPTION]... [PATH|DEVICE] Try 'grub2-probe --help' or 'grub2-probe --usage' for more information.

    By: trevor getty at: 2019-07-02 16:29:53

    The epel link for the 7.0.2 elrepo is now longer valid. If you try that link, it now contains a redirect which will not be followed by the rpm command. Instead use:

    http://elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

    By: Fernando at: 2019-07-30 18:07:10

    Thanks Muhammad Arul for the tutorial. It helps me a lot. And thanks Trevor Getty for your actualization, it was very usefull too. 

    By: kevin at: 2019-08-26 14:46:43

    hello , do you kwno when I can find the lastest version of Centos Version ??

    I try search it in centos documentation

    By: Bout-chou at: 2019-09-07 11:34:20

    Thanks very much, really helpfull