How do I apply a patch to my Linux kernel?

Your Linux distribution usually has its own instructions. Search their website or ask on IRC – or at least tell us the distro; without knowing it, it's impossible to provide a reliable anwer.

  • Arch Linux: https://wiki.archlinux.org/index.php/Kernels#Compilation
  • CentOS: http://wiki.centos.org/HowTos/Custom_Kernel
  • Debian: http://users.wowway.com/~zlinuxman/Kernel.htm, http://debian-handbook.info/browse/stable/sect.kernel-compilation.html, https://www.debian.org/releases/jessie/i386/ch08s06.html.en
  • Fedora: https://fedoraproject.org/wiki/Building_a_custom_kernel
  • Ubuntu: https://help.ubuntu.com/community/Kernel/Compile

The generic instructions are:

  1. Download the kernel source from Kernel.org. "Stable" is probably the best choice. Extract to a convenient place (I use ~/src/linux).

    • Read the file named README.
  2. Once inside the source directory, copy the current kernel's configuration, with:
    zcat /proc/config.gz > .config

    If /proc doesn't have it, look for /boot/config-[version] instead.

  3. Apply the patch, with:
    patch -p1 < foo.patch (try -p0 if it gets rejected).

  4. Compile the kernel with:
    make silentoldconfig
    make

  5. Install the modules with:
    sudo make modules_install

  6. Install the kernel image .../linux/arch/i386/boot/bzImage to wherever your bootloader wants it. (For example, /boot/vmlinuz-custom.)

  7. Build an initramfs for the new kernel, if your distribution uses it, and again make sure the bootloader knows its location. For example, on Arch Linux you would use:
    mkinitcpio -k /boot/vmlinuz-custom -g /boot/initramfs-custom