Modprobe Command in Linux – A Beginner’s Guide

Modprobe Command In Linux

In this article, we’ll learn about the modprobe command on Linux. The Linux kernel is modular in nature, which means that any end-user can remove the functionality of the kernel or add the functionality of the kernel simply by removing or adding a certain module. This is where modprobe command comes in handy, which makes it easy to add or remove kernel modules. Modprobe looks for modules in the standard 

Also read: Steps to Install Intel Graphic Drivers on Ubuntu 20.04LTS

What is a Linux kernel module?

A kernel module is an object file that contains code that can be loaded and unloaded to the kernel as per need. When loaded, it will extend the kernel functionality without ever needing to reboot the kernel itself.

This way, developers don’t have to write monolithic code for all services and functions. Think of the Linux kernel as a huge lego structure, onto which you can add another lego piece (module) without any difficulty.

Device drivers are a good example of kernel modules.

All the kernel modules on your device are stored in /lib/modules. You can list all the module names by using the following command.

find /lib/modules/$(uname -r)/kernel/ | grep ".ko" | more 
List Kernel Modules

To see which modules are currently loaded, you can use lsmod

Lsmod

What is the modprobe command used for?

The modprobe command is used to remove or insert a module in the Kernel. It looks for modules in the /lib/module/$(uname -r) directory.

How to load a module using modprobe?

First, list all the modules available for your current Kernel version:

find /lib/modules/$(uname -r) -type f -name '*.ko*' | more

You can load a kernel module using the following command along with the module name.

sudo modprobe <kernel module name>hideep.ko

After running this command, you can verify that the module has been loaded by using either the lsmod or insmod command.

lsmod | grep <kernel module name>

If this command returns anything, that means the module has been loaded.

Loading Kernel Module

Here on my system, I have loaded the hideep module, which is a device driver for touchscreens.

How to unload a module?

A kernel module can be unloaded using the -r flag in modprobe.

sudo modprobe -r <kernel module name>

Again to verify that the kernel module has been unloaded, you can run

lsmod | grep <kernel module name>

If everything runs perfectly fine, the above command shouldn’t return anything.

Unload Kernel Module

Useful flags in modprobe

Just like we used the -r flag to remove modules, modprobe has many other flags for other purposes. Some of the important ones are listed below.

-a : This flag allows you to add multiple modules at once 
Load Multiple Modules At Once
--show-depends : This lists all the dependencies of a module 
Show Dependencies Of Kernel Module
-v : If you want to know about what the program is doing in every single step, you can use this command. 
Modprobe Verbose
--first-time : Usually, if you try to add a module which is already loaded or remove a module which is already unloaded, modprobe will do nothing. However, when we use this flag, modprobe will fail. This is good for debugging purposes and to verify wheather a module has already been loaded/unloaded.  
First Time Modprobe

Conclusion

Sometimes when you are searching all the internet to try to fix that one driver issue or sound issue, many tutorials will ask you to load and unload driver modules from the Linux Kernel. In that case, you now know how to do it easily using modprobe. Refer to the official man pages of the command if you want to learn more about modprobe and kernel modules. Thank you and keep exploring.

Also Read

How to change Kernels on Arch Linux

How to upgrade the Kernel o n CentOS