How to Install Kernel Headers in CentOS 7

When you compile a custom kernel module such as a device driver on a CentOS system, you need to have kernel header files installed on the system, which include the C header files for the Linux kernel. Kernel header files provide different kinds of function and structure definitions required when installing or compiling any code that interfaces with the kernel.

When you install Kernel Headers, make sure it matches with the currently installed kernel version on the system. If your Kernel version comes with the default distribution installation or you have upgraded your Kernel using yum package manager from system base repositories, then you must install matching kernel headers using package manager only. If you’ve compiled Kernel from sources, you can install kernel headers from sources only.

Read Also: How to Install Kernel Headers in Ubuntu and Debian

In this article, we will explain how to install Kernel Headers in CentOS/RHEL 7 and Fedora distributions using default package manager.

Install Kernel Headers in CentOS 7

First confirm that the matching kernel headers are already installed under /usr/src/kernels/ location on your system using following commands.

# cd /usr/src/kernels/
# ls -l
Check Kernel Headers in CentOS 7
Check Kernel Headers in CentOS 7

If no matching kernel headers are located in the /usr/src/kernels/ directory, go ahead and install kernel headers, which is provided by the kernel-devel package that can be installed using default package manager as shown.

# yum install kernel-devel   [On CentOS/RHEL 7]
# dnf install kernel-devel   [On Fedora 22+]
Install Kernel Headers in CentOS 7
Install Kernel Headers in CentOS 7

After installing the kernel-devel package, you can find all the kernel headers files in /usr/src/kernels directory using following command.

# ls -l /usr/src/kernels/$(uname -r) 

Note on a VPS (for instance a Linode VPS), a kernel may have a customized version name, in such scenario, you have to identify the kernel version manually and check the installed kernel header files using following commands.

# uname -r	
# ls -l /usr/src/kernels/3.10.0-862.2.3.el7.x86_64
Check Kernel Version in CentOS 7
Check Kernel Version in CentOS 7
Sample Output
total 4544
drwxr-xr-x.  32 root root    4096 May 16 12:48 arch
drwxr-xr-x.   3 root root    4096 May 16 12:48 block
drwxr-xr-x.   4 root root    4096 May 16 12:48 crypto
drwxr-xr-x. 119 root root    4096 May 16 12:48 drivers
drwxr-xr-x.   2 root root    4096 May 16 12:48 firmware
drwxr-xr-x.  75 root root    4096 May 16 12:48 fs
drwxr-xr-x.  28 root root    4096 May 16 12:48 include
drwxr-xr-x.   2 root root    4096 May 16 12:48 init
drwxr-xr-x.   2 root root    4096 May 16 12:48 ipc
-rw-r--r--.   1 root root     505 May  9 19:21 Kconfig
drwxr-xr-x.  12 root root    4096 May 16 12:48 kernel
drwxr-xr-x.  10 root root    4096 May 16 12:48 lib
-rw-r--r--.   1 root root   51205 May  9 19:21 Makefile
-rw-r--r--.   1 root root    2305 May  9 19:21 Makefile.qlock
drwxr-xr-x.   2 root root    4096 May 16 12:48 mm
-rw-r--r--.   1 root root 1093137 May  9 19:21 Module.symvers
drwxr-xr-x.  60 root root    4096 May 16 12:48 net
drwxr-xr-x.  14 root root    4096 May 16 12:48 samples
drwxr-xr-x.  13 root root    4096 May 16 12:48 scripts
drwxr-xr-x.   9 root root    4096 May 16 12:48 security
drwxr-xr-x.  24 root root    4096 May 16 12:48 sound
-rw-r--r--.   1 root root 3409102 May  9 19:21 System.map
drwxr-xr-x.  17 root root    4096 May 16 12:48 tools
drwxr-xr-x.   2 root root    4096 May 16 12:48 usr
drwxr-xr-x.   4 root root    4096 May 16 12:48 virt
-rw-r--r--.   1 root root      41 May  9 19:21 vmlinux.id

In addition, if you need header files for the Linux kernel for use by glibc, install the kernel-header package using following command.

# yum install kernel-headers   [On CentOS/RHEL 7]
# dnf install kernel-headers   [On Fedora 22+]

Now you are good to go with compiling your own or existing kernel modules for software such as VirtualBox and many more.

That’s it! In this article, we have explained how to install kernel-devel and kernel-header packages in CentOS/RHEL 7 and Fedora systems. Remember that before you can compile kernel modules such as device driver on a Linux system, you should have necessary kernel header files installed. If you have queries, please use the comment form below to reach us.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

7 Comments

Leave a Reply
  1. What is the font name used on screens: “Check Kernel Headers in CentOS 7” and “Install Kernel Headers in CentOS 7”? It’s really nice.

    Reply
  2. This is the error I get when i run make menuconfig command.

    HOSTCC scripts/basic/fixdep
    In file included from /usr/include/bits/posix1_lim.h:160:0,
    from /usr/include/limits.h:144,
    from /usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/limits.h:168,
    from /usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/syslimits.h:7,
    from /usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/limits.h:34,
    from scripts/basic/fixdep.c:114:
    /usr/include/bits/local_lim.h:38:26: fatal error: linux/limits.h: No such file or directory
    #include
    ^
    compilation terminated.
    make[1]: *** [scripts/basic/fixdep] Error 1
    make: *** [scripts_basic] Error 2

    Reply
  3. I’m installing the kernel headers as a requirement for VirtualBox Machine Additions.

    This is also expecting the /build tree, but this isn’t installed with kernel-devel nor kernel-headers.

    Any tough on how to accomplish this in a straight forward way ?

    Reply
    • @Erik,

      Which Linux distribution you are using? and also have you install it using your package manager as shown.

      # yum install kernel-headers kernel-devel
      OR
      # dnf install kernel-headers kernel-devel
      
      Reply
      • @Ravi,

        I’m using CentOS 7.6.1810

        This is my install script :

        yum install deltarpm wget -y
        yum update -y
        yum install epel-release -y
        cd /etc/yum.repos.d/
        wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo
        yum groupinstall "Development Tools" -y
        yum install kernel-devel -y
        yum --enablerepo=epel install perl gcc dkms kernel-headers make bzip2 mc nano -y
        

        I also tried installing headers first and devel later, but this makes no difference.

        Everything gets installed, but there is no build folder.

        (If you look at the screenshot in above article, there is no Build folder)

        Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.