28 Managing kernel modules #
Although Linux is a monolithic kernel, it can be extended using kernel modules. These are special objects that can be inserted into the kernel and removed on demand. In practical terms, kernel modules make it possible to add and remove drivers and interfaces that are not included in the kernel itself. Linux provides several commands for managing kernel modules.
28.1 Listing loaded modules with lsmod and modinfo #
Use the lsmod
command to view what kernel modules are
currently loaded. The output of the command may look as follows:
>
lsmod
Module Size Used by
snd_usb_audio 188416 2
snd_usbmidi_lib 36864 1 snd_usb_audio
hid_plantronics 16384 0
snd_rawmidi 36864 1 snd_usbmidi_lib
snd_seq_device 16384 1 snd_rawmidi
fuse 106496 3
nfsv3 45056 1
nfs_acl 16384 1 nfsv3
The output is divided into three columns. The Module
column lists the names
of the loaded modules, while the Size
column displays the size of each
module. The Used by
column shows the number of referring
modules and their names. This list may be incomplete.
To view detailed information about a specific kernel module, use the
modinfo MODULE_NAME
command, where
MODULE_NAME is the name of the desired kernel
module. The modinfo
binary resides in the
/sbin
directory that is not in the user's PATH
environment variable. This means that you must specify the full path to the
binary when running modinfo
command as a regular user:
>
/sbin/modinfo kvm
filename: /lib/modules/6.4.0-150600.9-default/kernel/arch/x86/kvm/kvm.ko.zst
license: GPL
author: Qumranet
suserelease: SLE15-SP6
srcversion: 9DACE73AC65F98D556DAD60
depends: irqbypass
supported: yes
retpoline: Y
intree: Y
name: kvm
vermagic: 6.4.0-150600.9-default SMP mod_unload modversions
28.2 Adding and removing kernel modules #
While it is possible to use insmod
and
rmmod
to add and remove kernel modules, it is recommended to use the
modprobe
tool instead. modprobe
offers several
important advantages, including automatic dependency resolution and
blacklisting.
When used without any parameters, the modprobe
command installs a specified kernel module. modprobe
must be run with root privileges:
>
sudo
modprobe acpi
To remove a kernel module, use the -r
parameter:
>
sudo
modprobe -r acpi
28.2.1 Loading kernel modules automatically on boot #
Instead of loading kernel modules manually, you can load them
automatically during the boot process using the
systemd-modules-load.service
service. To enable a
kernel module, add a .conf
file to the
/etc/modules-load.d/
directory. It is good practice
to give the configuration file the same name as the module, for example:
/etc/modules-load.d/rt2800usb.conf
The configuration file must contain the name of the desired kernel
module (for example, rt2800usb
).
The described technique allows you to load kernel modules without any
parameters. If you need to load a kernel module with specific options,
add a configuration file to the /etc/modprobe.d/
directory instead. The file must have the .conf
extension. The name of the file should adhere to the following naming convention:
priority-modulename.conf
, for example:
50-thinkfan.conf
. The configuration file must
contain the name of the kernel module and the desired parameters. You can use the
example command below to create a configuration file containing the name of the kernel module and its parameters:
>
echo "options thinkpad_acpi fan_control=1" | sudo tee /etc/modprobe.d/thinkfan.conf
Most kernel modules are loaded by the system automatically when a
device is detected or user space requests specific
functionality. Thus, adding modules manually to
/etc/modules-load.d/
is rarely required.
28.2.2 Blacklisting kernel modules with modprobe #
Blacklisting a kernel module prevents it from loading during the boot
process. This can be useful when you want to disable a module that you
suspect is causing problems on your system. You can still load blacklisted
kernel modules manually using the
insmod
or modprobe
tools.
To blacklist a module, create a file
/etc/modprobe.d/60-blacklist-MODULE_NAME.conf
with the following content:
blacklist MODULE_NAME
Run the dracut
command as root to generate a new initrd
image, then reboot your machine (replace NAME
with the name of the current initrd and
KERNELVERSION with the currently running kernel):
>
su
echo "blacklist nouveau" >> /etc/modprobe.d/60-blacklist-nouveau.conf
/usr/bin/dracut --logfile /var/log/YaST2/mkinitrd.log --force /boot/$initrd-NAME $KERNELVERSION
reboot
To disable a kernel module temporarily only, blacklist it on-the-fly during the boot. To do this, press the E key when you see the boot screen. This drops you into a minimal editor that allows you to modify boot parameters. Locate the line that looks as follows:
linux /boot/vmlinuz...splash= silent quiet showopts
Add the
modprobe.blacklist=MODULE_NAME
command to the end of the line. For example:
linux /boot/vmlinuz...splash= silent quiet showopts modprobe.blacklist=nouveau
Press F10 or Ctrl–X to boot with the specified configuration.
To blacklist a kernel module permanently via GRUB, open the
/etc/default/grub
file for editing, and add the
modprobe.blacklist=MODULE_NAME
option to the GRUB_CMDLINE_LINUX
command. Then run the
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
command to enable
the changes.