Table of Contents#
- Prerequisites
- Getting the Kernel Source Code
- Configuring the Kernel
- Building the Kernel
- Installing the Custom Kernel
- Testing and Validation
- Troubleshooting Boot Issues
- Cleaning Up
- References
Prerequisites#
Before starting, ensure your system meets these requirements:
- Ubuntu/Debian system: Tested on Ubuntu 24.04 LTS and Debian 12.
- Disk space: At least 35 GB free (kernel sources + build artifacts).
- Time: Building takes 15 minutes to 2+ hours (varies by CPU cores and available RAM).
- RAM: At least 8 GB recommended; 16 GB or more speeds up the build significantly.
- Dependencies: Install build tools and libraries:
sudo apt update && sudo apt install -y \ build-essential libncurses-dev libssl-dev libelf-dev \ bison flex dwarves fakeroot git - Root access: You’ll need
sudoprivileges for installation steps.
Getting the Kernel Source Code#
You can obtain kernel sources from three places:
Option 1: Official Ubuntu/Debian Sources (Recommended for Stability)#
Ubuntu and Debian package their kernel sources, ensuring compatibility with system tools.
-
Enable source repositories:
On Ubuntu 24.04+, edit/etc/apt/sources.list.d/ubuntu.sourcesand adddeb-srcto theTypes:line (e.g.,Types: deb deb-src). On older systems, edit/etc/apt/sources.listand uncomment lines starting withdeb-src. -
Fetch the source for your current kernel:
# Get the version of your running kernel uname -r # Output: e.g., 6.8.0-38-generic # Fetch the source (replace <version> with output from uname -r) sudo apt update apt source linux-image-unsigned-$(uname -r)This creates a directory like
linux-6.8.0in your current folder.
Option 2: Latest Stable Kernel (kernel.org)#
For the newest features, download from kernel.org:
wget https://cdn.kernel.org/pub/linux/kernel/v7.x/linux-7.1.tar.xz
tar -xf linux-7.1.tar.xz
cd linux-7.1 Option 3: Git Repository (Bleeding Edge)#
For development versions, clone the Linux kernel git repo:
git clone https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
cd linux
git checkout v7.1 # Checkout a specific stable version Configuring the Kernel#
Kernel configuration determines which features, drivers, and optimizations are included. Start with a baseline config (e.g., your current kernel’s settings) and tweak from there.
Step 1: Copy the Current Kernel Config#
Use your existing kernel’s config as a starting point to avoid breaking hardware support:
# From the kernel source directory
cp /boot/config-$(uname -r) .config Step 2: Launch the Configuration Tool#
Edit the config with menuconfig (text-based) or xconfig/gconfig (GUI):
make menuconfig # Text-based (recommended for servers/SSH)
# OR
make xconfig # Qt-based GUI (needs X11)
# OR
make gconfig # GTK-based GUI Key Configuration Options to Tweak#
Here are common optimizations (navigate using arrow keys; press ? for help):
- General Setup → Local version - append to kernel release: Add a custom suffix (e.g.,
-mycustom) to identify your kernel (e.g.,7.1-mycustom). - Processor type and features → Processor family: Select your CPU (e.g., "Intel Core2/Newer Xeon" or "AMD Opteron/Athlon64").
- Processor type and features → Preemption Model:
- "No Forced Preemption" (server workloads, maximum throughput).
- "Preemptible Kernel (Low-Latency Desktop)" (desktop/gaming, better responsiveness).
- Kernel Features → Timer frequency: Set to
1000 Hz(default is 250 Hz) for lower latency (caution: may increase power usage). - Device Drivers: Disable unused hardware support (e.g., "Network device support" → "Wireless LAN" → uncheck drivers for Wi-Fi cards you don’t own).
- File systems: Disable unused filesystems (e.g., Btrfs, XFS if using only ext4).
- Security options: Enable "Control Flow Integrity" (CFI) or "Kernel Address Space Layout Randomization (KASLR)" for hardening.
After tweaking, save the config (default filename .config) and exit.
Building the Kernel#
Choose one of these methods to build the kernel. make bindeb-pkg (Debian-style) is recommended for easier installation/removal. Method 3 is best when you want to apply Ubuntu's official patches and configuration.
Method 1: Traditional make Workflow#
This method builds and installs the kernel directly.
Step 1: Clean Previous Build Artifacts (If Reconfiguring)#
If you’ve built before, clean old files:
make clean # Removes most build files (keeps .config)
# OR
make mrproper # Removes .config and all build files (start fresh) Step 2: Build the Kernel and Modules#
Compile the kernel and modules. Use -jN with N = number of CPU cores + 1 for faster builds:
# Check CPU cores: nproc → e.g., 8 cores → use -j9
make -j$(nproc)
make modules -j$(nproc) # Builds kernel modules Step 3: Install Modules and Kernel#
sudo make modules_install # Install modules to /lib/modules/<version>
sudo make install # Copy kernel image, System.map, and update GRUB Method 2: Debian-Style make bindeb-pkg (Recommended)#
make bindeb-pkg is a built-in kernel Makefile target that builds .deb packages, simplifying installation, removal, and dependency management. This is the modern replacement for the deprecated make-kpkg.
Step 1: Build the Debian Package#
From the kernel source directory:
# -jN: parallel jobs (use nproc for optimal speed)
# KDEB_PKGVERSION: custom version suffix (e.g., "mycustom1")
make -j$(nproc) bindeb-pkg KDEB_PKGVERSION=$(make kernelversion)-mycustom1 This generates two .deb files in the parent directory:
linux-image-7.1-mycustom1_amd64.deb(kernel binary)linux-headers-7.1-mycustom1_amd64.deb(headers for module compilation)
Method 3: Ubuntu's Official Build Method (Alternative)#
For building Ubuntu-specific kernels with official patches applied, use the Ubuntu build system:
# From the kernel source directory
chmod a+x debian/rules
chmod a+x debian/scripts/*
chmod a+x debian/scripts/misc/*
fakeroot debian/rules clean
fakeroot debian/rules binaryThis produces .deb packages in the parent directory with Ubuntu's official kernel configuration and patches.
Note on
make-kpkg: You may encounter older guides that referencemake-kpkgfrom thekernel-packagepackage. This tool has been deprecated and removed from modern Debian and Ubuntu releases. Usemake bindeb-pkginstead — it provides the same functionality without requiring an external package.
Installing the Custom Kernel#
If Using make install (Method 1):#
The make install step already copies files and updates GRUB. Skip to testing.
If Using make bindeb-pkg (Method 2):#
Install the .deb packages:
# From the directory containing the .deb files
sudo dpkg -i linux-image-7.1-mycustom1_amd64.deb
sudo dpkg -i linux-headers-7.1-mycustom1_amd64.deb Update GRUB (Critical!)#
Ensure GRUB recognizes the new kernel:
sudo update-grub Testing and Validation#
Reboot your system and select the custom kernel from the GRUB menu (hold Shift during boot if GRUB doesn’t appear).
Verify the Kernel Version#
After logging in, confirm the new kernel is running:
uname -r # Should output: 7.1-mycustom1 (or your custom suffix) Check Hardware and Functionality#
Test critical hardware (network, sound, graphics) and workloads:
ping google.com(network)- Play audio/video (sound card)
- Run
glxinfo | grep "OpenGL renderer"(graphics drivers)
Monitor Performance/Stability#
Use tools like htop (CPU/memory), iostat (disk I/O), or dmesg (kernel logs) to check for errors:
dmesg | grep -i error # Look for critical issues Troubleshooting Boot Issues#
If the system fails to boot:
Step 1: Revert to the Old Kernel#
Reboot and select your previous kernel from the GRUB "Advanced options" menu.
Step 2: Diagnose the Problem#
- Check GRUB entries:
grep menuentry /boot/grub/grub.cfg - View kernel logs:
journalctl -b -1(logs from last boot) - Missing initramfs: Regenerate with
sudo update-initramfs -c -k 7.1-mycustom1 - Incompatible drivers: Rebuild with
make menuconfigand re-enable critical drivers (e.g., storage controllers).
Step 3: Uninstall the Custom Kernel#
If all else fails, remove the custom kernel:
-
If using
make bindeb-pkg(Debian packages):sudo apt remove linux-image-7.1-mycustom1 linux-headers-7.1-mycustom1 -
If using
make install:
Manually delete kernel files:sudo rm -rf /boot/vmlinuz-7.1-mycustom1 \ /boot/initrd.img-7.1-mycustom1 \ /lib/modules/7.1-mycustom1 sudo update-grub
Cleaning Up#
Free disk space by removing build artifacts and old kernels.
Remove Source Build Files#
From the kernel source directory:
make clean # Removes object files (keeps .config)
# OR
make mrproper # Removes all build files and .config (start fresh) Remove Old Kernels#
Keep only the 2-3 most recent kernels:
# List installed kernels
dpkg --list | grep linux-image
# Remove old kernels (replace <version> with the kernel to delete)
sudo apt remove linux-image-<version> linux-headers-<version>
sudo apt autoremove # Clean up dependencies References#
- Ubuntu Kernel Documentation (Official build guide for Ubuntu kernels)
- Debian Kernel Handbook
- Linux Kernel Newbies (Beginner-friendly guides)
- Kernel Configuration Options
- Linux Kernel Archives (Official kernel source downloads)
By following these steps, you’ve built and installed a custom kernel tailored to your needs. Whether optimizing for performance, adding hardware support, or learning, this process empowers you to take full control of your system’s core. With Linux 7.x and Ubuntu 24.04/26.04 LTS, there's never been a better time to explore kernel customization. Happy hacking! 🐧