When you upgrade a Linux Kernel, the out-of-tree modules may break because they were compiled for the previous Kernel version. Normally you resolve this by installing the Linux Headers for the new Kernel version and then compiling the modules again from source and loading them in to the Kernel.
DKMS (Dynamic Kernel Module Support) automates this process for you for every Kernel upgrade so you don’t have to worry about your out of tree modules breaking and having to manually compile and load them again.
Pre-Requisites before proceeding
- Tools required
dkmspackage- Kernel Headers meta package
- build/compile tools
- Module source code
- It should contain
dkms.conffile - It should contain
Makefile
- It should contain
- Root privileges
Let’s install the required packages
apt install dkms build-essential
DKMS depends on binary build tools like make, gcc, g++ etc. to work. You can install these using a package bundle apt install build-essential
Install this meta package to automatically install the new kernel headers after a kernel upgrade: apt install linux-headers-generic
Install Kernel headers for your current version so we can install our first module with DKMS: apt install linux-headers-$(uname -r)
Let’s see how to compile and add a module through DKMS
-
Download the source code and open the
dkms.conffile in it.If it’s missing, you may have to create one which I’m not covering in this article.
Note the
PACKAGE_VERSIONandPACKAGE_NAME. -
Create a directory under
/usr/srclike/usr/src/<PACKAGE_NAME>-<PACKAGE_VERSION> -
Copy the source code (along with
dkms.conf)into/usr/src/<PACKAGE_NAME>-<PACKAGE_VERSION> -
Add the module to DKMS’ tracking tree so it knows about it when the Kernel upgrades.
sudo dkms add -m <PACKAGE_NAME> -v PACKAGE_VERSIONNote: This basically creates a symlink from
/usr/src/<PACKAGE_NAME>-<PACKAGE_VERSION>to/var/lib/dkms/<PACKAGE_NAME>/<PACKAGE_VERSION>/source -
Let’s build the module from source code
sudo dkms build -m <PACKAGE_NAME> -v PACKAGE_VERSIONNote: Some modules depend on other modules to function. When you’re building a module using DKSM, it may also build and install it’s dependency modules. You can check this by reading the
dkms.conffile. Look forBUILT_MODULE_NAMEvariable.Example:

-
Install the compiled module (not loading into Kernel)
sudo dkms install -m <PACKAGE_NAME> -v PACKAGE_VERSION -
Test if DKMS has properly installed the module - ie. dropped the compiled module file to the proper location for the Kernel which is under
/lib/modules/<kernel-version>/.sudo dkms status -
Finally let’s load the module into the Kernel
sudo modprobe <PACKAGE_NAME>Note: If you’re getting an error: modprobe: ERROR: could not insert ‘veeamblksnap’: Key was rejected by service - it’s because your UEFI firmware has secure boot enabled and it doesn’t have the corresponding public key to verify the module before loading it into the Kernel.
Refer below section on how to add the key to firmware and resume from this step.
-
Verify if the module is loaded into Kernel
sudo lsmod | grep -i <PACKAGE_NAME> -
Configure to load module on system bootup
This may vary between systems. If you’re using systemd, then create a config file under
/etc/modules-load.d/my_module_name.confand add the module name in it.
Removing a module that was previously installed through DKMS
-
Unload the module from Kernel
sudo modprobe -r <MODULE-NAME> -
Find the module version and name using
sudo dkms status -
Remove from DKMS database
sudo dkms remove <module>/<module-version> --all -
Remove the source code from under
/usr/src -
Remove auto load config file from under
/etc/modules-load.d/
Let’s see how to load modules with Secure Boot enabled
If you have not setup an MOK (Machine Owner Key) before, DKMS auto generates a key pair for you and signs the module with the private key when you run dkms build command.

Of course this signing step is ignored if secure boot is disabled.
If you want to use your own MOK key, tell DKMS where it is in /etc/dkms/framework.conf under mok_signing_key= variable before running the build command. Then run dkms install .
At this point if you try to load the module (modprobe) you’ll get an error because the UEFI firmware doesn’t have the corresponding public key to verify the module.
So add the pub key to the UEFI firmware using
sudo mokutil --import /var/lib/dkms/mok.pub
It’ll ask to input a password, you’ll need it later.
Reboot your system.
The UEFI firmware will present an MOK Management screen where you have to use the above password to confirm key upload.

Here is a guide put together by the DKMS developers on how to verify the key upload from the MOK management screen: https://github.com/dkms-project/dkms#secure-boot
After logging in, verify if the key has been successfully uploaded using mokutil --list-enrolled | grep -i DKMS
Finally add the module using sudo modprobe <MODULE NAME>
To learn about MOK, DKMS+Secure Boot, check out this doc from Debian: https://wiki.debian.org/SecureBoot#MOK_-_Machine_Owner_Key
Troubleshooting Tips
- Check Kernel logs using
sudo dmesg -T -H | grep - i <module name>orjournalctl -k - DKMS logs are under
/var/lib/<MODULE-NAME>/.../log/make.log
Package Managers and DKMS
Most modern modules are distributed through package repositories. They package their modules with DKMS config and distributed through package repositories.
The package manager (apt, dnf, pacman … ) script basically automates all of the above steps to add, build, install and load modules using DKMS - thus simplifying the experience for the user.