Manage Kernel Modules in Linux

CompTIABeginner
Practice Now

Introduction

In this lab, you will learn the fundamental techniques for managing kernel modules in a Linux environment. You will explore how to list currently loaded modules to view the system's active components and inspect individual modules to understand their details, such as their purpose, dependencies, and other parameters.

Building on these inspection skills, you will practice dynamically unloading and loading modules from the running kernel. Finally, you will learn how to configure the system to load a specific kernel module automatically at boot, ensuring essential functionality is available without manual intervention after a restart.

List and Inspect Kernel Modules with lsmod and modinfo

In this step, you will learn how to list the currently loaded kernel modules and inspect their details. The Linux kernel is modular, meaning parts of its functionality can be loaded or unloaded on demand. These dynamically loadable parts are called kernel modules, and they often correspond to device drivers, filesystem drivers, or system calls.

First, let's view the status of all kernel modules currently loaded into your system's kernel. The lsmod command provides a clean and readable list. Since the list can be quite long, we will pipe its output to the less utility for easy navigation.

Execute the following command in your terminal:

lsmod | less

You will see a list of modules. The output is organized into three columns: Module, Size, and Used by.

  • Module: The name of the kernel module.
  • Size: The size of the module in bytes.
  • Used by: The number of other modules or processes currently using this module, followed by a list of the dependent modules.

Your output will look similar to this:

Module                  Size  Used by
nls_iso8859_1          16384  1
nls_cp437              20480  1
vfat                   20480  1
fat                    69632  1 vfat
...

You can scroll through the list using the arrow keys. Press q to exit the less viewer and return to the command prompt.

Now that you know how to list modules, let's get detailed information about a specific one. The modinfo command displays various details about a kernel module, such as its filename, license, description, author, and dependencies.

Let's inspect the parport module, which is related to the parallel port.

modinfo parport

The command will display detailed information about the parport module.

filename:       /lib/modules/x.x.x-xx-generic/kernel/drivers/parport/parport.ko
license:        GPL
description:    Parallel port driver
author:         Philip Blundell, Tim Waugh, Grant Grundler
srcversion:     <some_version_string>
depends:
retpoline:      Y
intree:         Y
vermagic:       x.x.x-xx-generic SMP mod_unload
sig_id:         ...
signer:         ...
sig_key:        ...
sig_hashalgo:   ...
signature:      ...

This output tells you the exact location of the module file (.ko file), its license, a brief description, and more. Notice the depends field is empty, indicating this module has no dependencies.

Modules often rely on other modules. The depmod command creates a list of module dependencies by analyzing the modules in /lib/modules/$(uname -r). This allows the system to automatically load required modules. Let's generate this dependency file. This command requires root privileges, so we'll use sudo.

sudo depmod

This command typically produces no output upon successful completion. It creates or updates a file named modules.dep in the kernel's module directory. The $(uname -r) part of the path is a command substitution that inserts your current kernel's release version.

Now, let's view the dependency file you just built.

less /lib/modules/$(uname -r)/modules.dep

This file contains a list of modules and their dependencies, which is used by tools that manage modules automatically. You can press q to exit less.

Unload a Kernel Module with rmmod

In this step, you will learn how to unload a kernel module from the running kernel using the rmmod command. Unloading modules can be useful for troubleshooting hardware issues, freeing up system memory, or when a module is no longer needed. It's important to note that you can only unload modules that are not currently in use by the system or other modules.

Let's continue with the joydev module, which provides joystick support. First, let's check if this module is loaded. We can use lsmod combined with grep to filter the output.

lsmod | grep joydev

If the module is loaded, you will see an output similar to this. The exact size may vary.

joydev                 24576  0

The 0 in the Used by column indicates that the module is loaded but not currently in use by any process or other module. This makes it a safe candidate for removal. If this number were greater than zero, rmmod would likely fail, preventing you from unloading a module that the system depends on.

Now, let's remove the joydev module. Since this action modifies the running kernel, it requires root privileges, which we will obtain using sudo.

sudo rmmod joydev

The rmmod command usually doesn't produce any output if it succeeds. To confirm that the module has been successfully unloaded, we can run the same lsmod | grep joydev command again.

lsmod | grep joydev

This time, the command should produce no output. This silence is your confirmation that the joydev module is no longer loaded in the kernel.

Load a Kernel Module with insmod

In this step, you will learn how to manually load a kernel module into the running kernel using the insmod command. This is the counterpart to rmmod. While modern systems often use a more advanced tool called modprobe which automatically handles dependencies, understanding insmod is fundamental to learning how kernel modules work.

In the previous step, you unloaded the joydev module. Now, we will load it back. The insmod command requires you to provide the full path to the module's object file, which has a .ko (Kernel Object) extension.

The standard directory for kernel modules is /lib/modules/$(uname -r)/kernel/, where $(uname -r) expands to your current kernel version. The joydev module is typically located in the drivers/input/ subdirectory.

Let's load the joydev module using its full path. This operation requires root privileges.

sudo insmod /lib/modules/$(uname -r)/kernel/drivers/input/joydev.ko

Like rmmod, insmod will not produce any output if it is successful.

To confirm that the module has been loaded, you can use the lsmod | grep joydev command again.

lsmod | grep joydev

After running the command, you should see the joydev module listed in the output, confirming it has been successfully loaded back into the kernel.

joydev                 24576  0

Configure a Kernel Module to Load Automatically at Boot

In this step, you'll learn how to make sure a kernel module is loaded automatically every time your system starts. Manually loading modules with insmod is temporary and does not persist after a reboot. To make the change permanent, you need to configure your system to load the module during the boot process.

On modern Linux systems that use systemd, the standard way to automatically load kernel modules at boot is by adding a configuration file to the /etc/modules-load.d/ directory. Any file in this directory with a .conf extension will be read, and the system will attempt to load every module listed inside it, one per line.

Let's configure the joydev module to load automatically. We will create a file named joydev.conf inside /etc/modules-load.d/ and add the word joydev to it. We can do this in a single command using echo and tee with sudo.

echo joydev | sudo tee /etc/modules-load.d/joydev.conf

This command works as follows:

  • echo joydev prints the text 'joydev'.
  • The pipe | sends this text as input to the next command.
  • sudo tee /etc/modules-load.d/joydev.conf receives the text. The tee command writes the input it receives to the specified file (/etc/modules-load.d/joydev.conf) and also displays it on the screen. We use sudo because this directory is protected and requires root privileges to write to.

You should see the following output, which is the content being written to the file:

joydev

To confirm that the file was created correctly, you can display its contents using the cat command:

cat /etc/modules-load.d/joydev.conf

The output should be the name of the module you just added:

joydev

Now, every time the system boots, it will automatically load the joydev module. You have successfully configured a persistent module load.

Summary

In this lab, you learned the fundamental operations for managing kernel modules in a Linux environment. You practiced listing all currently loaded modules using the lsmod command and inspecting detailed information about a specific module, such as its filename and description, with the modinfo command.

You also gained hands-on experience with dynamically altering the kernel by unloading a module with rmmod and loading one with insmod. Finally, you learned how to configure a module to load automatically at boot, ensuring that necessary drivers or functionalities are available after a system restart. These skills are essential for managing device drivers and kernel features effectively.