Ship Out-of-Tree Kernel Modules With DKMS
Package an out-of-tree kernel module with DKMS: write dkms.conf, add, build and install, rebuild on kernel upgrades automatically and sign for Secure Boot.

The problem DKMS solves
An out-of-tree module is built against one kernel, and the next kernel update makes it useless: the vermagic no longer matches, so the module stops loading and whatever depends on it, a graphics driver or a network card, stops working after the reboot. Rebuilding by hand after every update is error prone. DKMS, the Dynamic Kernel Module Support framework, automates it. You give it the source and a small description file, and it rebuilds and installs the module for each new kernel as it is installed.
This post packages the earlier hello module for DKMS, walks through the commands that add, build and install it, shows how the automatic rebuild is triggered, and covers signing for Secure Boot. It uses dkms 3.x on Debian 13 with Linux 6.12 LTS. The tool is documented in its dkms(8) manual page.
Install and prepare
sudo apt install dkms build-essential linux-headers-"$(uname -r)"DKMS needs the kernel headers for every kernel it builds for, so on Debian-based systems the linux-headers-<release> package must be installed with each kernel. The metapackage linux-headers-amd64 keeps them current.
The source layout
DKMS looks for a source directory named /usr/src/<name>-<version>/. Create it with the module source, its Makefile and a dkms.conf:
sudo mkdir -p /usr/src/hello-1.0
sudo cp hello.c Makefile /usr/src/hello-1.0/The Makefile is the one from the out-of-tree build post, but it must work when DKMS calls it with the kernel version set. DKMS passes the target kernel in the environment variable kernelver, so the build command in dkms.conf uses it:
PACKAGE_NAME="hello"
PACKAGE_VERSION="1.0"
BUILT_MODULE_NAME[0]="hello"
DEST_MODULE_LOCATION[0]="/updates/dkms"
AUTOINSTALL="yes"
MAKE[0]="make -C ${kernel_source_dir} M=${dkms_tree}/${PACKAGE_NAME}/${PACKAGE_VERSION}/build modules"
CLEAN="make -C ${kernel_source_dir} M=${dkms_tree}/${PACKAGE_NAME}/${PACKAGE_VERSION}/build clean"Copy that to /usr/src/hello-1.0/dkms.conf. Each setting has a purpose:
| Setting | Meaning |
|---|---|
PACKAGE_NAME, PACKAGE_VERSION | Must match the directory name hello-1.0 |
BUILT_MODULE_NAME[0] | The module file produced, without .ko |
DEST_MODULE_LOCATION[0] | Where to install under /lib/modules/<release>/, updates/dkms takes priority over stock modules |
AUTOINSTALL="yes" | Rebuild automatically when a new kernel is installed |
MAKE[0] | The build command; ${kernel_source_dir} is the headers of the target kernel |
CLEAN | How to clean the build |
Add, build, install
Three commands take the module from source to installed:
sudo dkms add -m hello -v 1.0
sudo dkms build -m hello -v 1.0
sudo dkms install -m hello -v 1.0Creating symlink /var/lib/dkms/hello/1.0/source -> /usr/src/hello-1.0
Building module(s)... done.
Installing /lib/modules/6.12.9-amd64/updates/dkms/hello.ko
Running depmod... done.The exact messages vary with the DKMS version. add registers the source tree, build compiles it for the running kernel inside /var/lib/dkms/hello/1.0/, and install copies the result into /lib/modules/<release>/updates/dkms/ and runs depmod. Check the state:
dkms statushello/1.0, 6.12.9-amd64, x86_64: installedNow sudo modprobe hello loads the module by name, since it is in the module database.
Automatic rebuild on a new kernel
The reason to use DKMS is what happens next. When a new kernel package installs, a hook runs. On Debian it is a script in /etc/kernel/postinst.d/dkms, and on Fedora and Arch it is a systemd unit or a package hook. The hook loops over every module registered with AUTOINSTALL="yes" and builds it for the new kernel. After a kernel update the status shows both:
hello/1.0, 6.12.9-amd64, x86_64: installed
hello/1.0, 6.12.12-amd64, x86_64: installedOn the reboot into 6.12.12, modprobe hello works without any manual rebuild. The condition is that the new kernel's headers are installed when the hook runs, and a failed build is reported in the package manager's output and in /var/lib/dkms/hello/1.0/<release>/x86_64/log/make.log. Read that file first when a module is missing after an update.
Sign for Secure Boot
With Secure Boot enabled, the kernel only loads modules signed by a key it trusts, and a DKMS module built locally is unsigned. DKMS can sign automatically if a signing key exists. On Debian-based systems, it creates a Machine Owner Key at /var/lib/dkms/mok.key and /var/lib/dkms/mok.pub, and the key must be enrolled in the firmware once:
sudo mokutil --import /var/lib/dkms/mok.pubinput password:
input password again:The command asks for a one-time password, then you reboot, and the MOK manager (a blue screen at boot) lets you confirm the enrollment with that password. Afterwards DKMS signs each module it builds with the key, and the kernel accepts it. Key locations and the way the framework finds them differ between distributions and DKMS versions, so check dkms(8) and your distribution's documentation instead of assuming these paths.
Remove it
sudo dkms remove -m hello -v 1.0 --all
sudo rm -rf /usr/src/hello-1.0--all removes the module from every kernel it was installed for. Without it, only the running kernel's copy is removed.
Pitfalls
Missing headers. The build fails with kernel headers not found. Install linux-headers-<release> for the target kernel.
Directory and version mismatch. PACKAGE_NAME and PACKAGE_VERSION must equal the directory name or dkms add rejects it.
A module that no longer compiles. Kernel internals change between versions, so a module that built on 6.8 may fail on 6.12 with an API error. DKMS only automates the build; it cannot fix the source.
Automatic rebuild disabled. Without AUTOINSTALL="yes" you must run dkms autoinstall or build by hand.
When DKMS is not the right tool
DKMS compiles on every user's machine, which needs a compiler and headers on production hosts and adds boot-time work after a kernel update. For a fleet, build the module once per kernel in a controlled environment and ship the result as a package, a model many distributions use for common drivers. DKMS fits desktops, developers' machines and small deployments where per-machine builds are acceptable.
Inspecting what DKMS did
DKMS keeps its work under /var/lib/dkms/<name>/<version>/. There you find a source link to /usr/src, a directory per kernel release holding the build output and the logs, and a build directory used while compiling. When something fails, the log is /var/lib/dkms/hello/1.0/6.12.12-amd64/x86_64/log/make.log, and it contains the full compiler output including the error that stopped the build. A frequent cause after a kernel upgrade is an API change in the kernel: a function removed or renamed between 6.8 and 6.12 appears in the log as an implicit declaration or an unknown structure member, and the cure is a patch to the module's source, usually guarded with LINUX_VERSION_CODE checks so the same file still builds on older kernels.
Use dkms status after every kernel update on a machine you depend on, before rebooting, to confirm that each module shows installed for the new release. A reboot into a kernel where the network driver failed to build is an avoidable surprise on a remote server, and one command tells you in advance.


