Hello World Kernel Driver : If you are starting your journey into Linux device driver development, writing a simple Hello World kernel module is the perfect first step. In this tutorial, we will walk you through creating, compiling, and loading a kernel driver in Linux — all in a beginner-friendly way.
Learn how to write your first Hello World Linux kernel driver with this step-by-step tutorial for beginners. This complete Linux kernel module development guide covers everything from setting up your environment to compiling and loading a kernel module. You will understand the basics of Linux device driver development, including how to create a simple printk() output in the kernel log, use module_init and module_exit functions, and build your driver using a Makefile. Whether you’re new to kernel programming in Linux or want to get started with embedded systems and operating system internals, this beginner-friendly guide will help you master the fundamentals of writing and running your own kernel modules. Perfect for students, hobbyists, and professionals looking to learn Linux driver programming from scratch.
What is a Hello World kernel module in Linux ?
A Linux kernel module (LKM) is a piece of code that can be loaded and unloaded into the Linux kernel at runtime. This allows you to add new functionalities, such as hardware drivers, without rebuilding the entire kernel.
A “Hello World” kernel driver is the simplest example — it just prints a message when loaded and unloaded.
Prerequisites of Hello World Kernel Driver
Before writing your kernel module, make sure you have:
- A Linux system (Ubuntu, Debian, Fedora, or similar)
- Basic knowledge of C programming
- Installed kernel headers and build tools
For Ubuntu/Debian-based systems, run:
sudo apt update
sudo apt install build-essential linux-headers-$(uname -r)
Step 1: Create the Source Code
Create a new directory for your driver and a file named hello.c:
mkdir hello_driver
cd hello_driver
nano hello.c
Paste the following code:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A Simple Hello World Linux Kernel Module");
MODULE_VERSION("1.0");
static int __init hello_init(void) {
printk(KERN_INFO "Hello, World! Kernel module loaded.\n");
return 0;
}
static void __exit hello_exit(void) {
printk(KERN_INFO "Goodbye, World! Kernel module unloaded.\n");
}
module_init(hello_init);
module_exit(hello_exit);
Explanation:
printk()prints messages to the kernel log (viewable withdmesg).module_init()specifies the function to run when the module loads.module_exit()specifies the function to run when the module unloads.
Step 2: Create the Makefile
The Makefile tells the Linux kernel build system how to compile your module.
Create a file named 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) clean
Step 3: Compile the Kernel Module
Run:
make
If successful, you will see a new file called hello.ko — this is your compiled kernel driver.
Step 4: Load the Module
To load your driver into the kernel:
sudo insmod hello.ko
Check the kernel log:
dmesg | tail
You should see:
Hello, World! Kernel module loaded.
Step 5: Unload the Module
When you want to remove the module:
sudo rmmod hello
Check the log again:
dmesg | tail
Output:
Goodbye, World! Kernel module unloaded.
Step 6: Clean Up
To remove build files:
make clean
Final Thoughts
You’ve just created your first Hello World kernel driver in Linux! This small example lays the foundation for developing more advanced Linux device drivers. From here, you can explore working with hardware, file operations, and different driver types (character, block, and network drivers)
Frequently Asked Questions (FAQ) | Hello World Linux Kernel Driver
1. What is a Hello World Kernel Driver ?
2. What is the purpose of a Hello World kernel module?
3. Do I need special hardware to write a Hello World Linux kernel driver?
4. How do I compile a Hello World kernel module?
make command with the correct -C /lib/modules/$(uname -r)/build path will generate the .ko file (kernel object file) for your module.5. How do I load a kernel driver in Linux?
insmod command, for example:sudo insmod hello.ko
To confirm it loaded successfully, check the kernel log using:
dmesg | tail
6. How do I remove a kernel driver in Linux?
rmmod command:sudo rmmod hello
Then, verify it was removed by checking the kernel log with
dmesg.7. Is it safe to experiment with kernel programming?
8. Can I use this Hello World driver on any Linux distribution?
You can also Visit other tutorials of Embedded Prep
- Multithreading in C++
- Multithreading Interview Questions
- Multithreading in Operating System
- Multithreading in Java
- POSIX Threads pthread Beginner’s Guide in C/C++
- Speed Up Code using Multithreading
- Limitations of Multithreading
- Common Issues in Multithreading
- Multithreading Program with One Thread for Addition and One for Multiplication
- Advantage of Multithreading
- Disadvantages of Multithreading
- Applications of Multithreading: How Multithreading Makes Modern Software Faster and Smarter”

Leave a Reply