Right, let’s talk about the module circus. You’ve booted your Linux kernel, but it’s not a monolith. It’s more like a core framework with a bunch of hot-swappable components, called modules, that you can plug in and yank out on the fly. This is how your system can support everything from a 20-year-old dial-up modem to a brand-new GPU without you having to recompile the entire kernel from scratch every Tuesday. The ringmaster for this circus? A suite of deceptively simple commands.

Your Running Cast of Characters: lsmod

First, you need to know what’s currently loaded. That’s lsmod’s job. It’s the simplest command you’ll run all day. It takes no arguments and just spits out a list.

$ lsmod
Module                  Size  Used by
nvidia              35323904  203
uas                  00000000  0
usb_storage          00000000  7 uas
btusb                00000000  0
btrtl                00000000  1 btusb
btbcm                00000000  1 btusb
btintel              00000000  1 btusb
bluetooth            00000000  5 btrtl,btintel,btbcm,btusb
... # and so on

Don’t be fooled by its simplicity; the Used by column is the real gossip. It tells you the dependency relationships between modules. Here, usb_storage is being used by 7 other things (probably a counter for active connections), and the bt* modules are all cozying up to the main bluetooth module. If you try to remove a module that has a non-zero “Used by” count, the kernel will rightly tell you to get lost. It’s like trying to remove a foundation block from the bottom of a Jenga tower.

The Intelligent Loader: modprobe

Now, insmod exists. It’s a dumb, low-level tool that tries to shove a single module file (.ko) into the kernel. I say “dumb” because if that module depends on another module not being loaded, insmod will fail with a spectacularly unhelpful “unknown symbol” error. It’s like trying to plug a power strip into itself.

You are not dumb, so you will use modprobe. This is the intelligent front-end. You give it the module name, and it does the real work:

  1. It consults the magical modules.dep file (generated by depmod) to understand all dependencies.
  2. It recursively loads every single dependency that isn’t already there.
  3. Then it loads the module you asked for.

Loading the nvidia module? modprobe handles the dozen other bits and pieces it needs.

# This is the right way. It just works.
sudo modprobe nvidia

# This is the 'I enjoy pain' way. Don't do this.
sudo insmod /lib/modules/$(uname -r)/kernel/drivers/video/nvidia.ko

The best part? modprobe also reads configuration files from /etc/modprobe.d/. This is where you go to blacklist a module that’s causing trouble or pass custom options to a driver every time it loads. It’s the difference between a one-off hack and a permanent, clean solution.

The Module Encyclopedia: modinfo

Before you shove a random piece of code into the very heart of your operating system, don’t you think you should know something about it? modinfo is your due diligence.

$ modinfo nvidia
filename:       /lib/modules/6.5.0-21-generic/updates/dkms/nvidia.ko
alias:          char-major-195-*
version:        535.161.07
license:        NVIDIA
author:         NVIDIA Corporation
description:    NVIDIA Kernel Driver
srcversion:     5C0235C6A8C0C7A8A6C6D6C
depends:        drm,ipmi_msghandler
retpoline:      Y
name:           nvidia
vermagic:       6.5.0-21-generic SMP preempt mod_unload modversions
parm:           NvSwitchUnsupported:NvSwitch devices are not supported (bool)
parm:           NVreg_EnableMSI:Enable MSI functionality (0=disable, 1=enable; default=1) (int)
... # lots more parms

This output is a goldmine. You see the exact path to the file, the version (invaluable for debugging), the license (crucial for vendors), and most importantly, the parameters (parm). These parameters are the knobs you can tweak to change the module’s behavior on load. Notice how modinfo also confirms the dependencies we suspected from lsmod. It’s all connected.

The ‘Are You Sure About This?’ Tool: rmmod

This is how you remove a module. The command is simple: sudo rmmod module_name. The consequences can be anything from “fine” to “immediate kernel panic.”

rmmod will refuse to unload a module that is still in use (as shown by lsmod), which is good. But even if it’s not “used,” it might be critical. Unloading a filesystem module for a currently mounted drive? A module for your root controller? Don’t. Just don’t. The kernel tries to stop you from doing truly catastrophic things, but it operates on a principle of “you asked for it, you got it.” It assumes you, the user with root privileges, have a death wish and respects your agency.

The safe, intelligent way to remove a module and all its now-unused dependencies is actually with modprobe -r.

# The safe way. Removes the target and any dependencies that are no longer needed.
sudo modprobe -r nvidia

# The more direct, slightly more dangerous way.
sudo rmmod nvidia

The cardinal rule? You can’t remove a module that’s built into the kernel (you’ll see these in /lib/modules/$(uname -r)/modules.builtin). And you really, really shouldn’t remove modules that are essential to the system you’re currently using. If your terminal is on a tty provided by vesafb, removing vesafb will make your screen look like modern art in a bad way. The best practice is to only remove modules for hardware you’ve physically unplugged or are absolutely certain you’re done with. This isn’t a toybox; it’s the core of your machine. Treat it with the respect it deserves, and it won’t punish you for it.