How to Compile a Custom Linux Kernel on Debian

This article outlines the standard, modern procedure for compiling a custom Linux kernel on a Debian-based operating system using native packaging utilities. By building standard .deb packages directly from the kernel source tree, you can ensure clean integration, straightforward dependency management, and easy installation or removal via Debian's standard package management tools.

1. Install Required Build Dependencies

Before compiling, install the necessary development tools, compilers, and libraries required to build the kernel and generate Debian packages:

sudo apt update
sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev bc rsync kmod cpio

2. Obtain and Extract the Kernel Source

You can use the official Debian-packaged kernel source or download a mainline release directly from kernel.org. To use standard vanilla sources:

cd /usr/src
sudo wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.6.tar.xz
sudo tar -xf linux-6.6.tar.xz
cd linux-6.6

3. Configure the Kernel

The safest way to configure a custom kernel is to base it on your currently running Debian configuration:

  1. Copy the current configuration file:

    cp /boot/config-$(uname -r) .config
  2. Update the configuration to match the new source tree:

    make olddefconfig
  3. (Optional) Customize components via the interactive menu:

    make menuconfig
  4. Disable standard Debian build keys that cause compilation errors when building outside the official Debian build infrastructure:

    scripts/config --set-str SYSTEM_TRUSTED_KEYS ""
    scripts/config --set-str SYSTEM_REVOCATION_KEYS ""

4. Compile the Kernel Packages

Modern kernel source trees contain built-in targets to generate Debian packages natively. Run the build command utilizing all available CPU threads:

make -j$(nproc) bindeb-pkg

Note: The bindeb-pkg target builds only the binary packages (linux-image, linux-headers, and linux-libc-dev), significantly reducing build time compared to deb-pkg, which also generates source packages.

5. Install the Generated .deb Packages

Once compilation finishes, the resulting .deb packages will be placed in the parent directory (..). Install the kernel image and headers using dpkg:

cd ..
sudo dpkg -i linux-image-*.deb linux-headers-*.deb

Installing the linux-image package automatically generates the initramfs using initramfs-tools and updates the GRUB bootloader to include the new kernel entry.

6. Reboot and Verify

Restart your system to boot into the newly compiled custom kernel:

sudo reboot

After the system boots, verify that the new kernel is running:

uname -r