Your First Linux Kernel Module
Build, load and inspect a working kernel module from scratch. Understand kernel space vs user space, the module lifecycle, and why a bug here crashes the whole machine.
Writing a kernel module is the fastest way to feel the difference between application code and kernel code. Let's build one.
Kernel space vs user space
Your normal programs run in user space: isolated, each with its own virtual memory, unable to touch hardware directly, and killable without consequence. The kernel runs in a single privileged address space with full hardware access. A module is code you load into that privileged space at runtime — which is exactly why it's powerful and dangerous.
The smallest real module
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("you");
MODULE_DESCRIPTION("A first module");
static int __init hello_init(void)
{
pr_info("hello: module loaded\n");
return 0; /* 0 = success; nonzero aborts the load */
}
static void __exit hello_exit(void)
{
pr_info("hello: module unloaded\n");
}
module_init(hello_init);
module_exit(hello_exit);Things that are different from normal C already:
- No
main()— you register an init and an exit function instead. - No libc —
pr_infoinstead ofprintf; the kernel has its own API. MODULE_LICENSEis mandatory; omit it and the kernel marks itself "tainted" and withholds some symbols.__init/__exitare hints letting the kernel free that code after it's used.
Build it with a Kbuild Makefile
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) cleanYou need the headers for your running kernel (apt install linux-headers-$(uname -r)), then:
makeThis produces hello.ko — a kernel object.
Load, inspect, unload
sudo insmod hello.ko # load it
lsmod | grep hello # confirm it's resident
modinfo hello.ko # metadata: license, author, deps
sudo dmesg | tail # see your pr_info() output
sudo rmmod hello # unload itWatch dmesg and you'll see "module loaded" on insert and "module unloaded" on removal. You just ran code inside the kernel.
The lifecycle, conceptually
insmod ──▶ module_init() runs ──▶ module is live, its code callable
│
rmmod ──▶ module_exit() runs ──▶ cleanup ──▶ memory reclaimedThe golden rule: whatever you allocate or register in init, you must release in exit. There is no garbage collector and no process teardown to clean up after you. A module that registers a handler and forgets to unregister it on exit leaves a dangling pointer the kernel will later jump into — an instant crash.
Where to go next
From here the interesting paths are: exposing a /proc or /sys entry, writing a character device with file_operations, or handling an interrupt. Each one teaches a core kernel API. But the mental shift — no libc, no safety net, manual cleanup, global consequences — is the thing you just learned, and it carries through all of them.
Takeaways
- Modules load C code into the kernel's privileged space at runtime.
module_init/module_exitreplacemain(); the kernel API replaces libc.- Everything you set up in init must be torn down in exit — there's no cleanup for you.
- Always develop in a disposable VM; kernel bugs take down the whole system.