VMware ESXi and vSphere Cluster Management
Remove Loaded Kernel Modules in Linux with rmmod
Learn how to inspect and unload Linux kernel modules with lsmod, rmmod, and modprobe -r, handle dependencies safely, and understand the risks of forced removal.
A kernel module is a loadable component that extends the capabilities of the running Linux kernel. Modules commonly provide device drivers, filesystem support, networking features, and other optional kernel capabilities.
Linux can keep module code available on disk without loading it into memory. A compiled module usually has a .ko filename extension. An installed module is only a file available to the system; a loaded module is currently active in the running kernel. This lesson covers removing the latter.
Why remove a loaded kernel module?
- Free memory used by a module that is no longer needed.
- Temporarily disable a driver or optional kernel feature.
- Prepare to load a replacement or updated module during troubleshooting.
Unloading a module can interrupt hardware access or services that depend on it. Before removal, stop relevant services, unmount filesystems, disconnect affected devices, or otherwise end activity that uses the driver or feature.
Inspect loaded modules before removal
Use lsmod to list modules currently loaded into the running kernel:
lsmodTypical output includes these useful fields:
- Module: the loaded module name.
- Size: the amount of kernel memory associated with the module.
- Used by: the use count followed by modules that currently depend on it.
The use count is an indicator that a module is referenced by other modules or active kernel functionality. A nonzero count commonly prevents ordinary removal, although the precise cause should be investigated rather than guessed.
To search for a particular module:
lsmod | grep -i <module>Use modinfo when you need metadata, aliases, parameters, or dependency-related information:
modinfo <module>Use the module identifier, not the path to its compiled file. For example, pass example_driver, not /lib/modules/.../example_driver.ko.
Remove a module with rmmod
rmmod is the direct, low-level utility for requesting removal of a loaded kernel module. The basic form is:
sudo rmmod <module>Module names are commonly written with underscores, such as example_driver. Linux module tooling may accept a hyphenated equivalent such as example-driver in some contexts, but use the name shown by lsmod when possible.
Example: remove a simple unused module
- Confirm that the module is loaded:
lsmod | grep -i example_driver- Request normal removal:
sudo rmmod example_driver- Verify that it is no longer listed:
lsmod | grep -i example_driverIf the final command produces no matching line, the module is not currently listed as loaded. A successful removal changes the running system only; it does not delete the module file from disk.
Dependencies and modules in use
A module dependency exists when one module needs another module to remain loaded. The dependent module is sometimes called the consumer, while the module it needs is the provider. The provider ordinarily cannot be unloaded until its consumers have been removed.
A module can also be busy because a device, service, mounted filesystem, or network function is actively using it. In these cases, rmmod reports an error describing an in-use or dependency problem. The correct removal order is from consumers to providers.
For example, if module consumer_mod requires provider_mod, remove them in this order:
sudo rmmod consumer_mod
sudo rmmod provider_modInspect lsmod output to identify likely consumers. Use modinfo for additional metadata and dependency details:
lsmod
modinfo provider_modmodprobe is a module-management utility that resolves dependencies when loading or removing modules. Its removal form can remove a target and removable dependent modules:
sudo modprobe -r <module>This is often more convenient than manually finding and removing every dependent module, but it still cannot safely remove modules that remain actively used.
Example: removal blocked by a dependent module
- Attempt normal removal and read the error:
sudo rmmod provider_mod- Inspect the loaded-module list and metadata:
lsmod
modinfo provider_mod- Stop the service or workload using the feature, then remove the consumer:
sudo rmmod consumer_mod- Retry the provider, or use dependency-aware removal when appropriate:
sudo rmmod provider_mod
# Alternative:
sudo modprobe -r provider_modControlled replacement during troubleshooting
Unloading can be useful when replacing a driver or testing a changed module. Use a controlled sequence:
- Stop services and applications using the affected device or feature.
- Unmount relevant filesystems or disconnect affected hardware when applicable.
- Inspect dependencies with
lsmodandmodinfo. - Remove dependent modules first, or use
sudo modprobe -r <module>. - Load the desired module with
modprobeif needed. - Validate device and service operation, then check kernel messages if anything fails.
sudo modprobe <module>
lsmod | grep -i <module>Force removal with rmmod -f
The -f option requests a forced unload:
sudo rmmod -f <module>Forced removal is available only when the running kernel was built with CONFIG_MODULE_FORCE_UNLOAD enabled. Support is therefore kernel-build dependent; the option is not guaranteed to work on every system.
If rmmod -f is unavailable or rejected, do not assume that changing the kernel configuration is appropriate. Stop the workload, remove dependent modules, and retry ordinary removal. Forced unloading is best reserved for exceptional diagnostic or recovery situations where its risks are understood.
Command reference
Troubleshooting removal failures
Check kernel messages
After a failure or unexpected device behavior, inspect kernel messages with the tools available on your system, such as dmesg or journalctl. Messages can reveal driver errors, failed cleanup, or a resource that is still active. Verify the final state with:
lsmodRuntime removal and persistence
rmmod affects only the currently running system. The module may load again after reboot, hardware detection, service startup, or an explicit modprobe request.
If the actual goal is to prevent automatic loading in future sessions, runtime removal is not enough. A blacklist is configuration intended to prevent automatic loading of a module. Boot-time module configuration and blacklisting are separate administration tasks and should be used only after confirming that the driver is not required for boot, storage, networking, or recovery.
Exam-relevant notes
rmmoddirectly requests removal;lsmodlists modules currently loaded;modprobemanages loading and dependency-aware removal.- Use a module name such as
example_driver, not the path to an.kofile. - Remove dependent consumer modules before their provider module.
- A nonzero use count or an active device can block ordinary unloading.
rmmod -frequiresCONFIG_MODULE_FORCE_UNLOADand is dangerous, not a standard fix.- Removing a module at runtime does not blacklist it or delete it from disk.
For this topic, return to the Linux kernel module removal guide when reviewing the normal workflow and safety checks.