Kernel modules are pieces of code that extend the functionality of the operating system. Since this is fairly fundamental to Linux, there are tools (generally prefixed or suffixed with mod) to manage modules, such as insmod, modprobe, etc.

Note that insmod and rmmod are low-level programs that provide fine control but few safeguards; as a result, modprobe is generally the preferred tool.

CommandUsage
insmodInstall a module
modprobeManage modules with dependencies
rmmodSafely remove a module
depmodAnalyze system dependencies
lsmodList loaded modules
modinfoProvide detailed info about a module

modprobe

modprobe loads and checks for dependencies when installing a module, unlike insmod. The main advantage of this is that it will handle dependencies, at the cost of fine control over the process.

It searches the module directory and loads everything required. Note that unlike insmod, modprobe does not require the entire path to the module itself - it will work simply by name (as a consequence of searching for dependencies, etc. I assume).

In addition to this we can use a few flags, such as -a to add multiple modules, or -r to unload and remove modules that rely on the module being removed. There is -f to force loading, -n for dry-runs, -v for verbose mode, and -s for redirecting output to system logs.

Example

modprobe -r wifi_driver && modprobe -v wifi_driver

This command will unload and remove the wifi_driver module and its dependants; once successful, it will re-install the driver and show verbose output to the screen.

insmod

insmod is used to manually install a module by directly loading it into the kernel. Note that this command does not check for dependencies.

It requires a full path to a .ko file. The command is often used for testing or loading custom modules; again, insmod does not resolve dependencies.

Example

insmod /lib/modules/$(uname -r)/extra/custom_module.ko

This command will insert the custom_module.ko file into the actively running kernel; yet it can fail since there is no error handling or dependency resolution

rmmod

Safely remove a module from the kernel. Nonetheless, it should be used with caution, as it does not check for dependencies, much like insmod.

Example

rmmod usb_storage

This would remove the USB storage module, but might fail if a USB connection is active. Can be solved by unmounting or removing the device; however, it is generally suggested that we use modprobe -r instead.

depmod

Before Linux can load Kernel modules properly, it needs a dependency map to know which modules rely on others. depmod scans all available modules, analyses their dependencies, and generates a file that helps modprobe load them correctly.

The modules.dep file will have a generic format: <module_path>:<dependency1> [<dependency2>...]

Example

sudo depmod

This will generate a modules.dep file that lists module dependencies. With this file, we can then check which files depend on a particular module, for example nfs:

cat /lib/modules/$(uname -r)/modules.dep | grep nfs

In my case, this gives, among others:

kernel/fs/nfs_common/nfs_acl.ko.zst: kernel/net/sunrpc/sunrpc.ko.zst Meaning that nfs_acl depends on sunrpc.

lsmod

lsmod is used to list all loaded kernel modules, as well as their size and their dependants. It can be used to find conflicting modules or to explore hardware malfunctions, issues with drivers, or kernel crashes.

Example

lsmod

This will generate something like:

Module                  Size  Used by
uinput                 28672  1
snd_seq_dummy          12288  0
rfcomm                106496  7
snd_hrtimer            12288  1
snd_seq               139264  7 snd_seq_dummy
ccm                    20480  6

modinfo

modinfo gives us specific information about a particular module. It is useful to understand information about it before making any changes.

Example

modinfo md4

This will output something like:

filename:       /lib/modules/6.16.1-zen1-1-zen/kernel/crypto/md4.ko.zst
alias:          crypto-md4
alias:          md4
description:    MD4 Message Digest Algorithm
license:        GPL
srcversion:     237564196E6A187C0CB8326
depends:
intree:         Y
name:           md4
retpoline:      Y
vermagic:       6.16.1-zen1-1-zen SMP preempt mod_unload
sig_id:         PKCS#7
signer:         Build time autogenerated kernel key
sig_key:        74:4A:77:BD:78:12:E0:E3:F5:1B:9D:4D:BB:0B:B2:1A:2E:F6:A5:4D
sig_hashalgo:   sha512
signature:      30:65:02:31:00:8B:E5:D9:7A:48:24:1F:12:1C:A1:43:05:5F:92:9A: