Understanding Linux Kernel Modules

Linux kernel modules are loadable code pieces that extend kernel functionality without rebooting. Key management tools include lsmod (view loaded modules), modprobe (load/unload with dependencies), insmod (basic loading), and depmod (manage dependencies).

Understanding Linux Kernel Modules

Linux kernel modules are pieces of code that can be loaded into the kernel on demand, extending its functionality without requiring a system reboot. Think of them as plugins for your operating system's core. They allow you to add device drivers, file system support, and other kernel-level features dynamically.

Unlike monolithic kernels where everything is compiled into one large binary, Linux uses a modular approach. This design keeps the base kernel lean while providing flexibility to load only the components you need. When you plug in a USB device or connect to a Wi-Fi network, kernel modules handle the low-level communication.

How Kernel Modules Work

Kernel modules are compiled code files with a .ko (kernel object) extension. They contain initialization and cleanup functions that register and unregister functionality with the kernel. When loaded, a module becomes part of the kernel's address space and runs with full system privileges.

The beauty of this system lies in its flexibility. You can load modules when hardware is detected, unload them to free memory, or even replace them with updated versions without rebooting the entire system.

Essential Module Management Tools

Viewing Loaded Modules with lsmod

The lsmod command shows all currently loaded kernel modules:

$ lsmod
Module                  Size  Used by
snd_hda_codec_realtek    114688  1
snd_hda_codec_generic     77824  1 snd_hda_codec_realtek
snd_hda_intel             40960  5
snd_intel_dspcfg          24576  2 snd_hda_intel,snd_sof_pci

The output shows the module name, memory usage, and dependency relationships. The "Used by" column indicates which other modules depend on this one.

Loading Modules with insmod and modprobe

You can manually load modules using insmod for basic loading:

$ sudo insmod /path/to/module.ko

However, modprobe is the preferred tool because it automatically handles dependencies:

$ sudo modprobe bluetooth

The modprobe command consults the module dependency database and loads any required supporting modules first. It can also remove modules with the -r option:

$ sudo modprobe -r bluetooth

Managing Dependencies with depmod

The depmod command analyzes module dependencies and creates the database that modprobe uses. It's typically run automatically during kernel updates, but you can run it manually:

$ sudo depmod -a

This creates the /lib/modules/$(uname -r)/modules.dep file containing dependency information.

Practical Module Management

To check if a specific module is loaded:

$ lsmod | grep module_name

To get detailed information about a module:

$ modinfo module_name

This shows the module's description, author, license, parameters, and file location.

For automatic module loading at boot, you can add module names to /etc/modules-load.d/ configuration files. To prevent modules from loading, use blacklisting in /etc/modprobe.d/.

What's Next

Now that you understand kernel modules and their management tools, the next step is exploring device files and the /dev directory. We'll examine how Linux represents hardware devices as files and how the kernel communicates with your system's hardware components.


Linux+ study resources