Linux kernel modules are like plugins for your operating system’s kernel. They let you load and unload functionality on demand, without rebooting. This is incredibly powerful, but understanding how to manage them is key.
Let’s see modprobe in action. Imagine you need to enable support for a new USB network adapter. You’d typically install the driver package, and then tell the kernel to load it:
sudo modprobe usbnet
This single command is often all you need. It finds the usbnet.ko file (the compiled module), checks if it has any dependencies (other modules it needs to load first), loads those dependencies, and then loads usbnet.ko itself into the running kernel. If usbnet requires specific parameters, you can pass them:
sudo modprobe my_driver param1=value1 param2=value2
Now, how do you see what’s currently loaded? That’s where lsmod comes in.
lsmod
This command lists all loaded modules, their size in memory, and how many other modules are currently using them (the "refcount"). You’ll see output like this:
Module Size Used by
usbnet 21773 1
r8169 79693 0
mii 10564 1 usbnet,r8169
Here, usbnet is loaded, takes up 21,773 bytes, and is currently used by one other module (itself, in this case, the refcount is for instances of the module being used by other modules). r8169 is a common Ethernet driver, and it also uses mii, which is a generic module for managing Ethernet interfaces. The refcount of 1 for usbnet means it’s loaded but not actively managing any hardware yet. If your USB network adapter were plugged in and recognized, the refcount would likely be higher.
What if you want to know more about a specific module, like its author, license, or what parameters it accepts? That’s what modinfo is for.
modinfo usbnet
This will spit out details:
filename: /lib/modules/5.15.0-76-generic/kernel/drivers/net/usb/usbnet.ko
license: GPL
author: Greg Kroah-Hartman <greg@kroah.com>
description: Generic USB network driver framework
alias: usb-driver-1234:abcd:ef01:*
...
parm: early_init:force early initialization (int)
The parm: lines are crucial. They show you the tunable parameters for the module. If you wanted to force early initialization for usbnet, you could do so when loading it:
sudo modprobe usbnet early_init=1
This tells the usbnet module to attempt to initialize itself as early as possible in the boot process, potentially before other network components are ready.
The real power comes when you start combining these. Suppose you’ve identified a specific USB network card that usbnet supports, but it’s not loading automatically. You might discover (through dmesg logs or vendor documentation) that it requires a specific parameter. You’d then use modinfo to find the parameter name, and modprobe to load it with that parameter.
To remove a module, you use rmmod. For example, to unload usbnet:
sudo rmmod usbnet
This only works if no other module is currently using usbnet (i.e., its refcount is 0). If r8169 was still actively using usbnet, rmmod usbnet would fail with an error. This is a safety mechanism to prevent you from breaking active hardware.
A common pitfall is forgetting that modules can have aliases. An alias lets a module register itself under multiple names. For instance, a specific Wi-Fi chip might have a driver that registers itself as ath5k but also has an alias for phyXXXX (where XXXX is a device ID). modprobe understands these aliases, so you can often load a module by its alias rather than its direct filename, which is why modprobe my_driver works even if the actual module file is named something else.
The depmod command is also vital, though less frequently used interactively. It scans your /lib/modules/<kernel_version>/ directory and builds dependency files (modules.dep) that modprobe uses to quickly figure out module relationships. If you manually install a new module file, you’d run sudo depmod -a to update these dependencies.
When troubleshooting, remember that dmesg is your best friend. It shows kernel ring buffer messages, which include module loading success or failure, errors during module initialization, and hardware detection. If modprobe fails, dmesg will often tell you why by showing the specific error from the module itself or the kernel’s module loading subsystem.
Understanding the interplay between modprobe, lsmod, and modinfo is fundamental to managing your Linux system’s hardware and features dynamically. It allows for a lean kernel and flexible hardware support.
The next thing you’ll likely encounter is configuring modules to load automatically at boot time using configuration files in /etc/modprobe.d/.