How to check if a kernel module is loaded in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check the status of kernel modules in Linux. You will explore three essential methods: listing currently loaded modules using lsmod, checking if a module can be loaded and its dependencies without actually loading it using modprobe --dry-run, and viewing detailed information about a specific module with modinfo. These techniques are crucial for understanding which kernel components are active and managing module dependencies on your system.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux(("Linux")) -.-> linux/PackagesandSoftwaresGroup(["Packages and Softwares"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/SystemInformationandMonitoringGroup -.-> linux/ps("Process Displaying") linux/SystemInformationandMonitoringGroup -.-> linux/service("Service Managing") linux/PackagesandSoftwaresGroup -.-> linux/software("Linux Software") subgraph Lab Skills linux/echo -.-> lab-558721{{"How to check if a kernel module is loaded in Linux"}} linux/ls -.-> lab-558721{{"How to check if a kernel module is loaded in Linux"}} linux/ps -.-> lab-558721{{"How to check if a kernel module is loaded in Linux"}} linux/service -.-> lab-558721{{"How to check if a kernel module is loaded in Linux"}} linux/software -.-> lab-558721{{"How to check if a kernel module is loaded in Linux"}} end

List loaded modules with lsmod

In this step, you'll learn how to list the currently loaded kernel modules using the lsmod command.

The Linux kernel is the core of the operating system. It manages the system's resources and provides a bridge between the hardware and the software. Kernel modules are pieces of code that can be loaded and unloaded into the kernel on demand. They extend the kernel's functionality without requiring a system reboot. This is particularly useful for adding support for new hardware or filesystems.

To see which modules are currently loaded into the kernel, open your terminal and type the following command:

lsmod

Press Enter.

You will see a list of modules, their size, and how many other modules are using them. The output will look something like this (the exact list will vary depending on the system and loaded drivers):

Module                  Size  Used by
fuse                  143360  3
snd_seq_midi           20480  0
snd_seq_midi_event     16384  1 snd_seq_midi
snd_rawmidi            40960  1 snd_seq_midi
snd_seq                77824  3 snd_seq_midi,snd_seq_midi_event,snd_rawmidi
snd_seq_device         16384  3 snd_seq,snd_seq_midi,snd_rawmidi
... (many more lines)

Let's break down the columns:

  • Module: The name of the kernel module.
  • Size: The size of the module in bytes.
  • Used by: The number of other modules that are currently using this module. If this number is 0, the module is not currently being used by any other module.

This command is essential for understanding which drivers and kernel components are active on your system.

Now, try running the lsmod command in your terminal.

Click Continue to proceed to the next step.

Check module status with modprobe --dry-run

In this step, you'll learn how to use the modprobe command with the --dry-run option to check if a module can be loaded and what other modules it depends on, without actually loading it.

The modprobe command is used to add or remove modules from the Linux kernel. It's more sophisticated than the older insmod and rmmod commands because it understands module dependencies. When you try to load a module with modprobe, it will automatically load any other modules that the requested module requires.

The --dry-run option is very useful for testing. It tells modprobe to go through the process of resolving dependencies and checking if the module can be loaded, but it won't actually load the module into the kernel. This allows you to see if there are any issues or missing dependencies before making changes to the running system.

Let's try checking the status of a common module, for example, the fuse module, which is often used for user-space filesystems.

Type the following command in your terminal:

modprobe --dry-run fuse

Press Enter.

If the fuse module and its dependencies are available, you might see output similar to this:

modprobe: INFO: could not insert 'fuse': File exists

This output indicates that the fuse module is likely already loaded (as seen in the previous lsmod step), and modprobe is reporting that it cannot insert it again because it already exists. The --dry-run option still performs the dependency check even if the module is already loaded.

If the module wasn't loaded and could be loaded, the output might be empty or indicate the modules that would be loaded. If there were issues, modprobe would report them here.

Using --dry-run is a safe way to test module loading without affecting your system's state.

Now, try running the modprobe --dry-run fuse command yourself.

Click Continue to move on.

View module details with modinfo

In this step, you'll learn how to get detailed information about a specific kernel module using the modinfo command.

While lsmod shows you which modules are loaded, modinfo provides much more detail about a module file, whether it's loaded or not. This information includes the module's author, description, license, parameters, and dependencies.

Let's use modinfo to inspect the fuse module again.

Type the following command in your terminal:

modinfo fuse

Press Enter.

You will see a comprehensive output about the fuse module, similar to this:

filename:       /lib/modules/5.15.0-113-generic/kernel/fs/fuse/fuse.ko
license:        GPL
description:    Filesystem in Userspace
author:         Miklos Szeredi <[email protected]>
alias:          devname:fuse
alias:          char-major-10-229
alias:          fs-fuse
depends:
retpoline:      Y
intree:         Y
name:           fuse
vermagic:       5.15.0-113-generic SMP mod_unload modversions
sig_id:         PKCS#7
signer:         Ubuntu Kernel Module Signing Authority
sig_key:        ...
sig_hashalgo:   sha512
signature:      ...
parm:           max_user_bgreq:Maximum number of pending background requests (uint)
parm:           max_user_cong_req:Maximum number of pending congested background requests (uint)
parm:           max_user_inline_write:Maximum size of inline write data (uint)

Let's look at some of the key fields:

  • filename: The path to the module file on the filesystem.
  • license: The license under which the module is distributed (e.g., GPL).
  • description: A brief description of what the module does.
  • author: The author(s) of the module.
  • depends: A list of other modules that this module depends on.
  • parm: Information about parameters that can be passed to the module when it is loaded to modify its behavior.

modinfo is an invaluable tool for troubleshooting module-related issues or simply learning more about the components of your Linux kernel.

Now, try running the modinfo fuse command yourself to see the details of the fuse module.

Click Continue to finish this lab.

Summary

In this lab, you learned how to check the status of kernel modules in Linux. You started by using the lsmod command to list all currently loaded kernel modules, understanding the output columns showing the module name, size, and usage count.

You then explored the modprobe command with the --dry-run option to simulate loading a module and identify its dependencies without actually loading it. Finally, you learned how to use the modinfo command to view detailed information about a specific kernel module, including its description, author, license, and parameters. These commands are fundamental tools for managing and troubleshooting kernel modules on a Linux system.