Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

insmod refuses to load a kernel module: Invalid module format

Errorinsmod: ERROR: could not insert module ./hello.ko: Invalid module format

Short answer

The kernel rejects a module that was not built for the exact kernel that is running. Compare the module's vermagic with uname -r, then rebuild against /lib/modules/$(uname -r)/build on the same machine and architecture. A stale build after a kernel update is the usual cause.

  • Applies to Linux kernel 6.x, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the kernel the module was built for and the kernel that is running. If they differ, cause 1 or 2 applies.

    modinfo -F vermagic ./hello.ko
    uname -r
    file ./hello.ko
  2. 2Match what you see, then jump to its fix

    1. 1The version in vermagic is not the string uname -r printsThe module was built for another kernel than the one runningFix
    2. 2The module builds fine, but the vermagic belongs to a source tree or an older kernel you did not intendMakefile points at the wrong kernel treeFix
    3. 3vermagic matches uname -r, but file shows another CPU type (for example ARM on an x86 machine), or symbol checks failSame version string, but built for a different architecture or a different configurationFix

What does the error look like?

  • insmod: ERROR: could not insert module ./hello.ko: Invalid module format
  • insmod: error inserting './hello.ko': -1 Invalid module format

Cause 1 of 3

The module was built for another kernel than the one running

1Check

A module is tied to the kernel it was compiled against. The loader checks this when you insert it and refuses a mismatch with the 'exec format' class error (ENOEXEC). A kernel update or reboot into another kernel after the build is enough to trigger it.

modinfo -F vermagic ./hello.ko
uname -r

2Fix

Rebuild the module against the running kernel's build tree and insert the fresh file.

make -C /lib/modules/$(uname -r)/build M=$PWD modules
sudo insmod ./hello.ko

3Verify

The module shows in the loaded list.

lsmod | grep hello

Confirmed by man7.org

Did this fix it?

Cause 2 of 3

Makefile points at the wrong kernel tree

1Check

The kernel build system expects a prepared kernel tree, and for the running kernel that is /lib/modules/<version>/build. A hard-coded path from a tutorial, or another machine, builds against whatever is there.

grep -n -E 'KDIR|-C ' Makefile
ls -d /lib/modules/$(uname -r)/build

2Fix

Default the tree to the running kernel, and let the caller override it. If the build directory does not exist, install the headers package your distribution provides for that exact kernel first.

KDIR ?= /lib/modules/$(shell uname -r)/build
all:
	$(MAKE) -C $(KDIR) M=$(PWD) modules

3Verify

Clean, rebuild and compare vermagic again.

make -C /lib/modules/$(uname -r)/build M=$PWD clean
make
modinfo -F vermagic ./hello.ko

Confirmed by docs.kernel.org

Did this fix it?

Cause 3 of 3

Same version string, but built for a different architecture or a different configuration

1Check

The syscall also fails for an image that is invalid or built for another architecture. With module versioning enabled, the kernel stores a checksum of each exported symbol's prototype and refuses the module when they differ, which happens when the headers come from another build of that version.

file ./hello.ko
uname -m

2Fix

Build on the target machine, or with a toolchain and kernel tree for the target, using the kernel tree that matches the running kernel exactly.

make -C /lib/modules/$(uname -r)/build M=$PWD modules

3Verify

insmod now succeeds without an error.

sudo insmod ./hello.ko && echo loaded

Confirmed by docs.kernel.org

Did this fix it?

How do I stop it coming back?

  • Rebuild every out-of-tree module after each kernel update.
  • Keep KDIR defaulted to /lib/modules/$(uname -r)/build in the Makefile.
  • Check vermagic against uname -r before loading a module you did not build on this machine.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Servers

dnf cannot install MySQL on Amazon Linux 2023

dnf cannot find a mysql-server package on Amazon Linux 2023

Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.