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 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.

Ravi Saive

I am Ravi Saive, creator of TecMint. A Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux. Follow Me: Twitter, Facebook and Google+

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

16 Responses

  1. Ravi Singh says:

    Sequence for kernel compilation are wrong.

    It should be like(Centos7):

    # make
    # make modules_install
    # make install
    
  2. vedula phanindra says:

    In make old config command, it is asking for user inputs in every line. Do we have any command line arguments to pass so that it does not ask for user inputs??

    • Ravi Saive says:

      @Vedula,

      This article is too old and haven’t been updated a while, the latest Kernel is 4.15 and we are in process of updating this article with latest instructions for CentOS/RHEL based distributions. Till then stay tuned for more updates..

  3. kashyap says:

    Hi,
    Is it ok to delete /usr/src/linux3.5 directory after compiling and installing and booting into 3.5 kernel

  4. cris says:

    Hi, I did it by following your instructions but it seems that the kernel version did not change using uname-r…

  5. Rupam Roy says:

    I am trying this on RHEL 6.6 , the `make ` works fine but at the end of running the `make modules_install install` command I get the following error

    DEPMOD 3.5.0
    sh /usr/src/linux-3.5/arch/x86/boot/install.sh 3.5.0 arch/x86/boot/bzImage \
    System.map “/boot”
    ERROR: modinfo: could not find module vsock
    ERROR: modinfo: could not find module vmware_balloon
    ERROR: modinfo: could not find module vmci
    [[email protected] linux-3.5]# uname -r
    2.6.32-504.16.2.el6.x86_64

    Can someone please help

    • Ravi Saive says:

      @Rupam,

      It seems you’re upgrading your Kernel version without rebuilding the vmware drivers. To rebuild the modules type the following command:

      # vmware-modconfig --console --install-all
      

      And then try again to compile your Kernel, hope it will help..

      • Rupam Roy says:

        Hi when i try this command after the zip extraction step it gives me the following error:

        -bash: vmware-modconfig: command not found

        • Rupam Roy says:

          @Ravi – Do you have any updates for me

          • Ravi Saive says:

            @Rupam,

            This article is little outdated, and really don’t know about error you getting, give us some time to update this article with the latest release to fix some bugs.

  6. sumit says:

    Also add, one need to go and edit /etc/grub.conf and set default to the new kernel & reboot.
    Finally uname -r

  7. Munu says:

    Hi,

    I tried upgrading RHEL-6.5 to 3.5 Kernel.

    I got this error:

    cp: accessing `/lib/modules/3.5.0/kernel/sound/usb/usx2y’: Not a directory
    INSTALL sound/usb/usx2y/snd-usb-usx2y.ko
    mkdir: cannot create directory `/lib/modules/3.5.0/kernel/sound’: Not a directory
    cp: accessing `/lib/modules/3.5.0/kernel/sound/usb/usx2y’: Not a directory
    INSTALL /lib/firmware/acenic/tg2.bin
    install: writing `/lib/firmware/acenic/tg2.bin’: No space left on device
    make[1]: *** [/lib/firmware/acenic/tg2.bin] Error 1
    make: *** [_modinst_post] Error 2

    Please let know if we have any work arounds.

    Thank You,
    Munu

    • Samual says:

      Check and make sure you’re using 64 bit OS. The reason is 3.5 kernel supports only 64 bit version. It will not support 32 bit OS.

  8. doujiang24 says:

    I also get the error when install (make modules_install install)
    there only 512RAM, but I set 2GB swap (seems it won’t use swap when I check it via free -m)

    the error: ( hope for your help, thanks very much )

    DEPMOD 3.5.0
    /usr/src/linux-3.5/scripts/depmod.sh: line 37: 8277 Killed “$DEPMOD” “[email protected]” “$KERNELRELEASE”
    make: *** [_modinst_post] Error 137

  9. Thank you for this guide. I just tested this successfully on a CentOS 6.4 system with kernel 3.9.4.

    Apart from the dependencies already mentioned in the article, on a freshly installed minimal CentOS 6.4 I also needed make and bc.

    Note that “make menuconfig” allows you to select the already running kernel’s configuration file (resides in /boot/), and accepts the defaults for all options that were introduced between the old and the new version of the kernel, whereas “make oldconfig” automatically finds the configuration file in /boot/ and asks you questions for all the new configuration options.

    Once again, thanks for the guide!

  10. Benito says:

    Hi

    Upgrading CentOS 6.3 64-bit to 3.5 kernel.

    It is installed on an MSI netbook (wind u135) 1GB of RAM.

    I have successfully completed all the steps up to the 2nd last one .
    When doing a make modules_install install I get :

    Out of memory : Kill process 11891 (depmod) score 861 or sacrifice child
    Killed process 11891, UID 0, (depmod) total-vm:1961888kB, anon-rsss:850832kB,
    file-rss:8kB
    /usr/src/linux-3.5/scripts/depmod.sh: line 37:11891 Killed
    make: *** [modinst_post] Error 137

    Do I need more than 1GB of RAM to install the new kernel ? Any ideas for workarounds ?

    Thank you

Leave a Reply to kashyap Cancel reply

Your email address will not be published. Required fields are marked *

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