Write Your First Linux Kernel Module: Hello World LKM Step by Step
Build, load and unload a Hello World Linux kernel module. Covers module_init, module_exit, printk, the Kbuild Makefile, parameters and safe testing in a VM.
Published November 27, 2026
Writing a kernel module sounds intimidating, but the simplest one is about fifteen lines of C. Building it teaches you how the kernel loads code, how kernel logging works, and why kernel programming has rules you never meet in user space. Do this in a virtual machine: a mistake in kernel code can crash the whole system.
The concept
A module needs two entry points: an init function that runs when the module is loaded and an exit function that runs when it is removed. You register them with the module_init() and module_exit() macros. Instead of printf you use printk (or the pr_info family), which writes to the kernel ring buffer that dmesg reads.
Kernel code has no standard C library. There is no malloc (use kmalloc), no printf, no floating point by default, and a small fixed stack (typically 16 KiB). An invalid pointer dereference does not give you a segfault; it gives you a kernel oops. That is why every example below runs in a disposable VM.
The workflow
- Install kernel headers and build tools for the running kernel
- Write hello.c with init and exit functions
- Write a one-line Kbuild Makefile
- make builds hello.ko against /lib/modules/$(uname -r)/build
- insmod, check dmesg, rmmod, check dmesg again
The source code
MODULE_LICENSE is not optional decoration. Without a GPL-compatible license the kernel taints itself and refuses to let your module use GPL-only symbols.
// hello.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
static char *who = "world";
module_param(who, charp, 0444);
MODULE_PARM_DESC(who, "Who to greet");
static int __init hello_init(void)
{
pr_info("hello: Hello, %s!\n", who);
return 0; /* non-zero would abort the load */
}
static void __exit hello_exit(void)
{
pr_info("hello: Goodbye, %s!\n", who);
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("DevStudio");
MODULE_DESCRIPTION("Hello world kernel module");The Makefile
The kernel has its own build system (Kbuild). Your Makefile simply hands control to the kernel source tree, telling it where your module lives with M=.
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) cleanMakefile recipes must be indented with a real tab character, not spaces.
Build and run it
Practical examples
Watch the kernel log live
Keep this running in a second terminal while you test.
sudo dmesg -wH
# or with systemd
journalctl -k -fExpose a file in /proc
A tiny step further: a read-only /proc/hello entry using seq_file.
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
static int hello_show(struct seq_file *m, void *v)
{
seq_printf(m, "Hello from the kernel, jiffies=%lu\n", jiffies);
return 0;
}
/* in init: proc_create_single("hello", 0444, NULL, hello_show); */
/* in exit: remove_proc_entry("hello", NULL); */Sign the module for Secure Boot
Create a MOK key, enroll it once, then sign every build.
openssl req -new -x509 -newkey rsa:2048 -nodes -days 3650 \
-subj "/CN=My module key/" -keyout MOK.priv -outform DER -out MOK.der
sudo mokutil --import MOK.der # reboot and enroll
sudo /usr/src/linux-headers-$(uname -r)/scripts/sign-file sha256 MOK.priv MOK.der hello.koCommon mistakes
- Testing kernel code on your main machine instead of a VM.
- Returning a non-zero value from init by accident, which makes insmod fail with an error.
- Forgetting to free resources in the exit function, leaking memory or leaving dangling /proc entries that crash on access.
- Using spaces instead of a tab in the Makefile ("missing separator").
- Building against headers that do not match the running kernel.
Key takeaways
- A module needs init and exit functions registered with module_init and module_exit.
- Use pr_info/printk and read output with dmesg.
- Build with a Kbuild Makefile that calls into /lib/modules/$(uname -r)/build.
- Always declare MODULE_LICENSE and test in a disposable VM.
FAQ
Can I write kernel modules in Rust?
Yes. Mainline Linux has had Rust support since 6.1, and an increasing number of drivers use it, though C is still the default for most subsystems.
What is the difference between printk and pr_info?
pr_info(fmt) is shorthand for printk(KERN_INFO fmt). The pr_* helpers also support a pr_fmt prefix so every message from your module is tagged.
Why does my module taint the kernel?
Out-of-tree modules set the O taint flag and unsigned ones set E. It is informational only and does not affect functionality.