The Linux Kernel is a mystery to most, but it need not be! Welcome to the exciting world of lsmod, a tool that lets you peek at what modules the Kernel is using, and more!
What Is lsmod?
lsmod, the tool which allows one to list modules in the Linux Kernel, one first has to understand what a module is. A Linux module is a modular piece of code (running in a compiled format) that can be loaded or unloaded from the Linux kernel at will. The main advantage of using such modules is their modularity and granularity of them.
You can think about Kernel modules as plugins in other software. Whenever you need a specific – less common, or not universal – functionality, it will be available as a plugin, or in the case of the Linux Kernel as a module. There is little difference between a plugin in other software and Linux Kernel modules. However, the level at which they operate is much closer to the hardware and core operating software, rather than at the end of the stack at the user level.
For example, your graphics card driver – especially if you are using NVidia drivers – is loaded as a Kernel module. When you install the driver, a kernel module is configured and loaded (possibly on the next restart of your system).
Coming back to
lsmod, which should be included in the default installation of your Linux operating system, this tool will list an overview of kernel modules that are currently available.
lsmod: First Looks
lsmod is a tool which renders the contents of the (virtual)
/proc/modules file into more human readable content.
/proc/modules file is a 0-byte virtual file which, when viewed with for example
vi, will contain (or better render) a list of modules currently available for the Linux Kernel on the system where
lsmod is being executed.
Let’s start by checking the contents of the
/proc/modules file for the presence of a AHCI (Advanced Host Controller Interface) Linux Kernel module:
cat /proc/modules | grep ahci
Here we use
cat to display (render) the contents of
/proc/modules, and subsequently use
grep to grep for the AHCI module. Note that we did not specify the
-i option (which would do a case-insensitive search) to
grep, as the full list of modules
/proc/modules is lowercase only.
We can see from the output provided what AHCI Linux Kernel modules are currently available. Otherwise, the output looks a bit cryptic and not very descriptive. Let’s try
lsmod instead now.
lsmod | grep -E '^Module|ahci'
Here we used
lsmod, a tool with no options, and used
grep to grab multiple outcomes of interest. The
-E option to grep allows us to make a selector/selection list (strings) of items we want to see, all separated by one or more pipe symbol (
Here we thus are looking for any
ahci occurrences (in the second selector/selection string), and any
^Module occurrences (in the first selector/selection string), where the
^ is a regular expression (applicable to the first selector only), selecting the start of a line, indicating we want to only see results where the start of the line is immediately followed by ‘Module’ and this would be our first header line.
We see the output now nicely rendered; the first line (which our
grep command included as thanks to the
^Module selector) and the second and third line listing the
ahci modules which our
grep selected due to the second selector.
The header column outputs tell us what each column contains; the first column the module name (Module), the second the size is bytes (Size), and the third column provides a reference count (Used by), or in other words how many times a module is referenced (which can perhaps vaguely be translated to used) by the kernel.
If the reference count in the third column is zero, it means that while the module is available “in”/to the Kernel, it is not currently being used.
Tip: if you ever see
-2 in the output of
lsmod, you may want to checkout lsmod shows -2 in the “Used by” column on Stackexchange.
Diving Deeper With modinfo
lsmod tool has no options itself, we can use a another utility in combination with lsmod to discover more information:
modinfo ahci -F 'description' modinfo ahci -F 'author'
Here we used the
modinfo command to find out more information about the
ahci Linux Kernel module. In particular, we used the
-F option to
modinfo to select a specific field for which we would like to learn it’s contents. Note that if you execute
modinfo ahci without any options, you will see a full list of all available information on the
ahci Kernel module. This is output is quite verbose as it includes signing key data etc.
We could have also used
grep similarly as we did earlier to select both pieces of information in a single command. To do so, we use
modinfo ahci | grep -E 'description|author', which renders the same output of two lines, albeit directly under each other.
Note also that one could again use the regular expression
' to select only the output strings which start at the beginning of the line (for both fields being searched for), i.e.
modinfo ahci | grep -E '^description|^author'
In contrast to
modinfo has some further options which may of interest. Checkout
man modinfoto see all available options.
In this article, we explored the option-less
lsmod tool and the handy output it provides us with, showing us which Linux Kernel modules are available and how many times each such Kernel Module is currently being referenced (“used”) by the Kernel. We also looked at how to obtain more information about a specific module by using the
modinfo command in Linux. We also looked at how we can use
grep -E to obtain two pieces of information at once by using multiple selection strings to scan for.
If you enjoyed reading this article, have a look at our How to Use ltrace to Trace Library Calls and How to Work with Shared Object (Library) Dependencies in Linux articles. Enjoy!