Howto: Display List of Modules or Device Drivers In the Linux Kernel
H ow do I display the list of loaded Linux Kernel modules or device drivers on Linux operating systems?
You need to use lsmod program which show the status of loaded modules in the Linux Kernel. Linux kernel use a term modules for all hardware device drivers.
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | Yes |
Requirements | lsmod |
Time | Less than a one minute |
Please note hat lsmod is a trivial program which nicely formats the contents of the /proc/modules , showing what kernel modules are currently loaded.
This is an important task. With lsmod you can verify that device driver is loaded for particular hardware. Any hardware device will only work if device driver is loaded.
Task: List or display loaded modules
Open a terminal or login over the ssh session and type the following command
$ less /proc/modules
Sample outputs:
To see nicely formatted output, type:
$ lsmod
Sample outputs:
First column is Module name and second column is the size of the modules i..e the output format is module name, size, use count, list of referring modules.
Finding more info about any module or driver
Type the following command:
# modinfo driver-Name-Here
# modinfo thermal_sys
# modinfo e1000e
Sample outputs:
Is there a standard directory in Linux where my driver files are stored?
echo «Kernel drivers dir: \»/lib/modules/$(uname -r)/kernel/drivers/\» \ for Linux kernel version \»$(uname -r)\» «
To view drivers, enter:
ls -l /lib/modules/$(uname -r)/kernel/drivers/
How to list all loadable kernel modules?
I’m looking for a few kernel modules to load i2c-dev and i2c-bcm2708 . But the modprobe command returns:
How can I list all the available modules in the system? In which directory are they located?
4 Answers 4
By default modprobe loads modules from kernel subdirectories located in the /lib/modules/$(uname -r) directory. Usually all files have extension .ko , so you can list them with
or, taking into account compressed files:
Each module can be also loaded by referring to its aliases, stored in the /lib/modules/$(uname -r)/modules.alias (and modules.alias.bin ).
However, to load a modules successfully modprobe needs their dependencies listed in the file /lib/modules/$(uname -r)/modules.dep (and a corresponding binary version modules.dep.bin ). If some module is present on the system, but is not on the list, then you should run a command depmod which will generate such dependencies and automatically include your module to modules.dep and modules.dep.bin .
Additionally, if the module is successfully loaded it will be listed in the file /proc/modules (also accessed via command lsmod ).
Kernel module
Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system.
To create a kernel module, you can read The Linux Kernel Module Programming Guide. A module can be configured as built-in or loadable. To dynamically load or remove a module, it has to be configured as a loadable module in the kernel configuration (the line related to the module will therefore display the letter M ).
Contents
Obtaining information
Modules are stored in /usr/lib/modules/kernel_release . You can use the command uname -r to get your current kernel release version.
To show what kernel modules are currently loaded:
To show information about a module:
To list the options that are set for a loaded module:
To display the comprehensive configuration of all the modules:
To display the configuration of a particular module:
List the dependencies of a module (or alias), including the module itself:
Automatic module loading with systemd
Today, all necessary modules loading is handled automatically by udev, so if you do not need to use any out-of-tree kernel modules, there is no need to put modules that should be loaded at boot in any configuration file. However, there are cases where you might want to load an extra module during the boot process, or blacklist another one for your computer to function properly.
Kernel modules can be explicitly listed in files under /etc/modules-load.d/ for systemd to load them during boot. Each configuration file is named in the style of /etc/modules-load.d/
.conf . Configuration files simply contain a list of kernel modules names to load, separated by newlines. Empty lines and lines whose first non-whitespace character is # or ; are ignored.
See modules-load.d(5) for more details.
Manual module handling
Kernel modules are handled by tools provided by kmod package. You can use these tools manually.
To load a module by filename (i.e. one that is not installed in /usr/lib/modules/$(uname -r)/ ):
To unload a module:
Setting module options
To pass a parameter to a kernel module, you can pass them manually with modprobe or assure certain parameters are always applied using a modprobe configuration file or by using the kernel command line.
Manually at load time using modprobe
The basic way to pass parameters to a module is using the modprobe command. Parameters are specified on command line using simple key=value assignments:
Using files in /etc/modprobe.d/
Files in /etc/modprobe.d/ directory can be used to pass module settings to udev, which will use modprobe to manage the loading of the modules during system boot. Configuration files in this directory can have any name, given that they end with the .conf extension. The syntax is:
Using kernel command line
If the module is built into the kernel, you can also pass options to the module using the kernel command line. For all common bootloaders, the following syntax is correct:
Simply add this to your bootloader’s kernel-line, as described in Kernel Parameters.
Aliasing
Aliases are alternate names for a module. For example: alias my-mod really_long_modulename means you can use modprobe my-mod instead of modprobe really_long_modulename . You can also use shell-style wildcards, so alias my-mod* really_long_modulename means that modprobe my-mod-something has the same effect. Create an alias:
Some modules have aliases which are used to automatically load them when they are needed by an application. Disabling these aliases can prevent automatic loading but will still allow the modules to be manually loaded.
Blacklisting
Blacklisting, in the context of kernel modules, is a mechanism to prevent the kernel module from loading. This could be useful if, for example, the associated hardware is not needed, or if loading that module causes problems: for instance there may be two kernel modules that try to control the same piece of hardware, and loading them together would result in a conflict.
Some modules are loaded as part of the initramfs. mkinitcpio -M will print out all automatically detected modules: to prevent the initramfs from loading some of those modules, blacklist them in a .conf file under /etc/modprobe.d and it shall be added in by the modconf hook during image generation. Running mkinitcpio -v will list all modules pulled in by the various hooks (e.g. filesystems hook, block hook, etc.). Remember to add that .conf file to the FILES array in /etc/mkinitcpio.conf if you do not have the modconf hook in your HOOKS array (e.g. you have deviated from the default configuration), and once you have blacklisted the modules regenerate the initramfs, and reboot afterwards.
Using files in /etc/modprobe.d/
Create a .conf file inside /etc/modprobe.d/ and append a line for each module you want to blacklist, using the blacklist keyword. If for example you want to prevent the pcspkr module from loading:
However, there is a workaround for this behaviour; the install command instructs modprobe to run a custom command instead of inserting the module in the kernel as normal, so you can force the module to always fail loading with:
This will effectively blacklist that module and any other that depends on it.
Using kernel command line
You can also blacklist modules from the bootloader.
Simply add module_blacklist=modname1,modname2,modname3 to your bootloader’s kernel line, as described in Kernel parameters.
Troubleshooting
Modules do not load
In case a specific module does not load and the boot log (accessible with journalctl -b ) says that the module is blacklisted, but the directory /etc/modprobe.d/ does not show a corresponding entry, check another modprobe source folder at /usr/lib/modprobe.d/ for blacklisting entries.
A module will not be loaded if the «vermagic» string contained within the kernel module does not match the value of the currently running kernel. If it is known that the module is compatible with the current running kernel the «vermagic» check can be ignored with modprobe —force-vermagic .
How to List/Load/Unload Linux Kernel Module
Linux is a monolithic kernel. A monolithic kernel is a kernel architecture in which all the services of process management, filesystem management, I/O and device management run in the kernel space, i.e. the kernel is responsible for handling these services. So these are loaded into memory with the kernel, at the time of booting up. With this approach, adding new components into the kernel becomes difficult and complex, because the kernel would have to be recompiled with new components and then loaded into the memory. However, the Loadable Kernel Modules (LKMs) supported by Linux (and some other monolithic kernels), can load and remove from kernel dynamically.
The kernel image needs to be as light as possible. Moreover, not all the drivers can be built into the kernel. The modules in linux can be some third party hardware drivers, drivers for some filesystems, or some other components (for example, there is a module for iptables in linux). Although the kernel daemon loads and unloads these modules for you automatically, but if you want, you can do it yourself, or if there is a module that kernel is not able to load by itself, it must be loaded manually (and unloaded when it is no longer needed).
Modules in Linux
The modules are called Kernel objects in Linux. They are present in the system as .ko files. They are located in the subdirectories of a directory named as your Kernel version number under /lib/modules. The Kernel version can be checked with uname command.
In the example above, the modules are present under the directory named ‘2.6.13-generic’ on my machine.
To see the .ko files, we will need to look in the subdirectories. For example,
List the loaded Modules
The modules that are loaded into the kernel can be viewed by «lsmod» command. It will list all modules loaded currently.
Load a new module
Any module that you wish to load into kernel can be loaded using the command ‘modprobe’ or ‘insmod’. However, ‘modprobe’ command is more powerful, so you might want to use ‘modprobe’ instead of ‘insmod’. Moreover, if there is any dependency in the module that we wish to load, ‘modprobe’ takes care of it. So for the purpose of demonstration, we will use ‘modprobe’.
The module for iptables (ip_tables) has not been loaded into memory yet. This can be checked in the output above, or we can search for it using grep.
This command returns nothing. So we will load the corresponding module using ‘modprobe’ as:
Here, we wished to load ‘ip_tables’ module and ‘x_tables’ was its dependency that was taken care of by ‘modprobe’.
Unload module
When we no longer need the module, it can be removed using the command ‘rmmod’ or ‘modprobe -r’. Once again, ‘modprobe’ will unload any dependencies as well. Note that a module will not be unloaded unless it is not in use by any other module. If we try to remove ‘x_tables’ module before ‘ip_tables’, we get an error, because ‘x_tables’ is in use by ‘ip_tables’.
Now if we remove ip_tables module with ‘modprobe’, it will remove ‘x_tables’ as well.
Get module information
The command ‘modinfo’ displays information about a module:
Conclusion
In this tutorial, we learned commands used in linux to list, load and unload kernel modules. Hope you enjoyed reading this and please leave your suggestion in the below comment section.