Kernel module basics. Show all, (un)load, blacklist, module info

Modules expand the Linux kernel with new features. It can be device drivers, encryption algorithms, file systems, and more. Modules work in kernel space, so they quickly execute their code. They don’t require kernel compilation, they are compiled separately. So, this is a quick way to expand the Linux kernel. Also unnecessary modules can be unloaded and disabled. So you can dynamically change the capabilities of the kernel.

So let’s start with the basics.

Basics

All modules are in the /lib/modules/ directory. Each kernel version has its own module directory. There are the modules themselves and their configuration files. Let’s see it

 ls /lib/modules/4.18.0-1-amd64/kernel/
...
arch  block  crypto  drivers  fs  lib  mm  net  sound  virt

All modules are sorted by directories.

Basic commands for working with modules

lsmod - show loaded modules
modinfo - information about the module
insmod - load module
rmmod - remove module

The fastest way to show all installed modules for your current Linux Kernel

find /lib/modules/$(uname -r) -name *.ko

If you want to know if a particular module is installed, you can filter the output

find /lib/modules -name *.ko | grep nouveau
...
/lib/modules/4.18.0-1-amd64/kernel/drivers/gpu/drm/nouveau/nouveau.ko

We can find out which kernel modules are currently loaded. We can read the /proc/modules file. It is located in RAM, so it will be a quick way to grab info for bash scripts.

cat /proc/modules
...
acpi_cpufreq 24576 0 - Live 0x0000000000000000
it87 61440 0 - Live 0x0000000000000000
hwmon_vid 16384 1 it87, Live 0x0000000000000000
coretemp 16384 0 - Live 0x0000000000000000
ip_tables 28672 0 - Live 0x0000000000000000
...

The second way to do it use the lsmod command. Its output is more pleasant for eyes. But it requires the use of sudo.

sudo lsmod
...
nouveau              2162688  4
acpi_cpufreq           24576  0
it87                   61440  0
hwmon_vid              16384  1 it87
coretemp               16384  0

To see the description of the module and some other information, use modinfo

sudo modinfo coretemp
...
filename:       /lib/modules/4.18.0-1-amd64/kernel/drivers/hwmon/coretemp.ko
license:        GPL
description:    Intel Core temperature monitor
...

Here we can see the location of the module, its author, license, aliases and other data.

How To Load and Unload Linux kernel modules

By the modprobe command, we can load the kernel module by its name

sudo modprobe coretemp

But there is another way, we can load the kernel module by its directory path, use insmod instead

sudo insmod /lib/modules/4.18.0-1-amd64/kernel/drivers/hwmon/coretemp.ko

modprobe or insmod, which one is better?

It is better to use modprobe, because it loads all dependencies of the module automatically.

How to unload kernel module

If we want to unload a module, we can use the modprobe -r command

sudo modprobe -r vboxdrv
...
modprobe: FATAL: Module vboxdrv is in use.

The error tells us that the module is used by other modules, but doesn’t tell which ones. Let’s use rmmod instead

sudo rmmod vboxdrv
...
rmmod: ERROR: Module vboxdrv is in use by: vboxpci vboxnetadp vboxnetflt

First we need to unload other modules, add their names to the rmmod command

sudo rmmod vboxpci vboxnetadp vboxnetflt

After we unload these modules, we can unload the last one, on which they depended

sudo rmmod vboxdrv

Successfully! Normally, modprobe -r works great, but sometimes something goes wrong.

How to add a kernel module to the black list

This is sometimes required when one module conflicts with another, but you only need one of them. Open /etc/modprobe.d/blacklist.conf file with your favorite text editor

nano /etc/modprobe.d/blacklist.conf

And add the blacklist line there, replace the module name with your

blacklist coretemp

Or check the /etc/modules file, maybe in the past you added a module to it and forgot it. The kernel loads the modules that are listed in this file.

cat  /etc/modules
...
coretemp
it87
Updated: March 31, 2019 — 1:41 pm