Introduction
Kernel modules in Linux play a key role in extending the operating system's functionality without recompiling the kernel. They allow adding support for new devices, file systems, or network protocols without restarting the system. This article explores the fundamental principles of working with kernel modules and the essential commands for their administration.
Theoretical Part
What is a Kernel Module?
A kernel module is an individual Linux component that can be loaded into the kernel during system runtime. These modules add new functionality, such as device drivers, network protocols, or file systems, without requiring kernel recompilation. This makes the system more flexible and easier to manage.
Advantages of Modularity
- Flexibility: Ability to add and remove functionality without modifying the kernel.
- Reduced Overhead: Unloading inactive modules frees up system resources.
- Hardware Support: Adding drivers for new devices without rebooting the system.
How Do Kernel Modules Work?
Modules are loaded into memory and integrated into the kernel using system calls. This allows the kernel to dynamically modify its capabilities and work with new devices or functional blocks without altering the source code.
Practical Part
Commands for Kernel Module Administration
1. lsmod — View Loaded Modules
Description:
The lsmod command lists all currently loaded kernel modules and their dependencies.
Syntax:
lsmod
Example Output:
Module Size Used by
nvidia 1024000 0
snd_hda_intel 45056 3
snd_hda_codec 81920 2 snd_hda_intel,snd_hda_codec_hdmi
- Module: Module name.
- Size: Module size in bytes.
- Used by: Number of dependencies or processes using the module.
Usage:
Helps administrators quickly view the list of active modules.
2. modprobe — Load and Unload Modules
Description:
The modprobe command loads and unloads kernel modules, automatically managing their dependencies.
Syntax:
modprobe <module_name>
Example (Load a Module):
modprobe nvidia
Syntax (Unload a Module):
modprobe -r <module_name>
Example (Unload a Module):
modprobe -r nvidia
Key Features:
- Automatically loads all module dependencies.
- The
rflag is used for unloading modules.
Usage:
Convenient for managing modules with dependency handling.
3. insmod — Force Load a Module
Description:
The insmod command manually loads a kernel module. It does not handle dependencies, so they must already be loaded.
Syntax:
insmod <module_path>
Example:
insmod /lib/modules/$(uname -r)/kernel/drivers/net/ethernet/realtek/r8169.ko
Key Features:
- Requires specifying the full path to the module.
Usage:
For advanced users who need to manually load a module.
4. rmmod — Remove a Module from the Kernel
Description:
The rmmod command unloads a module from the kernel without checking dependencies.
Syntax:
rmmod <module_name>
Example:
rmmod r8169
Key Features:
- Unloading is only possible if the module has no dependencies.
Usage:
For manually removing modules.
5. modinfo — Get Module Information
Description:
The modinfo command displays detailed information about a module: version, author, description, and dependencies.
Syntax:
modinfo <module_name>
Example:
modinfo nvidia
Example Output:
filename: /lib/modules/$(uname -r)/kernel/drivers/video/nvidia.ko
version: 460.56.03
license: NVIDIA
description: NVIDIA driver
author: NVIDIA Corporation
Usage:
Useful for diagnostics and obtaining module details.
Additional modprobe Capabilities
Load a Module with Parameters
Syntax:
modprobe <module_name> <param1=value1> <param2=value2>
Example:
modprobe nf_conntrack ipv6=1
Using Configuration Files
Configuration files located in /etc/modprobe.d/ or /lib/modprobe.d/ can set predefined parameters and behaviors for modules. For instance, you can configure a module permanently:
Example Configuration File Entry:
options nf_conntrack ipv6=1
Conclusion
Kernel module administration is an essential aspect of managing Linux systems. By using commands like lsmod, modprobe, insmod, rmmod, and modinfo, administrators can efficiently load, unload, and configure kernel modules. Mastering these tools enhances system flexibility and simplifies troubleshooting related to drivers and other kernel components.