insmod refuses to load a kernel module: Invalid module format
insmod: ERROR: could not insert module ./hello.ko: Invalid module formatShort 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.
Fix it fast
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.ko2Match what you see, then jump to its fix
- 1
The version in vermagic is not the string uname -r printsThe module was built for another kernel than the one runningFix - 2
The 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
vermagic 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
- 1
What does the error look like?
insmod: ERROR: could not insert module ./hello.ko: Invalid module formatinsmod: 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 -r2Fix
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.ko3Verify
The module shows in the loaded list.
lsmod | grep helloConfirmed by man7.org
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)/build2Fix
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) modules3Verify
Clean, rebuild and compare vermagic again.
make -C /lib/modules/$(uname -r)/build M=$PWD clean
make
modinfo -F vermagic ./hello.koConfirmed by docs.kernel.org
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 -m2Fix
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 modules3Verify
insmod now succeeds without an error.
sudo insmod ./hello.ko && echo loadedConfirmed by docs.kernel.org
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 fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
Apache warns that a ${VAR} config variable is not defined
${VAR} left unchanged in the configuration, with a warning in the error log
Apache expands ${VAR} from Define or from the shell environment that exists when the server starts.
3 causes, each with a fix