lsmod Command in Linux

Introduction to Linux Kernel Modules and the lsmod Command

This lab provides a practical introduction to the lsmod command in Linux, a crucial tool for any systemadmin. You'll discover the command's purpose: displaying details about currently loaded kernel modules. We'll explore lsmod output, learning to interpret information such as module name, size, and dependencies. You will also learn how to dynamically load and unload kernel modules. This tutorial aims to empower you with the knowledge to effectively manage and troubleshoot kernel modules on your Linux system.

Understanding the Functionality of the lsmod Command

This section focuses on the core purpose of the lsmod command within the Linux environment. The lsmod command serves as a vital utility for displaying information about the kernel modules presently loaded into the system's memory.

Kernel modules are self-contained pieces of code that extend the functionality of the kernel without requiring a reboot. They can be dynamically loaded and unloaded. The lsmod command provides a real-time view of which modules are active and their current status.

To execute the lsmod command, open your terminal and enter:

$ sudo lsmod

Example output:

Module                  Size  Used by
vboxguest              453376  0
vboxsf                  86016  1

The output lists the module name, the module size (in bytes), and the number of modules that depend on the specified module.

In this example, vboxguest and vboxsf are loaded kernel modules. These are typically associated with VirtualBox guest additions, enhancing the capabilities of a VirtualBox virtual machine.

A solid understanding of the lsmod command is essential for effective management and troubleshooting of kernel modules in your Linux system administration tasks.

Analyzing lsmod Command Output for System Insights

This section guides you through interpreting the output of the lsmod command, extracting valuable information about loaded kernel modules.

Let's re-run the lsmod command:

$ sudo lsmod

Example output:

Module                  Size  Used by
vboxguest              453376  0
vboxsf                  86016  1

The lsmod output is structured into three key columns:

  1. Module: This column displays the name of the specific kernel module.
  2. Size: This indicates the memory footprint of the module in bytes.
  3. Used by: This shows the number of other modules currently utilizing the features provided by this module.

From the example, we see vboxguest and vboxsf are loaded. vboxguest consumes 453,376 bytes and is not used by any other module. vboxsf uses 86,016 bytes and is used by one other module.

The "Used by" column reveals module dependencies. A non-zero value signifies that other modules rely on the functionality of that particular module.

Proficiently interpreting lsmod output can assist in diagnosing kernel module-related issues and optimizing your Linux system's performance as a systemadmin.

Managing Kernel Modules: Loading and Unloading

This section explores the manual loading and unloading of kernel modules on your Linux system. This skill is critical for effective systemadmin work.

Loading a Kernel Module

The modprobe command is your primary tool for loading modules. Let's load the vboxguest module (from previous examples):

$ sudo modprobe vboxguest

Confirm the module has been loaded by running lsmod and filtering for vboxguest:

$ sudo lsmod | grep vboxguest
vboxguest              453376  0

Unloading a Kernel Module

Use modprobe -r to unload a module. Let's remove vboxguest:

$ sudo modprobe -r vboxguest

Verify that the vboxguest module is no longer listed in the lsmod output.

$ sudo lsmod | grep vboxguest
## (no output)

Mastering modprobe and modprobe -r allows you to dynamically manage kernel modules, adapting your system's capabilities based on current needs. Remember that unloading modules incorrectly can lead to system instability, especially for modules with dependencies. Be careful when unloading modules with "Used by" count greater than zero.

Summary: Mastering lsmod for Linux System Administration

This lab introduced the lsmod command, which is crucial for displaying information about the kernel modules currently loaded on the system. You learned to analyze lsmod output, identifying module name, size, and usage. Finally, you practiced loading and unloading kernel modules using the modprobe and modprobe -r commands. This knowledge is vital for efficient Linux system administration, allowing you to understand and control your kernel environment.

400+ Linux Commands