Linux insmod Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the insmod command in Linux, which is used to insert a kernel module into the running kernel. Kernel modules are pieces of code that can be loaded and unloaded into the kernel as needed, providing additional functionality or drivers. We will learn how to compile a simple kernel module and use insmod to insert it into the running kernel.

The lab covers the following steps: introduction to the insmod command, compiling a kernel module, and inserting a kernel module with insmod. The linux-headers-$(uname -r) package is required to compile the kernel module, and the insmod command is used to insert the compiled module into the running kernel.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/uname("`System Information Displaying`") subgraph Lab Skills linux/uname -.-> lab-422737{{"`Linux insmod Command with Practical Examples`"}} end

Introduction to the insmod Command

In this step, we will explore the insmod command in Linux, which is used to insert a kernel module into the running kernel. Kernel modules are pieces of code that can be loaded and unloaded into the kernel as needed, providing additional functionality or drivers.

First, let's check the current kernel version and the available kernel modules:

uname -r
lsmod

Example output:

5.15.0-58-generic
Module                  Size  Used by
nvme_core              102400  1
pci_stub                16384  1
vboxpci                 24576  0
vboxnetadp              45056  0
vboxnetflt              49152  0
vboxdrv               1028096  3 vboxnetadp,vboxnetflt,vboxpci

The lsmod command lists the currently loaded kernel modules. We can see that there are several modules related to VirtualBox, which is a common virtualization software.

Next, let's create a simple kernel module. Create a new file named hello.c in the ~/project directory with the following content:

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

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple kernel module");

int init_module(void) {
    printk(KERN_INFO "Hello, kernel!\n");
    return 0;
}

void cleanup_module(void) {
    printk(KERN_INFO "Goodbye, kernel!\n");
}

This module simply prints a "Hello, kernel!" message when it's loaded and a "Goodbye, kernel!" message when it's unloaded.

Now, let's compile the kernel module:

sudo apt-get install -y linux-headers-$(uname -r)
gcc -Wall -DMODULE -D__KERNEL__ -I/lib/modules/$(uname -r)/build/include -c hello.c

The linux-headers-$(uname -r) package contains the header files needed to compile kernel modules for the current kernel version. The gcc command compiles the hello.c file into an object file hello.o.

With the compiled kernel module, we can now use the insmod command to insert it into the running kernel:

sudo insmod hello.o

Example output:

The insmod command loads the hello.o module into the kernel. You should see the "Hello, kernel!" message in the system logs (e.g., dmesg).

To verify that the module is loaded, run:

lsmod | grep hello

Example output:

hello                  16384  0

The output shows that the hello module is now loaded in the kernel.

Compiling a Kernel Module

In this step, we will learn how to compile a kernel module from source code.

First, let's create a new kernel module file named hello2.c in the ~/project directory with the following content:

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

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Another simple kernel module");

int init_module(void) {
    printk(KERN_INFO "Hello, from the second kernel module!\n");
    return 0;
}

void cleanup_module(void) {
    printk(KERN_INFO "Goodbye, from the second kernel module!\n");
}

This module is similar to the one we created in the previous step, but with a different message.

Now, let's compile the hello2.c module:

gcc -Wall -DMODULE -D__KERNEL__ -I/lib/modules/$(uname -r)/build/include -c hello2.c

The compilation process is the same as before, with the exception that we're now compiling the hello2.c file instead of hello.c.

After the compilation, you should see the hello2.o object file in the ~/project directory.

Inserting a Kernel Module with insmod

In this step, we will learn how to insert the compiled kernel module into the running kernel using the insmod command.

First, let's make sure that the hello2.o module is compiled and available in the ~/project directory:

ls -l ~/project/hello2.o

Example output:

-rw-r--r-- 1 labex labex 16384 Apr 11 12:34 /home/labex/project/hello2.o

Now, we can use the insmod command to insert the hello2.o module into the kernel:

sudo insmod ~/project/hello2.o

Example output:

The insmod command loads the hello2.o module into the kernel. You should see the "Hello, from the second kernel module!" message in the system logs (e.g., dmesg).

To verify that the module is loaded, run:

lsmod | grep hello2

Example output:

hello2                 16384  0

The output shows that the hello2 module is now loaded in the kernel.

Summary

In this lab, we explored the insmod command in Linux, which is used to insert a kernel module into the running kernel. We first checked the current kernel version and the available kernel modules using the uname -r and lsmod commands. Then, we created a simple kernel module that prints a "Hello, kernel!" message when loaded and a "Goodbye, kernel!" message when unloaded. We compiled the kernel module using the gcc command and the linux-headers-$(uname -r) package, which contains the header files needed for the current kernel version. Finally, we used the insmod command to insert the compiled kernel module into the running kernel.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like