Linux Kernel Module Management Commands: Complete Guide
Prerequisites
What are the main commands for Linux kernel module operations?
Linux kernel module management commands allow you to dynamically load, unload, and configure device drivers without rebooting. The essential commands are lsmod
(list modules), modprobe
(load/unload with dependencies), insmod
(low-level load), rmmod
(remove), modinfo
(detailed information), and depmod
(generate dependencies). Use modprobe
for most tasks as it handles dependencies automatically.
What Are Linux Kernel Module Management Commands?
Kernel module management commands are essential system administration tools that enable you to interact with loadable kernel modules (LKMs) in Linux. These modules are pieces of code that extend kernel functionality without requiring a system reboot, making them crucial for hardware driver management, filesystem support, and system feature activation.
Furthermore, understanding these commands is vital for troubleshooting hardware issues, optimizing system performance, and maintaining secure Linux environments. Modern Linux distributions rely heavily on modular kernels, where most drivers are loaded on-demand rather than compiled directly into the kernel.
Table of Contents
- How to List Loaded Kernel Modules
- How to Load Kernel Modules
- How to Unload Kernel Modules
- How to Get Module Information
- What Is the Difference Between modprobe and insmod
- How to Configure Persistent Module Loading
- How to Manage Module Dependencies
- Why Use Kernel Module Blocklisting
- How to Pass Parameters to Kernel Modules
- FAQ
- Troubleshooting Common Module Issues
How to List Loaded Kernel Modules
The lsmod
command displays all currently loaded kernel modules in your running system. This is typically your first step when diagnosing hardware or driver issues.
Basic Module Listing
$ lsmod
Module Size Used by
vfat 17411 1
fat 65059 1 vfat
usb_storage 65065 2 uas
e1000 137260 0
nfs 233966 3 nfsv3,nfsv4
bluetooth 516096 31 btrtl,btintel,btbcm
The output shows three critical columns:
- Module: The name of the loaded module
- Size: Memory footprint in bytes
- Used by: Reference count and dependent modules
Filtering Module Output
To search for specific modules, combine lsmod
with grep
:
# Find all network-related modules
$ lsmod | grep -i net
# Check if a specific driver is loaded
$ lsmod | grep e1000
e1000 137260 0
Pro Tip: The “Used by” column indicates module dependencies. Consequently, you cannot unload a module if other modules depend on it or if it’s actively being used by hardware.
How to Load Kernel Modules
Loading kernel modules is accomplished through two primary commands: modprobe
(recommended) and insmod
(low-level). Meanwhile, both commands require root privileges.
Using modprobe (Recommended Method)
The modprobe
command intelligently handles module dependencies:
# Load a module with automatic dependency resolution
$ sudo modprobe bluetooth
# Load a module with parameters
$ sudo modprobe parport_pc io=0x3bc irq=auto
# Dry-run to see what would happen
$ sudo modprobe --dry-run --verbose bluetooth
Using insmod (Low-Level Alternative)
In contrast, insmod
requires the full path to the module file:
# Load module with full path
$ sudo insmod /lib/modules/$(uname -r)/kernel/drivers/net/ethernet/intel/e1000/e1000.ko
# Load with parameters
$ sudo insmod /path/to/module.ko parameter=value
Real-World Example: Loading USB Storage Support
# Verify the module is available
$ modinfo usb_storage
# Load the module
$ sudo modprobe usb_storage
# Verify it loaded successfully
$ lsmod | grep usb_storage
usb_storage 65065 2 uas
# Check kernel messages for loading confirmation
$ sudo dmesg | tail -20
[12345.678] usb_storage: USB Mass Storage device detected
How to Unload Kernel Modules
Removing kernel modules from memory is essential for troubleshooting, updating drivers, or freeing system resources. Nevertheless, you can only unload modules that aren’t currently in use.
Using modprobe -r (Recommended)
The modprobe -r
command removes modules along with their unused dependencies:
# Remove a module and its dependencies
$ sudo modprobe -r bluetooth
# Force removal (use with caution)
$ sudo modprobe -rf module_name
Using rmmod (Direct Removal)
The rmmod
command removes only the specified module:
# Remove single module
$ sudo rmmod e1000
# Check for errors if removal fails
$ sudo rmmod bluetooth
rmmod: ERROR: Module bluetooth is in use by: btrtl btintel btbcm
Handling “Module in Use” Errors
# Identify what's using the module
$ lsmod | grep bluetooth
bluetooth 516096 31 btrtl,btintel,btbcm
# Stop services using the module
$ sudo systemctl stop bluetooth
# Now remove the module
$ sudo modprobe -r bluetooth
How to Get Module Information
The modinfo
command reveals detailed information about kernel modules, including parameters, dependencies, and licensing.
Basic Module Information
# Display all module information
$ modinfo e1000
filename: /lib/modules/5.15.0/kernel/drivers/net/ethernet/intel/e1000/e1000.ko
version: 7.3.21-k8-NAPI
license: GPL
description: Intel(R) PRO/1000 Network Driver
author: Intel Corporation
srcversion: ABC123DEF456
alias: pci:v00008086d00001000sv*sd*bc*sc*i*
depends:
retpoline: Y
Extracting Specific Information
# Show only description
$ modinfo -d e1000
Intel(R) PRO/1000 Network Driver
# Show module author
$ modinfo -a e1000
# List available parameters
$ modinfo -p e1000
debug:Debug level (0=none,...,16=all) (int)
copybreak:Maximum size of packet that is copied (int)
Module Information Comparison Table
Command | Output | Use Case |
---|---|---|
modinfo module | Complete details | Comprehensive analysis |
modinfo -d module | Description only | Quick identification |
modinfo -p module | Parameters | Configuration planning |
modinfo -F filename module | File location | Troubleshooting paths |
modinfo -F depends module | Dependencies | Dependency mapping |
What Is the Difference Between modprobe and insmod
Understanding the distinction between modprobe
and insmod
is crucial for effective kernel module management. Although both load modules, they differ significantly in functionality.
Comparison Matrix
Feature | modprobe | insmod |
---|---|---|
Dependency Resolution | Automatic | Manual |
Path Requirement | Module name only | Full path required |
Configuration Files | Reads /etc/modprobe.d/ | Ignores configuration |
Symbol Resolution | Automatic | Manual |
Recommended For | Production use | Debugging only |
Error Handling | Comprehensive | Basic |
Practical Examples
# modprobe: Simple and smart
$ sudo modprobe nfs
# Automatically loads: nfsv3, nfsv4, dns_resolver, sunrpc
# insmod: Manual and explicit
$ sudo insmod /lib/modules/$(uname -r)/kernel/fs/nfs/nfsv4.ko
# Error: Missing dependencies!
# Must manually load: sunrpc.ko, dns_resolver.ko first
When to Use Each Command
Use modprobe when:
- Loading modules in production environments
- You need automatic dependency handling
- Working with standard system drivers
- Configuring persistent module settings
Use insmod when:
- Debugging custom kernel modules
- Loading modules from non-standard locations
- Testing module development
- Working in emergency recovery scenarios
How to Configure Persistent Module Loading
Temporary module loading (via modprobe
) doesn’t survive reboots. Therefore, you need proper configuration for persistent module management.
Method 1: Using /etc/modules
On Debian-based systems (Ubuntu, Debian), add module names to /etc/modules
:
# Edit the modules file
$ sudo nano /etc/modules
# Add your modules (one per line)
nfs
bluetooth
e1000e
Method 2: Using /etc/modules-load.d/ (systemd)
Modern systemd-based distributions use the modules-load.d directory:
# Create a custom configuration file
$ sudo nano /etc/modules-load.d/network-drivers.conf
# Add modules to load at boot
e1000
ixgbe
igb
# Test the configuration
$ sudo systemd-modules-load
Method 3: Using /etc/modprobe.d/ for Options
To load modules with specific parameters:
# Create custom options file
$ sudo nano /etc/modprobe.d/sound-options.conf
# Set module options
options snd-hda-intel model=generic
options bluetooth disable_ertm=1
# Apply changes on next boot
$ sudo update-initramfs -u
Verification and Testing
# Test if module loads at boot
$ sudo reboot
# After reboot, verify
$ lsmod | grep your_module
# Check systemd service status
$ systemctl status systemd-modules-load
How to Manage Module Dependencies
The depmod
command generates module dependency files that modprobe
uses for automatic dependency resolution.
Understanding Module Dependencies
Module dependencies are stored in /lib/modules/$(uname -r)/modules.dep
:
# View dependency file
$ cat /lib/modules/$(uname -r)/modules.dep | grep bluetooth
kernel/net/bluetooth/bluetooth.ko:
kernel/drivers/bluetooth/btrtl.ko: kernel/net/bluetooth/bluetooth.ko
Generating Dependency Files
# Generate dependencies for current kernel
$ sudo depmod -a
# Generate for specific kernel version
$ sudo depmod -a 5.15.0-generic
# Quick check (skip if up-to-date)
$ sudo depmod -A
# Verbose output for debugging
$ sudo depmod -av
When to Run depmod
You must regenerate dependencies when:
- Installing custom kernel modules
- Upgrading kernel versions
- Moving module files manually
- Troubleshooting module loading failures
# Complete module installation workflow
$ sudo cp new_driver.ko /lib/modules/$(uname -r)/extra/
$ sudo depmod -a
$ sudo modprobe new_driver
Why Use Kernel Module Blocklisting
Blocklisting prevents specific modules from loading automatically, which is essential for resolving hardware conflicts, security issues, or system instability.
Creating Blocklist Configuration
# Create blocklist file
$ sudo nano /etc/modprobe.d/blacklist-custom.conf
# Add modules to blocklist
blacklist nouveau # Blocklist open-source NVIDIA driver
blacklist pcspkr # Disable PC speaker
blacklist btusb # Disable Bluetooth USB
Alternative: Using install Directive
For more control, use the install
directive:
$ sudo nano /etc/modprobe.d/disable-nouveau.conf
# Prevent module loading entirely
install nouveau /bin/true
blacklist nouveau
Applying Blocklist Changes
# Update initramfs (Debian/Ubuntu)
$ sudo update-initramfs -u
# Update initramfs (Fedora/RHEL)
$ sudo dracut --force
# Reboot to apply
$ sudo reboot
Verifying Blocklist Status
# Check if module is blocklisted
$ modprobe -c | grep blacklist
# Verify module isn't loaded
$ lsmod | grep nouveau
# (no output means it's not loaded)
How to Pass Parameters to Kernel Modules
Module parameters customize driver behavior without recompiling. Accordingly, parameters can be set temporarily or permanently.
Temporary Parameter Configuration
# Load with single parameter
$ sudo modprobe e1000 debug=16
# Load with multiple parameters
$ sudo modprobe bluetooth disable_ertm=1 enable_hs=0
# View available parameters
$ modinfo -p bluetooth
disable_ertm:Disable enhanced retransmission mode
enable_hs:Enable High Speed
Permanent Parameter Configuration
# Create options file
$ sudo nano /etc/modprobe.d/module-options.conf
# Set persistent options
options e1000 debug=2 copybreak=256
options bluetooth disable_ertm=1
options snd-hda-intel model=generic power_save=1
# Apply without reboot (if module not loaded)
$ sudo modprobe -r bluetooth && sudo modprobe bluetooth
Real-World Parameter Examples
# Intel wireless driver optimization
options iwlwifi 11n_disable=1 swcrypto=1
# USB storage for better compatibility
options usb_storage quirks=0123:4567:u
# Graphics driver power management
options i915 enable_guc=2 enable_fbc=1
FAQ
What happens if I load the same module twice?
Nothing happens. Linux prevents duplicate module loading. However, if you run modprobe
on an already-loaded module, the command simply exits successfully without errors. The kernel maintains a single instance of each module.
Can I unload critical system modules?
Some modules are marked as “permanent” or have dependencies that prevent unloading. Critical modules like kernel
, systemd
, or actively used filesystem drivers cannot be removed while the system is running. Additionally, the rmmod -f
option exists but is extremely dangerous and not recommended.
How do I find which module controls my hardware?
Use these commands to identify hardware-to-module mappings:
# List PCI devices with kernel drivers
$ lspci -k
# List USB devices with modules
$ lsusb -t
# Check specific hardware
$ dmesg | grep -i 'driver.*module'
What is the /lib/modules directory structure?
The directory structure follows this pattern:
/lib/modules/$(uname -r)/
βββ kernel/ # Kernel modules organized by subsystem
β βββ drivers/ # Hardware drivers
β βββ fs/ # Filesystem support
β βββ net/ # Network protocols
βββ modules.dep # Dependency database
βββ modules.alias # Hardware ID mappings
βββ modules.symbols # Symbol table
How do I recover from a bad module?
Boot into recovery mode or use kernel parameters:
# At GRUB, add to kernel line:
modprobe.blacklist=problematic_module
# Or boot with minimal modules:
systemd.unit=rescue.target
Can modules cause kernel panics?
Yes, buggy or incompatible modules can crash the kernel. Therefore, always test modules in non-production environments first. Moreover, check dmesg
and /var/log/kern.log
for module-related errors before system crashes.
How do I update kernel modules?
Modules are typically updated with kernel packages. However, third-party modules may require manual updates:
# Update via package manager (DKMS modules)
$ sudo apt update && sudo apt upgrade # Debian/Ubuntu
$ sudo dnf update # Fedora/RHEL
# Manual module update
$ sudo rmmod old_module
$ sudo cp new_module.ko /lib/modules/$(uname -r)/extra/
$ sudo depmod -a
$ sudo modprobe new_module
Troubleshooting Common Module Issues
Problem 1: Module Not Found
Symptoms:
$ sudo modprobe mydriver
modprobe: FATAL: Module mydriver not found in directory /lib/modules/5.15.0
Solutions:
- Verify module exists:
$ find /lib/modules/$(uname -r) -name "*mydriver*"
- Check module name spelling:
$ ls /lib/modules/$(uname -r)/kernel/drivers/ | grep -i driver
- Regenerate module database:
$ sudo depmod -a
$ sudo modprobe mydriver
- Install missing driver package:
# Debian/Ubuntu
$ apt-cache search mydriver
$ sudo apt install linux-modules-extra-$(uname -r)
# Fedora/RHEL
$ sudo dnf install kmod-mydriver
Problem 2: Module Already in Use
Symptoms:
$ sudo rmmod e1000
rmmod: ERROR: Module e1000 is in use
Solutions:
- Identify dependent modules:
$ lsmod | grep e1000
e1000 137260 2
- Find processes using the module:
$ sudo lsof | grep e1000
$ sudo fuser -v /dev/eth0
- Stop services gracefully:
$ sudo systemctl stop NetworkManager
$ sudo ip link set eth0 down
$ sudo modprobe -r e1000
Problem 3: Dependency Resolution Failures
Symptoms:
$ sudo insmod module.ko
insmod: ERROR: could not insert module: Unknown symbol in module
Solutions:
- Check missing symbols:
$ sudo dmesg | tail -20
module: Unknown symbol function_name
- Use modprobe instead (handles dependencies):
$ sudo modprobe module_name
- Verify dependency chain:
$ modinfo -F depends module_name
required_module1,required_module2
- Load dependencies manually:
$ sudo modprobe required_module1
$ sudo modprobe required_module2
$ sudo modprobe module_name
Problem 4: Module Version Mismatch
Symptoms:
$ sudo modprobe mydriver
modprobe: ERROR: could not insert 'mydriver': Invalid module format
Diagnostic Commands:
# Check kernel version
$ uname -r
5.15.0-76-generic
# Check module version
$ modinfo -F vermagic mydriver
5.15.0-72-generic SMP mod_unload
# View detailed error
$ sudo dmesg | grep mydriver
mydriver: version magic '5.15.0-72' should be '5.15.0-76'
Solutions:
- Recompile module for current kernel:
$ cd /usr/src/mydriver
$ make clean
$ make
$ sudo make install
$ sudo depmod -a
- Force load (not recommended):
$ sudo modprobe -f mydriver # Dangerous!
Problem 5: Persistent Loading Failures
Symptoms: Module loads manually but fails at boot.
Solutions:
- Check systemd module loading service:
$ systemctl status systemd-modules-load
$ journalctl -u systemd-modules-load
- Verify configuration files:
$ cat /etc/modules
$ ls -la /etc/modules-load.d/
- Test module loading manually:
$ sudo systemd-modules-load
- Add to initramfs (for boot-critical modules):
# Debian/Ubuntu
$ echo "mydriver" | sudo tee -a /etc/initramfs-tools/modules
$ sudo update-initramfs -u
# Fedora/RHEL
$ echo "mydriver" | sudo tee -a /etc/dracut.conf.d/mydriver.conf
$ sudo dracut --force
Problem 6: Performance Issues After Module Load
Diagnostic Steps:
# Monitor system resources
$ top
$ htop
# Check module parameters
$ cat /sys/module/module_name/parameters/*
# Review kernel logs
$ sudo dmesg -T | grep module_name
$ sudo journalctl -k -b | grep module_name
# Check interrupt usage
$ cat /proc/interrupts | grep module
Solutions:
- Adjust module parameters:
$ sudo modprobe -r problematic_module
$ sudo modprobe problematic_module param=new_value
- Update driver firmware:
$ sudo apt install linux-firmware
$ sudo update-initramfs -u
Module Loading Troubleshooting Checklist
- [ ] Verify module file exists in
/lib/modules/$(uname -r)/
- [ ] Check kernel version matches module version
- [ ] Run
depmod -a
to regenerate dependencies - [ ] Review
dmesg
output for error messages - [ ] Verify required firmware files are present
- [ ] Check for conflicting modules (blocklist if needed)
- [ ] Test with verbose output:
modprobe -v module_name
- [ ] Verify hardware is detected:
lspci -k
orlsusb -t
- [ ] Check SELinux/AppArmor policies if applicable
- [ ] Review
/var/log/kern.log
for historical issues
Command Quick Reference
Task | Command | Example |
---|---|---|
List loaded modules | lsmod | lsmod | grep bluetooth |
Load module | modprobe | sudo modprobe e1000 |
Unload module | modprobe -r | sudo modprobe -r e1000 |
Module info | modinfo | modinfo -p bluetooth |
Low-level load | insmod | sudo insmod /path/to/module.ko |
Remove module | rmmod | sudo rmmod e1000 |
Generate deps | depmod | sudo depmod -a |
List parameters | modinfo -p | modinfo -p snd-hda-intel |
Check config | modprobe -c | modprobe -c | grep blacklist |
Dry run | modprobe --dry-run | modprobe --dry-run bluetooth |
Additional Resources
Official Documentation
- Linux Kernel Module Programming Guide – kernel.org official documentation
- Man Pages: modprobe(8) – Comprehensive modprobe manual
- Man Pages: lsmod(8) – Complete lsmod reference
Distribution-Specific Guides
- Red Hat: Managing Kernel Modules – RHEL official guide
- Ubuntu: Kernel Module Management – Ubuntu community documentation
- Arch Linux: Kernel Modules – Comprehensive Arch Wiki guide
Development Resources
- Linux Device Drivers – Free book on driver development
- Kernel Module Programming Tutorial – TLDP comprehensive tutorial
Conclusion
Mastering Linux kernel module management commands is essential for effective system administration. The modprobe
command should be your primary tool for module operations, as it handles dependencies automatically and integrates with system configuration files. Nevertheless, understanding the complete toolsetβincluding lsmod
, insmod
, rmmod
, modinfo
, and depmod
βempowers you to troubleshoot complex driver issues and optimize system performance.
Remember that kernel modules directly interact with hardware and system resources. Therefore, always test module changes in non-production environments first. Moreover, maintain proper documentation of custom module configurations to ensure system maintainability.
By following the best practices outlined in this guide, you’ll be able to effectively manage kernel modules, resolve driver conflicts, and maintain stable Linux systems across various distributions and hardware configurations.
Keywords: Linux kernel module management commands, modprobe tutorial, lsmod command, insmod usage, kernel driver management, module loading Linux, depmod utility, rmmod command, module dependencies