How To Use The Modprobe Command In Linux

February 11, 2021

Introduction

The Linux kernel has a modular design. Functionality is extendible with modules or drivers. Use the modprobe command to add or remove modules on Linux. The command works intelligently and adds any dependent modules automatically.

The kernel uses modprobe to request modules. The modprobe command searches through the standard installed module directories to find the necessary drivers.

This article explains how to use modprobe to add or remove kernel modules.

How to Use the modprobe Command in Linux

Prerequisites

  • A system running Linux
  • Access to the terminal/command line
  • A user account with sudo or root privileges

modprobe Command Syntax Explained

All modprobe commands require sudo privileges. The general syntax for using modprobe is:

sudo modprobe <options> <module name>

By default, the modprobe command adds a module.

For multiple modules, expand with the option -a or -all:

sudo modprobe <options> -a <first module name> <second module name>

Options for modprobe Command

The available modprobe command options are divided into categories based on their use-case.

Management Options

Management options enable different module handling situations when inserting or removing modules with the modprobe command.

--all
-a
Enables multiple modules to be inserted or removed at the same time.
--remove
-r
Remove a module. Applies --remove-dependencies as well. Useful for removing broken modules.
--remove-dependenciesRemoves dependency modules.
--resolve-alias
-R
Look up and print all module names matching an alias. Useful for debugging alias problems.
--first-timePrints an error for already inserted or removed modules.
--ignore-install
--ignore-remove
-i
Ignore install/remove commands written in the module when inserting/removing a module.
--use-blacklist
-b
Blacklist resolved alias. Blacklisted modules are not automatically loaded.
--force
-f
Force module insertion or removal when version errors appear. Applies both --force-modversion and --force-vermagic. Use with caution.
--force-modversionIgnore module version on insertion or removal. Use with caution.
--force-vermagicIgnore module version magic on insertion or removal. Use with caution.

Query Options

Query options for modprobe show information about configuration and dependencies.

--show-depends
-D
Lists the module with the dependency files if there are any. The dependencies that install with the module have the “install” prefix.
--showconfig
--show-config
-c
Prints current configuration and exists.
--show-modversions
--dump-modversions
Dumps module version dependencies.

General Options

General options configure modprobe output options, module locations, and versions.

--dry-run
--show
-n
Do not execute insert/remove but print the output. Used for debugging purposes.
--config=<file name>
-C
Overrides the default configuration dependency (/etc/modprobe.d) with <file name>.
--dirname=<directory>
-d
Use <directory> as filesystem root for /lib/modules.
--set-version=<version>
-S
Use specified kernel <version> instead of using uname.
--syslog
-s
Prints the error messages through syslog instead of standard error (stderr). When stderr is unavailable, errors get printed to syslog automatically.
--quiet
-q
Disables display of error messages.
--verbose
-v
Enables more messages to show, if available. modprobe only prints messages if something goes wrong.
--version
-V
Shows the modprobe version.
--help
-h
Shows the help message with all the commands listed.

Examples of modprobe Command

All kernel modules are listed in the /lib/modules directory system in .ko (kernel object) files by default.

Find all the available modules for the current kernel version with:

find /lib/modules/$(uname -r) -type f -name '*.ko*' | more
Terminal output of available modules using the find command

Note: Consider removing old kernel versions. Check out our guide on how to remove old kernels on Ubuntu.

Adding Kernel Modules With modprobe Command

1. Add a module using the modprobe command:

sudo modprobe <module name>

For example:

sudo modprobe torture
Adding a module using the modprobe command in terminal

2. Confirm the module loaded with:

sudo modprobe <module name> --first-time

The output prints an error because the module is already in the kernel.

Terminal output of adding module with modprobe --first-time error

Alternatively, find the module in the active module loaded list with lmod:

lsmod | grep <module name>

For example:

lsmod | grep torture
Terminal output of lsmod and grep module

Removing Kernel Modules With modprobe Command

1. Remove a module using the modprobe -r command:

sudo modprobe -r <module name>

For example:

sudo modprobe -r torture

2. Confirm the module is removed by running:

sudo modprobe -r <module name> --first-time

An error message appears saying that the module is not in the kernel:

Terminal output of removing module with modprobe -r --first-time error

Alternatively, check the active module loaded list:

lsmod | grep <module name>

The removed module is not on the module loaded list.

Conclusion

The Linux kernel is created to be modular and easily extendible. Make sure to research the modules you want to add or remove to avoid problems with the kernel.

For further reading, follow our guide on how to build a Linux kernel from scratch.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to Build Linux Kernel From Scratch {Step-By-Step Guide}
November 12, 2020

All Linux distributions come with a predefined kernel. However, they are usually outdated. Follow this...
Read more
Linux Kernel 5.7 Released: 12 New Features to Consider
June 3, 2020

Linux released its latest LTS kernel version, the Linux kernel 5.7. Take a look at all the new features and...
Read more
How to Remove Old Kernels on Ubuntu 16.04, 18.04, & 19.04
November 7, 2024

There are several methods to remove old or unused kernels. It's also considered good system hygiene practice...
Read more
How to Check Kernel Version in Linux in Command Line
April 18, 2024

The Linux kernel is much like the central brain of the operating system. Although it is open-source - meaning...
Read more