Kernel module

Also known as: loadable kernel module

On this page 7

A kernel module is a piece of object code that can be loaded into and unloaded from the running Linux kernel on demand, extending its functionality without recompiling the kernel or rebooting the system. Also called a loadable kernel module (LKM), it typically provides a device driver, a file system, a network protocol or a security feature, and it executes in kernel space with full privileges.

Modules are one of the core ideas in Linux fundamentals because they explain how a single generic kernel shipped by a distribution can support thousands of different hardware combinations. The kernel image stays relatively small, and only the code a machine actually needs is loaded, either automatically at boot or manually by an administrator.

How a kernel module is loaded and linked

A kernel module is loaded by linking a relocatable ELF object with the .ko extension into the address space of the running kernel. The init_module or finit_module system call passes the file to the kernel, which resolves the module’s unresolved symbols against the exported kernel symbol table, allocates memory, applies relocations and then calls the module’s initialization function.

Every module declares an entry point with module_init() and an exit point with module_exit(). The init function registers the services the module offers, for example a driver for a PCI device ID, and the exit function unregisters everything and frees resources before removal. If initialization returns an error, the kernel discards the module immediately.

Modules can only call kernel functions that are explicitly exported with EXPORT_SYMBOL or EXPORT_SYMBOL_GPL. The second form restricts a symbol to modules that declare a GPL-compatible license, which is how the kernel separates its stable internal interfaces from code that proprietary modules may use.

The following table summarizes the main stages of a module’s lifetime and the components involved in each one.

StageWhat happensMain component
BuildSource compiled against kernel headers into a .ko fileKbuild and kernel headers
DiscoveryDevice or alias triggers a module requestudev and modalias strings
Dependency resolutionRequired modules are loaded firstmodprobe and modules.dep
Load and linkSymbols resolved, relocations appliedfinit_module system call
InitializationDriver or feature registeredmodule_init function
RemovalResources released, code unmappedmodule_exit and delete_module
Lifecycle of a loadable kernel module

Tools for managing kernel modules

Administrators manage modules with the kmod utility suite, which replaced the older module-init-tools package. The most important command is modprobe, because it reads the dependency map in modules.dep and loads or removes a module together with everything it requires, whereas insmod and rmmod act on a single file without any dependency handling.

The table below compares the standard commands and the situations in which each is appropriate.

CommandPurposeTypical use
lsmodLists loaded modules and use countsChecking which drivers are active
modinfoShows license, parameters and aliasesInspecting a module before loading
modprobeLoads a module with its dependenciesNormal loading by administrators
modprobe -rRemoves a module and unused dependenciesUnloading a driver safely
insmodLoads one file by path, no dependenciesTesting a freshly built module
rmmodRemoves one module by nameDevelopment and debugging
depmodRebuilds modules.dep and alias mapsAfter installing new modules
Common module management commands

Persistent behavior is configured through files rather than commands. Entries in /etc/modprobe.d/ set module parameters, define aliases or block a module with a blacklist line, and files in /etc/modules-load.d/ list modules that systemd loads at every boot. Loaded modules and their parameters appear under /sys/module/, which is the authoritative runtime view of the module state.

Kernel modules compared with built-in code

A feature in the Linux kernel can be compiled either as a module or directly into the kernel image, and the choice is made per option in the kernel configuration, where m means module and y means built in. Functionally the code is the same; the difference lies in when it becomes available and how flexibly it can be managed.

The comparison below shows why distributions build most drivers as modules while keeping a small core built in.

PropertyLoadable moduleBuilt-in code
AvailabilityAfter the module is loadedFrom the first moment of boot
Memory useOnly when neededAlways resident
UpdatingReplace and reload without reboot in many casesRequires a new kernel and reboot
ParametersSet at load time or via sysfsSet on the kernel command line
Root file system supportNeeds an initramfsWorks without an initramfs
Attack surfaceCan grow at runtimeFixed at build time
Modular versus built-in kernel features

Drivers for the storage controller and root file system are the classic dilemma. If they are modules, the bootloader must also load an initramfs that contains them, because the kernel cannot read modules from a disk it cannot yet access.

Versioning, signing and security

A kernel module is tied to the exact kernel version it was built for. Each module carries a vermagic string, and with CONFIG_MODVERSIONS enabled it also stores CRC checksums of every symbol it uses, so the loader refuses a module whose interface assumptions no longer match. Linux deliberately offers no stable in-kernel ABI, which is why out-of-tree modules must be rebuilt for every kernel update.

Frameworks such as DKMS (Dynamic Kernel Module Support) automate that rebuild by compiling registered module sources whenever a new kernel is installed. This is the usual delivery method for third-party graphics drivers, virtualization guest additions and some file system drivers.

Because module code runs with unrestricted kernel privileges, loading one is equivalent to granting full control of the machine. Module signature verification with CONFIG_MODULE_SIG lets the kernel reject unsigned modules, and on systems with UEFI Secure Boot many distributions enforce it through kernel lockdown. Setting kernel.modules_disabled to 1 prevents any further loading until the next reboot, which is a useful hardening step on servers with a fixed hardware profile.

Loading a module with a non-GPL license or without a valid signature sets a taint flag visible in /proc/sys/kernel/tainted. Kernel developers usually decline to debug crashes on a tainted kernel, because the proprietary code cannot be inspected.

Common problems and how to resolve them

Most module failures trace back to version mismatches, missing dependencies or policy restrictions, and the kernel log shown by dmesg or journalctl -k usually names the cause precisely. The table lists frequent symptoms with their typical causes and remedies.

SymptomLikely causeRemedy
Invalid module formatBuilt for a different kernel versionRebuild against current headers
Unknown symbol in moduleDependency not loaded or symbol not exportedUse modprobe, check license
Key was rejected by serviceUnsigned module under Secure BootSign the module or enroll a key
Module is in useNonzero reference countStop the dependent service first
Device works after manual load onlyMissing alias or blacklistedReview modprobe.d configuration
Boot fails after updateStorage driver absent from initramfsRegenerate the initramfs
Troubleshooting kernel module errors

A frequent misconception is that removing a module is always safe. A bug in module code can crash the entire system with a kernel panic or corrupt memory silently, since there is no isolation between modules and the rest of the kernel. For that reason, production systems should load only vendor-supported or well-audited modules.

Put simply

Think of the Linux kernel as the engine of the operating system. A kernel module is an add-on part that can be attached to the engine while it is running. When a new printer, network card or disk format appears, the system attaches the matching part, and when it is no longer needed, the part can be removed.

This keeps the core small and adaptable. The trade-off is trust: an add-on part works inside the engine itself, so a faulty or malicious module can break the whole system, which is why modern systems often accept only signed modules.

In practice

A systems administrator runs a fleet of virtualization hosts on a long-term-support Linux distribution and needs a third-party network driver for new 25 GbE adapters that the stock kernel does not support well. The vendor ships the driver as source code registered with DKMS, so each kernel update triggers an automatic rebuild of the .ko file.

Because the hosts use UEFI Secure Boot, the administrator generates a signing key, enrolls it with the machine owner key utility and configures DKMS to sign every rebuilt module. A file in /etc/modprobe.d/ blacklists the older in-tree driver, and the initramfs is regenerated so the correct module loads at boot.

After a staged rollout across 40 hosts, every server boots with the signed driver loaded automatically, lsmod confirms the expected module, and later kernel updates proceed without manual rebuilding or network outages.