- What does a kernel source tree contain? Is this related to Linux kernel headers?
- 1 Answer 1
- How to install kernel headers on Debian
- Installing kernel headers on Debian 11:
- What is the Linux kernel?
- What are Linux Kernel Headers?
- Conclusion:
- About the author
- David Adams
- How to Install Kernel Headers in Ubuntu and Debian
- Install Kernel Headers in Ubuntu and Debian
What does a kernel source tree contain? Is this related to Linux kernel headers?
My question may be naive, but I’m a bit confused. Is (2) what I should expect to see in the Kernel Source Tree; and why do I have the explicit reference to ‘headers’? I needed to install linux-generic-headers a while back for some other software and am unsure if this might be related. I realize there is good reason for the makefiles (eg, to install modules in the /driver sub-directory), but (pretty much) only makefiles?
1 Answer 1
Distribution kernel-header packages contain, as their name implies, only kernel header files (plus the necessary plumbing) that are required to build software like kernel modules.
You shouldn’t expect to find binary files at all in a kernel source directory, except for build output. (If you configure and build a kernel yourself, the kernel source directory will also contain the compiled objects, modules, the built kernel itself and a few other binary bits and pieces that make it work.)
KConfig files are a description of the kernel configuration options (and their dependencies) that are available for a given directory/module.
Apart from that, it’s all (mostly) C source code, header files and Makefile s. There are a few helper scripts here and there, and assembly source too.
Header packages (what you installed) only contain the header part of the above (and not all of that — only the «exported» headers), and some of the build infrastructure. So what you are seeing is expected. Header packages do not contain C source code (except for some stubs and build infrastructure code). The whole point of having this type of package is to save space (and bandwidth) — the whole Linux kernel source tree is rather large, and completely unnecessary if you don’t intend to compile the kernel yourself. The header packages are built and shipped by distributions to provide just the right things necessary to build modules, but no more. (They certainly do not contain the compiled kernel.)
Addressing your comment: header packages don’t relocate anywhere. They are built for specific versions of the kernel, packaged in a specific directory, and that’s that. It’s just a set of files. (Note that the header packages don’t necessarily have the same version as the current stable kernel binary packages — the header packages are generic, and can lag behind the actual kernel you’re running. They should not, however, be from a kernel version that is more recent than the current installed (or target) kernel.)
Installed kernel binaries are usually installed in the /boot directory, along with bootloader binaries and configuration files. (This is sometimes an independent filesystem, not mounted by default.) The exact name of the files depends on the kernel and distribution. (So does the bootloader.)
Installed kernel modules reside in sub-directories of:
So for instance on my system, they are currently in
Full kernel source code: On Ubuntu, if you want the full kernel sources to build a kernel yourself, you should install following the instructions here.
You could also download a source tarball from kernel.org and unpack it somewhere (do not overwrite Ubuntu-installed files if you use this tarball, keep your personal stuff and the stuff managed by RPM separate).
/usr/src/linux is a traditional place to put kernel sources, but nothing prevents you from putting kernel sources elsewhere. This path is also often just a symbolic link to a directory. e.g. I have this on my machine:
$ ls -l /usr/src/linux lrwxrwxrwx 1 root root 18 Dec 7 17:03 /usr/src/linux -> linux-3.1.4-gentoo
The symlink is there to simplify building applications that depend on the kernel source. You link that path to your running (or target) kernel so that you don’t have to specify exact version or path information when you build a module out-of-tree. Helps a bunch for source-based distributions at least.
How to install kernel headers on Debian
All instructions in this tutorial are also useful for other Debian-based Linux distributions such as Ubuntu.
Installing kernel headers on Debian 11:
First, update the information on your software using apt, as shown in the example below.
Check for available Linux kernel headers by running the following command.
For a different simple way to list available headers, run the command below.
To install the Linux kernel headers, run the command shown in the screenshot below.
Using Debian 11 x64 or using the second method to find the header, you can also run the following command.
In my case, the headers were installed in the previous example, so the output says it’s already properly installed.
What is the Linux kernel?
Some experts say the kernel is the core component of an Operating System; others disagree and say the kernel is not part of the OS but a vital component needed by any OS. The function of the kernel is to manage the device resources for them to interact with the operating system and applications. We can think about the kernel as a translator allowing communication between the hardware and the software. In order to manage resources properly, the kernel has full privileges. In some cases, the installed kernel may lack the proper code to allow the hardware and software interaction; that’s when we need to install kernel headers.
What are Linux Kernel Headers?
Linux kernel headers are components usually used to compile drivers and loadable modules adding support to the kernel. For this purpose, kernel headers include C headers with functions and structures needed to compile modules. Thanks to kernel headers, modules or drivers can interact with the kernel. It is important to clarify the kernel headers are part of the kernel, despite being distributed separately.
Dealing with kernel headers is easier than dealing with whole kernel sources. If you are looking for drivers for a device, you’ll use kernel headers, while to rebuild the whole kernel, you will need whole kernel sources. By default, some Linux distributions, including Debian, include the kernel headers to be used if needed (this tutorial assumes you discarded or removed the headers). If all your hardware works properly, you probably won’t need to deal with kernel headers.
Conclusion:
As you can see, installing Linux kernel headers on Debian or Ubuntu is pretty easy. Any Linux-level user should know the function of kernel headers and how to install them. Users must pay attention to the kernel headers they download that are compatible with their system, not only the architecture but also the version.
Thank you for reading this tutorial explaining how to install kernel headers on Debian. Keep following Linux Hint for additional Linux tips and tutorials.
About the author
David Adams
David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.
How to Install Kernel Headers in Ubuntu and Debian
In our last article, we have explained how to install kernel headers in CentOS 7. Kernel Headers contain the C header files for the Linux kernel, which offers the various function and structure definitions required when compiling any code that interfaces with the kernel, such as kernel modules or device drivers and some user programs.
It is very important to note that the kernel headers package you install should match with the currently installed kernel version on your system. If your kernel version ships with the default distribution installation or you have upgraded your Kernel using dpkg or apt package manager from the Ubuntu or Debian base repositories, then you must install matching kernel headers using package manager only. And if you’ve compiled kernel from sources, you must also install kernel headers from sources.
In this article, we will explain how to install Kernel Headers in Ubuntu and Debian Linux distributions using default package manager.
Install Kernel Headers in Ubuntu and Debian
First check your installed kernel version as well as kernel header package that matches your kernel version using following commands.
$ uname -r $ apt search linux-headers-$(uname -r)
On Debian, Ubuntu and their derivatives, all kernel header files can be found under /usr/src directory. You can check if the matching kernel headers for your kernel version are already installed on your system using the following command.
$ ls -l /usr/src/linux-headers-$(uname -r)
From the above output, it’s clear that the matching kernel header directory doesn’t exist, meaning the package is not yet installed.
Before you can install the appropriate kernel headers, update your packages index, in order to grab information about the latest package releases, using the following command.
Then run the following command that follows to install the Linux Kernel headers package for your kernel version.
$ sudo apt install linux-headers-$(uname -r)
Next, check if the matching kernel headers have been installed on your system using the following command
$ ls -l /usr/src/linux-headers-$(uname -r)
That’s all! In this article, we have explained how to install kernel headers in Ubuntu and Debian Linux and other distributions in the Debian family tree.
Always keep in mind that to compile a kernel module, you will need the Linux kernel headers. If you have any quires, or thoughts to share, use the comment form below to reach us.