Loadable Kernel Modules
Loadable Kernel Modules
In Linux, the kernel is the core part of the operating system that manages hardware, system resources, and communication between software and hardware. However, not all features need to be built into the kernel at all times. To keep the kernel lightweight and efficient, Linux allows for parts of its functionality to be loaded and unloaded on demand. These parts are called Loadable Kernel Modules.
A Loadable Kernel Module (LKM) is a piece of code that can be dynamically added to or removed from the Linux kernel while the system is running without rebooting the system. Unlike the core kernel, which is always present, modules allow you to extend functionality without rebooting or recompiling the entire kernel.
What is a Kernel Module?
A kernel module is an object file that contains code to extend the functionality of the kernel. It is compiled separately and can be loaded into the kernel when needed.
It is a compiled program that adds features like device drivers, filesystem support, or system utilities to the kernel. It runs in privileged kernel space and has direct access to hardware.
Modules are stored as .ko
(Kernel Object) files in /lib/modules
. Once loaded, it becomes part of the kernel and can directly interact with hardware or kernel resources.
Listing Modules
You can use the lsmod
command to view all currently loaded kernel modules. It displays the module name, size, and dependencies. Alternatively, check the /proc/modules
file or use modinfo <module_name>
for detailed information.
To view all currently loaded kernel modules, you can use the lsmod
command. This command displays a list of modules along with information like size and dependencies.
$ lsmod
Loading Modules
You can load a kernel module using the insmod
or modprobe
command.
To load a module, use the following command:
$ sudo modprobe <module_name>
$ sudo insmod <module_name>
The modprobe
command automatically resolves dependencies. For manual loading, use sudo insmod <path/to/module.ko>
. Modules are often loaded automatically when required by the system. While insmod
directly inserts a module, modprobe
also loads any required dependencies automatically.
$ sudo insmod mymodule.ko
$ sudo modprobe mymodule
Unloading Modules
To remove a kernel module, use the rmmod
or modprobe -r
command. Â This helps free up resources when the module is no longer needed.
To unload a module, you can use the following command:
$sudo modprobe -r <module_name>
to safely unload a module, including its dependencies.
For manual removal, use
$sudo rmmod <module_name>
Note that some modules cannot be unloaded if they are in use or marked as “permanent.”