7.3. Managing Debian Kernels
Debian installs kernels as packages, and the package names encode both the
policy and the exact version. A metapackage such as linux-image-amd64
depends on the newest kernel for the architecture; a versioned package such
as linux-image-6.12.117+deb13-amd64 contains the actual kernel, modules,
and boot files. Keeping the two concepts separate explains why a system can
have several kernels installed while only one metapackage is marked manual,
and why a fallback kernel needs deliberate protection.
7.3.1. Kernel Metapackages and Installed Kernel Versions
The installed kernel packages and their states are read from dpkg:
$ dpkg-query -W -f='${Package} ${Version} ${Status}\n' "linux-image-*"
linux-image-6.12.117+deb13-amd64 6.12.117-1 install ok installed
linux-image-6.12.117+deb13-amd64-unsigned unknown ok not-installed
linux-image-6.12.86+deb13-amd64 6.12.86-1 install ok installed
linux-image-6.12.86+deb13-amd64-unsigned unknown ok not-installed
linux-image-amd64 6.12.117-1 install ok installed
linux-image-generic unknown ok not-installed
The pattern matches known package names, so not-installed alternatives are
listed as well; the three install ok installed lines are the useful ones.
The machine has two versioned kernels and the linux-image-amd64
metapackage. The version of the metapackage matches the newest installed
kernel because it depends on that exact version:
$ apt-cache policy linux-image-amd64
linux-image-amd64:
Installed: 6.12.117-1
Candidate: 6.12.117-1
Version table:
7.2.12-1~bpo13+1 100
100 https://deb.debian.org/debian trixie-backports/main amd64 Packages
*** 6.12.117-1 500
500 https://security.debian.org/debian-security trixie-security/main amd64 Packages
100 /var/lib/dpkg/status
6.12.107-1 500
500 https://deb.debian.org/debian trixie/main amd64 Packages
The policy output is a reminder that a higher version number in a backports suite is not automatically the right kernel. The backports entry has priority 100 and is therefore not a candidate unless it is explicitly requested; the installed security update wins. On a production machine, choose the kernel series deliberately and test it before making it the default. Chapter 6 explains pinning and backports in detail.
The metapackage's dependency shows the exact kernel it pulls in:
$ apt-cache depends linux-image-amd64 | head -n 6
linux-image-amd64
Depends: linux-image-6.12.117+deb13-amd64
The versioned kernel package supplies the files under /boot and the module
tree under /lib/modules:
$ ls -l /boot/vmlinuz-* /boot/initrd.img-*
-rw-r--r-- 1 root root 55503992 Nov 19 03:39 /boot/initrd.img-6.12.117+deb13-amd64
-rw-r--r-- 1 root root 55150107 Nov 19 03:35 /boot/initrd.img-6.12.86+deb13-amd64
-rw-r--r-- 1 root root 12171200 Nov 12 09:30 /boot/vmlinuz-6.12.117+deb13-amd64
-rw-r--r-- 1 root root 12117952 May 8 2026 /boot/vmlinuz-6.12.86+deb13-amd64
$ ls -1 /lib/modules
6.12.117+deb13-amd64
6.12.86+deb13-amd64
Each kernel has a compressed image, an initramfs, and a module directory.
The config-* and System.map-* files are also kept in /boot; the listing
requested only the kernel and initramfs images. Modules are never shared
between kernel versions: a module built for 6.12.117 has a vermagic
string that names that kernel and will not load into 6.12.86. This is why
installing headers and rebuilding DKMS modules after a kernel update is not
optional when an out-of-tree driver is in use.
Table 7.6 lists the kernel-related packages an administrator is most likely to encounter. The metapackages are the ones to install deliberately; the versioned packages are normally pulled in as dependencies.
| Package | Contents and role |
|---|---|
linux-image-amd64 |
Metapackage depending on the newest linux-image-<version> for amd64. |
linux-image-<version> |
Kernel image, modules, System.map, config, and maintainer scripts. |
linux-headers-amd64 |
Metapackage depending on the matching versioned headers. |
linux-headers-<version> |
Build files for out-of-tree modules and DKMS. |
linux-kbuild-<version> |
Kernel build infrastructure used by the headers. |
linux-image-<version>-unsigned |
Variant without the Debian signature; normally not needed. |
APT protects kernel and firmware packages from automatic removal through
NeverAutoRemove patterns. The relevant configuration can be read directly:
$ grep -A4 "NeverAutoRemove" /etc/apt/apt.conf.d/01autoremove
NeverAutoRemove
{
"^firmware-linux.*";
"^linux-firmware$";
"^linux-image-[a-z0-9]*$";
The file continues with another linux-image pattern. These expressions,
together with APT's versioned-kernel handling, are why a routine
apt-get autoremove does not necessarily remove an older kernel even when it
is marked automatic. They are a safety net, not a
guarantee: an explicit apt-get remove can still delete a kernel, and a
locally modified or third-party kernel package may not match the patterns.
Check the installed set before and after any operation that removes packages.
7.3.2. Selecting a Previous Kernel and Preserving a Fallback
A fallback kernel is useful only if it is installed, bootable, and known to
work with the machine's storage and network. The installed versions are
marked automatic or manual by APT. apt-mark can show both sets:
$ apt-mark showauto "linux-image-*"
linux-image-6.12.117+deb13-amd64
linux-image-6.12.86+deb13-amd64
The older kernel is currently automatic, meaning APT may consider it for removal when its dependencies change. Marking it manual records the intent to keep it:
$ sudo apt-mark manual linux-image-6.12.86+deb13-amd64
linux-image-6.12.86+deb13-amd64 set to manually installed.
$ apt-mark showmanual "linux-image-*"
linux-image-6.12.86+deb13-amd64
linux-image-amd64
The manual list now contains the older kernel and the metapackage. The marking does not install anything or change the default boot entry; it only changes APT's dependency bookkeeping. To undo the demonstration:
$ sudo apt-mark auto linux-image-6.12.86+deb13-amd64
linux-image-6.12.86+deb13-amd64 set to automatically installed.
On a real machine, keep the older kernel manual and verify it boots before relying on it. A kernel that is installed but never tested is not a fallback.
GRUB can select a menu entry for the next boot without changing the default.
grub-reboot writes next_entry into /boot/grub/grubenv; the entry can be
a number, a title, or a submenu path separated by >. Using the title is less
fragile than counting entries:
# grub-reboot "Advanced options for Debian GNU/Linux>Debian GNU/Linux, with Linux 6.12.86+deb13-amd64"
The command is silent. The pending value is visible in the environment block:
# grub-editenv list
next_entry=Advanced options for Debian GNU/Linux>Debian GNU/Linux, with Linux 6.12.86+deb13-amd64
grub-editenv list prints the saved variables. The > separates the
top-level submenu title from the entry title; the shell needs the whole value
quoted because > would otherwise be a redirection. Reboot and confirm which
kernel is running:
$ uname -r
6.12.86+deb13-amd64
The kernel command line is the old kernel's line, without the test parameters that were removed earlier:
$ cat /proc/cmdline
BOOT_IMAGE=/boot/vmlinuz-6.12.86+deb13-amd64 root=UUID=be225267-eaf4-410c-9ffa-5cd32b221ce0 ro consoleblank=0 elevator=noop scsi_mod.use_blk_mq=Y net.ifnames=0 biosdevname=0
A graphical target can still be reached; the fallback kernel is a complete kernel, not a recovery mode:
$ systemctl is-active graphical.target
active
After the one-time boot, GRUB clears the pending entry. grub-editenv list
shows the variable with an empty value rather than removing the line:
# grub-editenv list
next_entry=
To return to the default kernel, either reboot normally or set the first entry explicitly:
# grub-reboot 0
The command is silent. After the reboot, uname -r reports
6.12.117+deb13-amd64 again. The
persistent default is still GRUB_DEFAULT=0; grub-reboot changed only the
one boot. Table 7.7 summarizes the GRUB selection tools.
| Command | Effect |
|---|---|
grub-reboot ENTRY |
Use ENTRY for the next boot only; writes next_entry. |
grub-set-default ENTRY |
Set the saved default entry; requires GRUB_DEFAULT=saved. |
grub-editenv list |
Show saved GRUB environment variables such as next_entry. |
grub-editenv - unset next_entry |
Remove a pending one-time entry. |
update-grub |
Regenerate /boot/grub/grub.cfg from the configuration inputs. |
The numeric index is the entry's position in the generated menu, counting
from zero; a submenu entry is addressed as submenu>entry. Numeric indices
can change when a kernel is added or removed, which is why a title or an
explicit grub-reboot from a known menu is safer for an automated recovery
script. The title must match exactly, including the kernel version.
7.3.3. Kernel Headers, DKMS, and Module Signing
Out-of-tree modules need the kernel headers for the exact running kernel and
a build environment. The metapackage linux-headers-amd64 pulls in the
headers for the newest kernel, while dkms provides the framework that
rebuilds a module when that kernel changes:
$ sudo apt-get install linux-headers-amd64 dkms build-essential
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
g++ g++-14 g++-14-x86-64-linux-gnu g++-x86-64-linux-gnu libstdc++-14-dev
Suggested packages:
menu g++-multilib g++-14-multilib gcc-14-doc libstdc++-14-doc
The following NEW packages will be installed:
build-essential dkms g++ g++-14 g++-14-x86-64-linux-gnu g++-x86-64-linux-gnu
libstdc++-14-dev linux-headers-amd64
0 upgraded, 8 newly installed, 0 to remove and 29 not upgraded.
Need to get 0 B/14.6 MB of archives.
After this operation, 60.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
The captured output is shortened after the confirmation prompt; the
installation continued with unpacking and Setting up lines for each
package. After it finishes, the installed versions are:
$ dpkg-query -W -f='${Package} ${Version}\n' linux-headers-amd64 dkms build-essential
build-essential 12.12
dkms 3.2.2-1~deb13u1
linux-headers-amd64 6.12.117-1
The headers for the running kernel are under /usr/src:
$ ls -d /usr/src/linux-headers-$(uname -r)
/usr/src/linux-headers-6.12.117+deb13-amd64
A minimal DKMS module makes the build and signing path concrete. Create the source in john's workspace; the parent directory is the chapter's workspace:
$ mkdir -p ~/debian-handbook/boot-kernel/ch07-hello-1.0
The command prints nothing. The C source is a module that logs one line when loaded and one when unloaded:
$ cat > ~/debian-handbook/boot-kernel/ch07-hello-1.0/ch07_hello.c <<"EOF"
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Debian Handbook Lab");
MODULE_DESCRIPTION("Minimal DKMS demonstration module");
static int __init ch07_hello_init(void)
{
pr_info("ch07_hello: loaded\n");
return 0;
}
static void __exit ch07_hello_exit(void)
{
pr_info("ch07_hello: unloaded\n");
}
module_init(ch07_hello_init);
module_exit(ch07_hello_exit);
EOF
The here-document is fed to cat; the command itself produces no output.
Next, the kbuild Makefile and the DKMS description:
$ cat > ~/debian-handbook/boot-kernel/ch07-hello-1.0/Makefile <<"EOF"
obj-m := ch07_hello.o
EOF
$ cat > ~/debian-handbook/boot-kernel/ch07-hello-1.0/dkms.conf <<"EOF"
PACKAGE_NAME="ch07-hello"
PACKAGE_VERSION="1.0"
BUILT_MODULE_NAME[0]="ch07_hello"
DEST_MODULE_LOCATION[0]="/updates/dkms"
AUTOINSTALL="yes"
MAKE[0]="make -C /lib/modules/${kernelver}/build M=${dkms_tree}/${PACKAGE_NAME}/${PACKAGE_VERSION}/build modules"
EOF
PACKAGE_NAME and PACKAGE_VERSION identify the DKMS tree;
BUILT_MODULE_NAME[0] is the module file without .ko;
DEST_MODULE_LOCATION[0] places it under updates/dkms; AUTOINSTALL=yes
rebuilds it for new kernels; and MAKE[0] invokes the kernel build system
with the copied source directory. The files are owned by john and readable:
$ ls -l ~/debian-handbook/boot-kernel/ch07-hello-1.0
total 12
-rw-rw-r-- 1 john john 22 Nov 19 04:28 Makefile
-rw-rw-r-- 1 john john 428 Nov 19 04:28 ch07_hello.c
-rw-rw-r-- 1 john john 254 Nov 19 04:28 dkms.conf
DKMS expects source archives or directories under /usr/src, so copy the
directory there. The copy is a root operation because /usr/src is
root-owned:
$ sudo cp -r /home/john/debian-handbook/boot-kernel/ch07-hello-1.0 /usr/src/
The command is silent. DKMS is driven by a few subcommands, summarized in Table 7.8. Register the source, build it, and install the built module into the module tree:
| Command | Description |
|---|---|
dkms add -m NAME -v VERSION |
Register the source directory under /usr/src. |
dkms build -m NAME -v VERSION |
Build the module against the selected kernel. |
dkms install -m NAME -v VERSION |
Install it under updates/dkms and run depmod. |
dkms remove -m NAME -v VERSION --all |
Remove it from all kernels and the DKMS tree. |
dkms status |
Show the module, kernel, and installed/built state. |
$ sudo dkms add -m ch07-hello -v 1.0
Creating symlink /var/lib/dkms/ch07-hello/1.0/source -> /usr/src/ch07-hello-1.0
$ sudo dkms build -m ch07-hello -v 1.0
Sign command: /lib/modules/6.12.117+deb13-amd64/build/scripts/sign-file
Signing key: /var/lib/dkms/mok.key
Public certificate (MOK): /var/lib/dkms/mok.pub
Certificate or key are missing, generating self signed certificate for MOK...
Building module(s)... done.
Signing module /var/lib/dkms/ch07-hello/1.0/build/ch07_hello.ko
$ sudo dkms install -m ch07-hello -v 1.0
Installing /lib/modules/6.12.117+deb13-amd64/updates/dkms/ch07_hello.ko.xz
Running depmod.... done.
The build output is the important part for module signing. In this example,
DKMS found no MOK key, generated a self-signed key pair, signed the module
with it, and installed the compressed module under
/lib/modules/$(uname -r)/updates/dkms. The state is recorded in the DKMS
database:
$ sudo dkms status
ch07-hello/1.0, 6.12.117+deb13-amd64, x86_64: installed
The module metadata reports the signature:
$ /usr/sbin/modinfo /lib/modules/$(uname -r)/updates/dkms/ch07_hello.ko.xz | sed -n "1,8p"
filename: /lib/modules/6.12.117+deb13-amd64/updates/dkms/ch07_hello.ko.xz
description: Minimal DKMS demonstration module
author: Debian Handbook Lab
license: GPL
depends:
name: ch07_hello
retpoline: Y
vermagic: 6.12.117+deb13-amd64 SMP preempt mod_unload modversions
The path to modinfo is spelled out because module tools live in /usr/sbin,
which is not always in an ordinary user's PATH; sudo modinfo works for
the same reason. The signer is the local DKMS key:
$ /usr/sbin/modinfo -F signer /lib/modules/$(uname -r)/updates/dkms/ch07_hello.ko.xz
DKMS module signing key
A stock Debian module is signed by the kernel build key instead:
$ /usr/sbin/modinfo -F signer /lib/modules/$(uname -r)/kernel/drivers/net/dummy.ko.xz
Build time autogenerated kernel key
The private key is protected, but the public certificate exists so it can be enrolled:
# ls -l /var/lib/dkms/mok.key /var/lib/dkms/mok.pub
-rw------- 1 root root 1704 Nov 19 04:28 /var/lib/dkms/mok.key
-rw-r--r-- 1 root root 832 Nov 19 04:28 /var/lib/dkms/mok.pub
Load the module and observe both the success and the kernel's trust decision:
# modprobe ch07_hello
The command is silent. The module is listed, and its initialization message is in the kernel journal:
$ lsmod | grep ch07_hello
ch07_hello 12288 0
# journalctl -k --no-pager | grep ch07_hello | tail -n 4
Nov 19 04:29:01 trixie kernel: ch07_hello: loading out-of-tree module taints kernel.
Nov 19 04:29:01 trixie kernel: ch07_hello: module verification failed: signature and/or required key missing - tainting kernel
Nov 19 04:29:01 trixie kernel: ch07_hello: loaded
The module loaded, but the kernel did not trust its signature because the
DKMS certificate is not in the kernel's trusted keyring. This is the normal
state on a machine without Secure Boot enrollment; the module works and the
kernel is marked tainted. On a Secure Boot machine the load would be refused
until the MOK is enrolled through MokManager. The taint flag is permanent for
the boot and is visible in /proc/sys/kernel/tainted; it is a warning that
an out-of-tree module is in use, not proof of a fault. Unload the module:
# modprobe -r ch07_hello
The command is silent. When the demonstration is over, remove the installed module and the source:
# dkms remove -m ch07-hello -v 1.0 --all
Module ch07-hello/1.0 for kernel 6.12.117+deb13-amd64 (x86_64):
Before uninstall, this module version was ACTIVE on this kernel.
Deleting /lib/modules/6.12.117+deb13-amd64/updates/dkms/ch07_hello.ko.xz
Running depmod.... done.
Deleting module ch07-hello/1.0 completely from the DKMS tree.
The ACTIVE wording is DKMS's record of the installed module for that
kernel; the runtime module had already been unloaded. Remove the copied
source:
# rm -rf /usr/src/ch07-hello-1.0
The command is silent. The source in john's workspace remains available for
inspection or a later rebuild; remove it when it is no longer needed. The
DKMS key pair remains under /var/lib/dkms; it can be reused by later DKMS
builds or removed after all locally built modules are gone.
DKMS is only as reliable as its last build. After a kernel upgrade, check
dkms status before rebooting; a module that failed to build against the new
kernel will not be available after the reboot. The headers package must be
for the new kernel, and a compiler upgrade can also require a rebuild. If a
build fails, fix the source or pin the previous kernel rather than rebooting
into a system without a required driver.