How to Compile Linux Kernel on CentOS 7

Running a custom compiled Linux Kernel is always useful, specially when you are looking to enable or disable specific Kernel features, which are not available in default distribution-supplied kernels.

In this article, I will explain how to compile and use latest Linux Kernel from the sources in CentOS 7 distribution (instructions provided here are also works on RHEL and Fedora).

If you don’t want to go through these complex setup, then follow our easy article that explains How to Install or Upgrade to Kernel on CentOS 7 using third-party RPM repository.

Install Required Packages for Kernel Compilation

First, make sure to update your software package repositories, install the development tools needed for compiling a kernel, and install the ncurses library using the following yum command.

# yum update
# yum install -y ncurses-devel make gcc bc bison flex elfutils-libelf-devel openssl-devel grub2

Compile and Install Kernel in CentOS 7

Download the latest Kernel 4.17 sources using wget command under /usr/src/ directory or you can also download the latest kernel by going to kernel.org.

# cd /usr/src/
# wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.17.11.tar.xz

Extract the archived files and change directories using following commands.

# tar -xvf linux-4.17.11.tar.xz
# cd linux-4.17.11/

Configure the Kernel in CentOS 7

The Kernel must be correctly configured with the following required configuration options within the CentOS 7 environment.

CONFIG_KVM_GUEST=y
CONFIG_VIRTIO_PCI=y
CONFIG_VIRTIO_PCI_LEGACY=y
CONFIG_BLK_DEV_SD
CONFIG_SCSI_VIRTIO=y
CONFIG_VIRTIO_NET=y
CONFIG_SERIAL_8250=y
CONFIG_SERIAL_8250_CONSOLE=y

I strongly suggest you to copy the running Kernel configuration (.config) from the /boot directory to new kernel linux-4.17.11 directory.

# cp -v /boot/config-3.10.0-693.5.2.el7.x86_64 /usr/src/linux-4.17.11/.config

Now run the make menuconfig command to configure the Linux kernel. Once you execute the below command a pop up window appears with all the menus. Here you can enable or disable certain kernel features. If you unfamiliar with these menus, just hit ESC key to exit.

# cd /usr/src/linux-4.17.11/
# make menuconfig

Once your kernel configuration options are set, click on Save to save the configuration interface and exit from the menu.

Make Menuconfig
Make Menuconfig
Save Kernel Configuration
Save Kernel Configuration

Compile the Kernel in CentOS 7

Before starting kernel compilation, make sure your system has more than 25GB of free space on the file system. To confirm, you can check the file system free space using df command as shown.

# df -h

Now compile and install the kernel and modules using following commands (it may take several hours). The compilation process place files under /boot directory and also make a new kernel entry in your grub.conf file.

# make bzImage
# make modules
# make
# make install
# make modules_install

Once the compilation completes, reboot the system and verify newly installed Kernel.

# uname -sr
Verify Kernel Version
Verify Kernel Version

That’s it. I hope this article will be much helpful to you all. If you’re facing any problems or difficulties while compiling or installing kernel feel free to ask or post your questions using our comment form below.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

23 thoughts on “How to Compile Linux Kernel on CentOS 7”

  1. Hi Ravi

    Here we are doing this kernel upgrade using ELRepo (kernel-ml), the main intention here is to keep the Linux box safe from all the latest vulnerabilities.

    I google and found that people also do security updates using yum, so my question is do we really need to upgrade the kernel from 3.10 to 5.4 using kernel-ml of ELRepo or just doing security update using yum to existing stable kernel us enough?

    just need your valuable suggestion, as you must have come across such experience

    Regards
    Manoj Raul

    Reply
    • @Manoj,

      I never upgrade kernels, unless any specific application requires a specific kernel version.

      Regular security updates are enough to keep our system safe from vulnerabilities.

      Reply
  2. Hi,

    I able to compile the CentOS 7 kernel from 3.10.0-693.11.6.el7.x86_64 to 5.xxxxx series. Now my question is that to keep our os safe and to avoid new vulnerabilities we are doing yum -y update every 1 month time.

    so my question is, if we upgrade the kernel from say 3.10 to 5.x. Can we still able to do yum -y update or install new software?

    As we use to do on 3.10 kernel?

    Regards
    Manoj Raul

    Reply
      • Thanks, Ravi for your valuable feedback.

        One more question is in mind

        which method is safer and stable as per your experience, if possible provide me pros and cons.

        1) ELRepo, a third-party repository
        2) customized kernel compilation from kernel.org

        Reply
        • @Manoj,

          I think the easier and safest way to install new Kernel is using ELRepo only, as it offers an easy way of upgrade when new kernel releases…

          Installing Kernel from the source is hard because you need to manually compile modules as per your requirements, also it time-consuming process…

          Reply

Leave a Reply to Rupam Roy Cancel reply

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.