Skip to content

Kernel_compilation

Comprehensive Guide to Building Custom Kernels

Section titled “Comprehensive Guide to Building Custom Kernels”

Reasons for Custom Kernel
+------------------------------------------------------------------+
| |
| Benefits: |
| +----------------------------------------------------------+ |
| | • Smaller kernel size (remove unused features) | |
| | • Better hardware support (latest drivers) | |
| | • Performance optimization | |
| | • Security patches (latest) | |
| | • Learning about kernel internals | |
| | • Custom patches/features | |
| +----------------------------------------------------------+ |
| |
| When NOT to compile: |
| +----------------------------------------------------------+ |
| | • Production systems (use distribution kernel) | |
| | • Beginners (complex troubleshooting) | |
| | • Need enterprise support | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Terminal window
# Arch Linux
sudo pacman -S base-devel bc cpio elfutils gzip libelf \
openssl pkgconf tar xz
# Ubuntu/Debian
sudo apt install build-essential bc cpio flex libelf-dev \
libncurses5-dev libssl-dev pkg-config
# RHEL/CentOS
sudo yum groupinstall "Development Tools"
sudo yum install ncurses-devel openssl-devel elfutils-libelf-devel
Terminal window
# From kernel.org
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.6.tar.xz
tar -xf linux-6.6.tar.xz
cd linux-6.6
# From Git
git clone --depth 1 https://github.com/torvalds/linux.git
cd linux
# Using apt source (Debian/Ubuntu)
apt source linux-image-$(uname -r)

Terminal window
# Using current config (recommended)
cp /boot/config-$(uname -r) .config
# Using localmodconfig (modules from current system)
make localmodconfig
# Using localyesconfig (everything currently used as built-in)
make localyesconfig
# Using defconfig (default)
make defconfig
# Interactive configuration
make menuconfig # NCurses (terminal)
make xconfig # Qt (graphical)
make gconfig # GTK (graphical)
# Configuration helpers
make olddefconfig # Use existing config, set new defaults
make savedefconfig # Save minimal config
Kernel Configuration
+------------------------------------------------------------------+
| |
| Processor Type: |
| +----------------------------------------------------------+ |
| | Processor family → Select your CPU architecture | |
| | General setup → Tickless system (for servers) | |
| +----------------------------------------------------------+ |
| |
| Loadable Module Support: |
| +----------------------------------------------------------+ |
| | Enable loadable module support = Y | |
| | Module unloading = Y | |
| +----------------------------------------------------------+ |
| |
| File Systems: |
| +----------------------------------------------------------+ |
| | Ext4, XFS, Btrfs (select needed) | |
| | Network file systems (NFS, CIFS) | |
| +----------------------------------------------------------+ |
| |
| Device Drivers: |
| +----------------------------------------------------------+ |
| | Select hardware drivers for your system | |
| | Graphics, network, storage | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Terminal window
# Clean before build
make clean # Remove most build files
make mrproper # Remove all generated files
make distclean # mrproper + editor files
# Compile
make -j$(nproc) # Parallel build
make -j8 # 8 jobs
# Install modules
sudo make modules_install
# Install kernel
sudo make install
# Update bootloader
sudo grub-mkconfig -o /boot/grub/grub.cfg
# Alternative: Using deb-pkg (Debian/Ubuntu)
make deb-pkg -j$(nproc)
Build Output
+------------------------------------------------------------------+
| |
| After compilation: |
| +----------------------------------------------------------+ |
| | arch/x86/boot/bzImage | Kernel image | |
| | vmlinux | uncompressed kernel | |
| | System.map | Symbol table | |
| | .config | Configuration | |
| +----------------------------------------------------------+ |
| |
| After modules_install: |
| +----------------------------------------------------------+ |
| | /lib/modules/<version>/ | Kernel modules | |
| +----------------------------------------------------------+ |
| |
| After make install: |
| +----------------------------------------------------------+ |
| | /boot/vmlinuz-<version> | Kernel image | |
| | /boot/initrd.img-<version> | Initial ramdisk | |
| | /boot/System.map-<version> | Symbol table | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Terminal window
# List installed kernels
ls -la /boot/vmlinuz-*
ls -la /lib/modules/
# Check boot entry
cat /boot/grub/grub.cfg | grep "menuentry"
# Reboot and select new kernel
sudo reboot
# Check kernel version
uname -r
uname -a
# Remove old kernels (Debian/Ubuntu)
sudo apt autoremove
sudo apt autoclean

  1. Why compile a custom kernel?

    • Performance, size optimization, custom features
  2. What’s the difference between make clean and make mrproper?

    • clean removes build files, mrproper removes everything
  3. What is localmodconfig?

    • Uses current system config as base
  4. What is initrd/initramfs?

    • Initial ram disk for early boot

Quick Reference
+------------------------------------------------------------------+
| |
| Commands: |
| +----------------------------------------------------------+ |
| | make menuconfig | Configure kernel | |
| | make -j$(nproc) | Compile | |
| | sudo make modules_install | Install modules | |
| | sudo make install | Install kernel | |
| | uname -r | Check kernel version | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+