Write Your First Linux Kernel Module: Hello World in C
Write, build and load your first Linux kernel module in C: the init and exit functions, MODULE_LICENSE, a Makefile and insmod, with the exact dmesg output.

What a module is
A loadable kernel module is a piece of kernel code that is linked into the running kernel on demand and can be removed again. Drivers are the common case, but a module can be anything that runs in kernel mode: a new file system, a network protocol, a tracing helper. This post writes the smallest useful one, the "hello world" of kernel programming, builds it and loads it, and explains every line, because each one exists for a reason.
You need a machine you can safely experiment on, root access, a C compiler and the headers for your running kernel. The code targets Linux 6.12 LTS and GCC 14, and it follows the kernel module documentation. A mistake in kernel code can crash the machine, so use a virtual machine if you can.
The source
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
static int __init hello_init(void)
{
pr_info("hello: module loaded\n");
return 0;
}
static void __exit hello_exit(void)
{
pr_info("hello: module unloaded\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A minimal hello-world kernel module");
MODULE_VERSION("0.1");There is no main. A module does not start at an entry point you name; it registers two callbacks, and the kernel calls them at the right moments.
The headers
linux/module.h provides module_init, module_exit and the MODULE_* macros. linux/init.h provides the __init and __exit annotations. linux/kernel.h is the older home of many helpers, and recent kernels need it less, but it is harmless here. All of them come from the kernel's own include/ directory, not from the C library, which the post on libc explained is unavailable.
__init and __exit
__init marks a function used only during initialization. For code built into the kernel, the build puts it in a section that the kernel frees after boot. For a module, it is an annotation with little effect, and the habit is worth keeping. __exit marks cleanup code that is dropped entirely if the code is built in, since built-in code can never be unloaded.
pr_info
pr_info writes a message to the kernel log at the info level. It is a macro around printk, and the kernel prepends a timestamp. Kernel messages end with a newline, because the log is line-oriented. There is no printf here, as the next sections show by what appears in the log rather than on your terminal.
Return values
hello_init returns 0 for success. A negative errno such as -ENOMEM tells the kernel the load failed, and insmod reports the error. If the init function fails, the exit function is not called, so any resource acquired before a failure point must be released before returning.
The registration macros
module_init(hello_init) records which function runs at load, and module_exit(hello_exit) records which runs at removal. For a built-in driver, module_init instead places the function in an initcall section, and the same source works in both cases, which is why drivers use the macro rather than a fixed name.
MODULE_LICENSE
This one is not decoration. The kernel exports many symbols only to modules that declare a GPL-compatible license, through EXPORT_SYMBOL_GPL. A module without a recognized license taints the kernel (a flag that appears in oops reports and tells developers the kernel is not fully open source) and cannot use those symbols. Use "GPL" for GPL-2.0 code, and read the license identifiers in include/linux/module.h for the other accepted strings.
Build it
Create the Makefile from the earlier post on out-of-tree builds:
obj-m += hello.o
KDIR ?= /lib/modules/$(shell uname -r)/build
all:
$(MAKE) -C $(KDIR) M=$(CURDIR) modules
clean:
$(MAKE) -C $(KDIR) M=$(CURDIR) cleanmakemake -C /lib/modules/6.12.9-amd64/build M=/home/user/hello modules
make[1]: Entering directory '/usr/src/linux-headers-6.12.9-amd64'
CC [M] /home/user/hello/hello.o
MODPOST /home/user/hello/Module.symvers
CC [M] /home/user/hello/hello.mod.o
LD [M] /home/user/hello/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-6.12.9-amd64'The paths and version differ on your machine. The product is hello.ko, a relocatable ELF object with extra sections for module metadata.
Load, observe, unload
sudo insmod hello.ko
lsmod | grep hello
sudo rmmod hello
sudo dmesg | tail -n 2hello 12288 0
[ 9120.481202] hello: module loaded
[ 9128.003987] hello: module unloadedlsmod prints the module name, its size in bytes and its use count. The size and timestamps will differ. The two kernel messages show the callbacks ran in order. If dmesg needs privileges on your system (kernel.dmesg_restrict is set on many distributions), use sudo as shown or journalctl -k.
Common failures
**insmod: ERROR: could not insert module hello.ko: Invalid module format.** The module was built against a different kernel than the running one. Check with modinfo hello.ko that vermagic equals uname -r.
**Operation not permitted.** You are not root, or module loading is locked: Secure Boot with lockdown refuses unsigned modules.
**module verification failed: signature and/or required key missing - tainting kernel.** The kernel enforces or logs unsigned modules. On a test machine, a kernel without forced signature checking loads it with this notice.
**No output in dmesg.** The message level may be filtered, or the ring buffer was already full. Check dmesg | tail right after loading.
What this module does not do
It does not interact with user space, does not hold any state and does not clean up anything, because it acquired nothing. A real module registers something: a character device, a file system, an interrupt handler. Every register_* call in the init function needs a matching unregister_* in the exit function, and a failure path in between that undoes the earlier ones. The pattern is the same one the coding style post showed with goto labels.
A safe habit while learning
Test modules in a virtual machine or on a spare machine. A null pointer dereference in user space kills one process, and in a module it produces an oops and can wedge the whole system. Keep a root console available, save your work before every insmod, and read the oops message when it comes: it names the instruction pointer and the module in the call trace, and addr2line or gdb on hello.ko turns it into a source line.
Taking it one step further
Once the module loads, extend it so that you see the same life cycle in a different way. Add a parameter, print the name of the task that loaded it with current->comm, or log a message from a kernel timer. Each addition teaches one idea while keeping the rest unchanged, and the previous posts in this series show where each belongs: the context in which your init runs is process context, so it may sleep and allocate with GFP_KERNEL, while a timer callback runs in softirq context and must not.
Resist the urge to write a large module as your second project. Grow the first one in small steps, loading and unloading after each, so that when something breaks you know which change did it.


