Skip to content

Linux Kernel Series

Writing Your First Kernel Module

Hands putting jigsaw pieces together

So we've already built the kernel from source and even modified the source code. While these were some pretty cool parlor tricks, isn't it about time we learned something useful?

Well, I can't promise that, but today we'll take a step in the right direction by building a Linux kernel module.

The Linux kernel is structured so that you can add extended functionality without modifying the core source. These added modules can be linked in as the source is compiled or added dynamically. The one we write today will be inserted dynamically, adding itself and running without a system reboot.

While writing a kernel module is definitely safer than modifying the source, it can still mess up your system a lot more than a user space application, so proceed with caution.

Making Simple Modifications to the Linux Source

Computer Code

The Linux kernel is one of the most complex open-source projects available to the public, and the source code that comprises it is highly intricate, to say the least. Knowing this, I wondered what it would take to pick apart such a technological beast and actually begin to understand it. The key to understanding, like anything else, is to learn by doing. The entire source repository is available to view in a browser, and more than 95% of it is written in C. How hard could it be to write a little C code?

As it turns out, it's harder than expected. Keep reading to get the full rundown on how I edited the kernel source, built it, broke it (several times), and finally made a simple change that produced an output in the system logs.

How to Compile the Linux Kernel from Source

Kernels... Kernels everywhere!

Linux is everywhere, and there are plenty of Linux distributions available providing great out-of-the-box features. But what happens when you need to make an intricate, low-level change to system functionality? What if you wanted to get into kernel development and start to understand one of the world's largest open source projects?

The first step to doing all of these things is compiling the Linux kernel from source. While it may seem tedious or trivial at first, this process will get you down in the weeds of the OS, allowing you to turn modules on or off, search for kernel features, and even examine (and if you're brave, tweak) the raw source code that runs everything on your Linux machine!