5 UNIX / Linux modprobe Command Examples

What is modprobe?
5 modprobe examples
Syntax and Options
Related Commands

What is modprobe?

The modprobe utility is used to add and remove kernel modules to/from linux kernel. Linux kernel modules have .ko as module name extension. ‘modprobe’ is intelligent enough to load the dependency of a kernel module(if any) first and then loads the actual module.

5 modprobe Examples

1. Basic example to load an LKM

In the first example we will see how to load a LKM (kept at any path in system) using modprobe. Use the following steps to achieve this :

sudo ln -s /path/to/your-kernel-module /lib/modules/`uname -r`
sudo depmod -a
sudo modprobe your-kernel-module

for example, what I did was :

 $ sudo ln -s lkm.ko /lib/modules/2.6.32-21-generic/
$ sudo depmod -a
$ sudo modprobe lkm

NOTE: If your module outputs some debug messages then confirmation of the loaded module could be achieved by looking at the logs from dmesg utility. Alternatively, the lsmod utility can be used to view the currently loaded modules

2. Unload a loaded using modprobe

The modprobe can also be used to remove the loaded module. Or in other words we can unload a loaded module through modprobe using the -r option.

$ sudo modprobe -r lkm

The above command unloads a currently loaded module ‘lkm’ from kernel.

3. Have a dry run

Sometimes we face problems like module not being loaded properly etc. In that case it becomes very important to debug and know the level at which the problem exists. It becomes crucial to know whether the problem is before loading or after loading. To facilitate this type of debugging, there exists option -n which if used, forces modprobe to do everything else except the final stage of loading the module.

$ sudo modprobe -vn lkm
insmod /lib/modules/2.6.32-21-generic/kernel/arch/x86/kernel/lkm.ko

I used the -v option along with -n so that some debugging info could be spitted out by modprobe.

4. Suppress the error information

Usually in some error condition, the modprobe utility would try to output some error info. If that kind of info is not needed, then the -q option is used to suppress this kind of info.

$ sudo modprobe  lk
FATAL: Module lk not found.
$ sudo modprobe -q lk
$

So we see that in the above output, when the command was run without the -q option then an error was thrown while when -q was used the error got suppressed.

5. List the modules

If there is a requirement to list all the modules or modules with specific name then modprobe provides -l option to accomplish this

$ modprobe -l crc*
kernel/arch/x86/crypto/crc32c-intel.ko
kernel/crypto/crc32c.ko
kernel/lib/crc-ccitt.ko
kernel/lib/crc-itu-t.ko
kernel/lib/crc7.ko
$ modprobe -l rds
kernel/net/rds/rds.ko

Syntax and Options

modprobe [ -v ] [ -V ] [ -C config-file ] [ -n ] [ -i ] [ -q ] [ -b ] [ -o modulename ] [ modulename ] [ module parameters... ]

modprobe [ -r ] [ -v ] [ -n ] [ -i ] [ modulename... ]

modprobe [ -l ] [ -t dirname ] [ wildcard ]

modprobe [ -c ]

modprobe [ --dump-modversions ] [ filename... ]
Short Option Long Option Option Description
-v –verbose Print messages about what the program is doing. Usually modprobe only prints messages if something goes wrong.This option is passed through install or remove commands to other modprobe commands in the MODPROBE_OPTIONS environment variable.
-C –config This option overrides the default configuration directory/file (/etc/modprobe.d or /etc/modprobe.conf). This option is passed through install or remove commands to other modprobe commands in the MODPROBE_OPTIONS environment variable.
-c –showconfig Dump out the effective configuration from the config directory and exit.
-n –dry-run This option does everything but actually insert or delete the modules (or run the install or remove commands). Combined with -v, it is use‐ful for debugging problems.
-i –ignore-install –ignore-remove This option causes modprobe to ignore install and remove commands in the configuration file (if any) for the module specified on the command line (any dependent modules are still subject to commands set for them in the configuration file).
-q –quiet Normally modprobe will report an error if you try to remove or insert a module it can’t find (and isn’t an alias or install/remove command). With this flag, modprobe will simply ignore any bogus names (the kernel uses this to opportunistically probe for modules which might exist).
-r –remove This option causes modprobe to remove rather than insert a module. If the modules it depends on are also unused, modprobe will try to remove them too. Unlike insertion, more than one module can be specified on the command line (it does not make sense to specify module parameters when removing modules).There is usually no reason to remove modules, but some buggy modules require it. Your kernel may not support removal of modules.
-f –force Try to strip any versioning information from the module which might otherwise stop it from loading: this is the same as using both –force-vermagic and –force-modversion. Naturally, these checks are there for your protection, so using this option is dangerous.
This applies to any modules inserted: both the module (or alias) on the command line and any modules it on which it depends.

Related Commands

modprobe.conf
lsmod
modinfo