Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧩 Linux Kernel Module (Hello World) – Linux Ubuntu 24

This guide explains how to create, compile, insert, verify, and remove a simple Linux Kernel Module on Ubuntu 24.


📁 Step 1: Create Working Directory

mkdir kernel_module
cd kernel_module

📝 Step 2: Create Kernel Module Source File

Create a file named hello.c:

nano hello.c

Add the following code:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

static int __init hello_init(void) {
    printk(KERN_INFO "Hello: Module loaded into kernel\n");
    return 0;
}

static void __exit hello_exit(void) {
    printk(KERN_INFO "Hello: Module removed from kernel\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Simple Hello World Kernel Module");

Save and exit: CTRL + X → Y → ENTER

⚙️ Step 3: Create Makefile nano Makefile Add:

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 4: Compile the Module

make

Output: hello.ko → Kernel object file

🚀 Step 5: Insert Module into Kernel

sudo insmod hello.ko

🔍 Step 6: Verify Module Loaded

lsmod | grep hello

📜 Step 7: Check Kernel Logs

If you face permission issue:

sudo dmesg | tail

Expected Output: Hello: Module loaded into kernel

❌ Step 8: Remove the Module

sudo rmmod hello

Check logs again:

sudo dmesg | tail

Expected Output: Hello: Module removed from kernel

🧹 Step 9: Clean Build Files

make clean

🔄 Workflow Summary

  • Install dependencies
  • Write hello.c
  • Create Makefile
  • Compile using make
  • Insert module → insmod
  • Verify → lsmod, dmesg
  • Remove → rmmod
  • Clean → make clean

📌 Author

Badri Narayanan

About

Inserting a Simple Module in Kernel - Linux Ubuntu 24

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages