Optimizing the Linux Kernel: How to ‘Tailor-Make’ and Package .deb for Ubuntu and Debian

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

The Default Kernel: When Versatility Becomes a Burden

Have you ever wondered why a modest VPS configuration (1 vCPU, 2GB RAM) running Ubuntu 22.04 feels sluggish even before you’ve installed much? I encountered this situation when deploying an application that required extremely low latency. Ubuntu’s default kernel is excellent because it “recognizes” almost any hardware, from ancient printers to the latest graphics cards. However, this broad compatibility creates unnecessary overhead.

Every time you boot, the system has to load dozens of redundant modules. Do you really need webcam drivers, obscure Wi-Fi cards, or ReiserFS file system support on a web server? Absolutely not. These modules not only occupy RAM but also cause the CPU to waste resources processing unnecessary interrupts.

Why You Should Build Your Own Kernel Instead of Using the Generic Version

Distributions like Debian or Ubuntu prioritize “out-of-the-box” compatibility. Consequently, their kernels are always in a Generic format.

Imagine you are managing a production server that only runs Nginx and MySQL. Keeping Xbox controller or Bluetooth drivers is a clear waste of resources. Removing them not only makes the system lighter but also narrows the attack surface, limiting security vulnerabilities from old, rarely used modules.

Additionally, self-compiling allows you to optimize specifically for your current CPU architecture, such as AMD EPYC or the latest Intel Xeon. This helps leverage specific instruction sets that generic kernels often overlook.

3 Popular Options for Upgrading Your Kernel

When performance optimization is needed, technical experts usually consider these three paths:

  • Third-party Kernels (XanMod, Liquorix): Fast installation, noticeable performance improvements, but you are dependent on someone else’s configuration.
  • Mainline Kernels from PPA: Pre-built versions from kernel.org for Ubuntu. The advantage is they are up-to-date, but they still contain too many redundant modules.
  • Compiling from Source: This path is the most time-consuming but yields the most satisfying results. You have total control over what stays in your system.

The Process of “Tailoring” a Kernel and Professional .deb Packaging

In a real-world project, I tried fine-tuning the Kernel for a 4GB RAM server. The result was a reduction of about 180MB in background RAM usage and a 5-second reduction in boot time. Here is how I did it to create .deb files, making deployment to multiple servers with the same configuration incredibly simple.

Step 1: Preparing the Compilation Toolchain

First, install the supporting libraries. This process will take up about 500MB of disk space.

sudo apt update
sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev -y
sudo apt install xz-utils wget curl bc -y

Step 2: Downloading the Official Kernel Source

Visit kernel.org to get the link for the latest Stable version. Here, I am choosing version 6.6.x.

mkdir ~/kernel-build && cd ~/kernel-build
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.6.15.tar.xz
tar -xvf linux-6.6.15.tar.xz
cd linux-6.6.15

Step 3: Fine-tuning Configuration – Trimming the Fat

Don’t blindly select every feature from scratch because the Kernel has thousands of options. The smartest way is to inherit the current Ubuntu configuration and then trim it down.

cp /boot/config-$(uname -r) .config
make menuconfig

In the menuconfig interface, focus on the following sections:

  • Processor type and features: Select your specific CPU family (e.g., Core i7, AMD Zen 3) instead of Generic.
  • Device Drivers: Disable Wireless, Sound card, or Multimedia support if this is a data center server.
  • File systems: If you only use ext4, disable Btrfs, XFS, or NTFS to keep the Kernel lean.

Pro Tip: For Ubuntu, look for the CONFIG_SYSTEM_TRUSTED_KEYS entry and leave its value empty. Otherwise, the build process will fail due to digital certificate errors.

Step 4: Automatic Compilation and Packaging

Instead of using the manual make install command, I recommend using bindeb-pkg. This command generates .deb files that make managing, uninstalling, or copying to another machine extremely clean.

To save time, leverage all your CPU power:

make -j$(nproc) bindeb-pkg

The waiting time usually takes between 20 to 60 minutes. This is a good time to take a break or handle other tasks.

Step 5: Installation and Verification

Once complete, the installation files will appear in the ~/kernel-build directory. You only need to install the two most important files:

cd ..
sudo dpkg -i linux-image-6.6.15*.deb linux-headers-6.6.15*.deb
sudo update-grub
sudo reboot

After rebooting, type uname -a. If the new version appears, you have officially taken control of the “heart” of your system.

Lessons Learned the Hard Way

Compiling the Kernel isn’t an everyday task, but it helps you gain a deep understanding of how Linux operates. A well-optimized system will be more stable and significantly more responsive than a default configuration.

One last note: Always keep an old Kernel version in the Grub menu. If the new version suffers a Kernel Panic, you still have a way back into the system to fix the error. Don’t rush to delete the default Kernel until the new one has run stably for at least a week.

Share: