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