insmod: Load Kernel Modules


insmod: Load Kernel Modules

insmod is the classic tool to insert a kernel module (.ko) into the running Linux kernel. It’s straightforward but powerful—and with power comes responsibility: you must ensure the module is compatible with your kernel and built for your current configuration.

Quick start

  • Basic usage:
sudo insmod /path/to/module.ko
  • If the module has dependencies not yet loaded, insmod will fail. You’ll need to load the dependent modules first, or use a tool that handles dependencies for you.

  • After loading, you can verify the module is loaded with:

lspci -k | head -n 20  # or: lsmod
Note: lsmod shows loaded modules; lsmod is the easiest read.
  • Common path tips:
    • Use the full path to the .ko file to avoid confusion.
    • Ensure the module was built for your current kernel.

When to use insmod

  • You need to insert a specific module manually, for debugging or development.
  • You know the module has no external dependencies, or you intentionally manage them yourself.
  • You’re working with a custom or out-of-tree module not included in the distribution’s package.

Why you might prefer modprobe

  • modprobe scans /lib/modules/$(uname -r)/modules.dep and automatically handles dependencies.
  • insmod does not resolve dependencies; if a required module isn’t loaded yet, the insertion will fail.

If you’re practicing, try with a harmless dummy module (if your distro provides one) to see how dependencies influence the process.

Common pitfalls

  • Predictor: Unsigned or incompatible modules

    • Kernel signing and module signing enforcement can reject unsigned modules.
    • Ensure your module was built against the same kernel version and config.
  • Dependencies not loaded

    • If a module depends on another, insmod fails with unresolved symbols.
    • Use modprobe for automatic dependency handling, or load all dependencies first.
  • Permissions and security

    • You typically need root privileges to insert modules.
    • Loaded modules affect kernel behavior and stability; only insert trusted modules.
  • Path and filename mistakes

    • A typo in the path or using a .ko built for a different kernel can cause hard failures.

Verification and debugging

  • Check loaded modules:
lsmod | grep <module_name>
  • View kernel messages for insertion results or errors:
dmesg | tail -n 50
  • If you need to remove a module:
sudo rmmod <module_name>
Note: rmmod is for removal; if the module is in use, you'll need to unload dependents or use modprobe -r.

Quick example: inserting a dummy test module

# Build your module (example steps depend on your setup)
# sudo insmod /path/to/example.ko
# Verify
lsmod | grep example

Summary

  • insmod inserts a single, explicitly specified module. It does not resolve dependencies. For everyday use, prefer modprobe, which handles dependencies for you. Always verify kernel compatibility and module origin, and clean up with rmmod when you’re done.

See Also