Modprobe Command in Linux with Examples

FREE Online Courses: Your Passport to Excellence - Start Now

In this article, you will learn what is modprobe command in Linux. We will look into the uses of modprobe, the syntax of the modprobe command, and the different types of options that are used with the modprobe. In the end, we will also be looking into some practical examples in the terminal using the modprobe command.’

What is modprobe?

Modprobe is a command-line-based utility in Linux based operating system that helps in removing and adding modules from the kernel. This utility was written by Rusty Russel and is used to add a loadable module into the Linux kernel or remove a module from the Linux kernel.

We all know that the Linux kernel is the sole heart of Linux-based operating systems as it acts as a mediator between the hardware and the software. The kernel has a modular design.

A kernel module, or most commonly called ‘driver’ is a piece of code that extends or improves the kernel’s functionality. Modules are of 2 types, either built-in modules or laudable modules.

Loadable modules can be loaded and unloaded into the kernel, without rebooting the system. This is precisely where modprobe comes into the picture as it helps us load (and unload) a module into the kernel manually.

Now a doubt may arise as to where are these loadable modules? Well, the modules are stored in the “/lib/modules/<kernel version>” directory on your computer. Only those users with administrative privileges can manage kernel modules.

Syntax of the modprobe command

Using the modprobe command, we can load or unload either a single module or multiple modules. The general syntax for using modprobe is:

sudo modprobe <options> <module name>

If you want to work with multiple modules, you use the option “-a” which stands for “-all” and specify the names of the multiple modules using the following syntax:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

sudo modprobe <options> -a <module name 1> <module name 2>

Options used with the modprobe command

When we speak of the options used with the modprobe command, there are generally 3 types of options used with it:

1. Management options

2. Query options

3. General option

Let us take a closer look at each of the option type used with the modprobe command.

1. Modprobe Management options

Management options are the options that enable different module handling situations when inserting or removing modules using the modprobe command. Let us take a look at the different management options available:

a. -a

This option enables multiple modules to be inserted or removed at the same time. You can also write this option as “–all”.

b. -r

This option removes a module. You can also write this option as “–remove”.

c. –remove-dependencies

This option removes dependency modules.

d. -R

This option looks up and prints all the module names matching the alias. You can also write this option as “–resolve-alias”.

e. –first-time

This option prints an error for already inserted or removed modules.

f. -i

This option ignores the install/removes commands written in the module when inserting/removing a module. You can also write this option as “–ignore-install” or “–ignore-remove”.

g. -b

This option blacklist resolves aliases. Blacklisted modules don’t get automatically loaded. You can also write this option as “–use-blacklist”

h. -f

This option forces module removal or insertion when version errors appear. You can also write this option as “–force”

g. –force-modversion

This option ignores the version of the module during insertion or removal of the module.

h. –force-vermagic

This option ignores the module version magic during insertion or removal of a module.

2. Modprobe Query options

The query options show information about configuration and dependencies. Let us take a look at all of the query options available:

a. -D

This option lists the modules with the dependency files if there are any. These dependencies that install along with the module have the prefix ‘install’. You can also write this option as “–show-depends”.

b. -C

This option prints the current configuration. You can also write this option as “–showconfig” or “–show-config”.

c. –dump-modversions

This option dumps the module version dependencies. You can also write this option as “–show-modversions”.

3. Modprobe General options

The general options help us configure the output options, module locations, and versions of the modules. Let us look into the different general options available:

a. -n

This option prints the output without executing insertion or removal commands. You can also write this option as “–show” or “–dry-run”.

b. -C

This option overrides the default configuration dependency. You can also write this option as “–config=<file name>”.

c. -d

This option uses the directory you specified as filesystem root for /lib/modules. You can also write this option as “–dirname=<directory>”.

d. -S

This option uses a specific kernel version you specify instead of using the uname. You can also write this option as “–set-version=<version>”.

e. -s

This option prints the error messages through Syslog instead of standard error, which is denoted by stderr. When the standard error is unavailable, the error gets printed to Syslog automatically. You can also write this option as “–syslog”.

f. -q

This option disables the display of error messages. You can also write this option as “–quiet”.

g. -v

This option provides a verbose for a better understanding of what is happening. You can also write this option as “–verbose”

h. -V

This option displays the modprobe version you currently have. You can also write this option as “–version”

i. -h

This option shows the help message with all the options, information, and commands regarding modprobe. You can also write this option as “–help”.

Now that you know how to use the modprobe command as we have see the syntax and the different options, let us take a look at some practical examples like inerting and removing modules.

Finding available modules

Before we add or remove modules, we need to look at the modules that are in the computer, so that we can choose which module to add. As discussed above, the modules are present in the “/lib/modules/<kernel version>” directory.

To find these modules we are going to use the find command as below:

find /lib/modules/$(unam -r) -type f -name ‘*.ko’ | more

finding available modules

Adding modules into the kernel

Let us now add a module into the kernel using the syntax

“sudo modprobe <module name>”

adding modules into the kernel

Checking if module is added to the kernel

Now that we have added our module into the kernel, we can now check if it has been successfully added into the kernel or not using the option “–first-time”.

checking if the module is added to the kernel

You also use this option to check if a module is already added to the kernel before reading it agin.

Finding the module using lsmod

You can also check if the module is in the active module loaded list. We can combine grep, pipe and lsmode like this:

lsmod | grep <module name>

finding the module using lsmod

Removing a module from the kernel

To remove a module from the kernel, use the option “-r” like this:

sudo modprobe -r soundcore

removing a module from the kernel

Checking if module has been succedully removed

Now that we have removed our module from the kernel, we can now check if it has been successfully removed or not by using the option “–first-time”.

checking if module has been successfully removed

Summary

As you have seen, Linux modprobe tool simplifies the process of adding and removing modules without rebooting the system. Research befor the insertion and removal of modules to avoid kernel promlesm, because sometimes you may add unwanted modules or remove important ones.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *