Custom kernel bashing
Recompiling the kernel is a rite of Linux passage. So, let’s install a bleeding-edge kernel or otherwise customise the heart of your distro.
ADVENTURE GAMES “If you’re feeling adventurous, you might even want to try the latest release candidate. We are not feeling adventurous.”
One of the advantages of having Kali Linux installed (as opposed to running it through live media) is that it’s much easier to compile a custom kernel. Unless you’re an advanced user, there’s no practical reason you should be doing this, but it’s a great way to gain insight into how the Linux kernel fits together and how customisable it is. These instructions work for almost all Debian-based distributions. A major caveat is that most of these distros (Kali excluded) use Secure Boot certificates, which won’t work in a custom kernel without changing some key-related settings. See https://linuxconfig.org/building-kernels-thedebian-way for a more thorough guide.
We need some tooling before we can build our kernel. Fetch this (although most of it should already be installed) with:
$ sudo apt install -y build-essential libncurses5-dev
fakeroot bc bison libelf-dev libssl-dev xz-utils
At the time of writing, Kali (itself based on Debian Testing) is based on version 6.5.6 of the Linux kernel. Check if that’s changed by running uname -a , paying attention to the second Debian version string, rather than the first Linux Kali one. On Debian (and derivatives like Kali), there are convenient packages (imaginatively named linux-source) for getting the current kernel sources including customisations and patches, but we’ll use a different approach here. You can see exactly what Kali injects into the vanilla kernel by studying the project’s GitLab (https://gitlab.com/kalilinux/packages /linux/-/blob/kali/master/debian/patches/series)
Some users are fans of running mainline kernels – those built from official sources at https://kernel.org – without any distro-specific patches. There’s a repo for this on Debian (and Ubuntu) systems, which you can find (with the requisite info) at https://github.com/ zabbly/linux. Ubuntu also provides the mainline PPA for this. On Debian Stable (which currently uses Kernel 6.1), and to a lesser extent on Ubuntu, building from mainline (or using someone else’s mainline build) lets you run a much newer kernel than the distro provides.
So, let’s update Kali to the latest mainline release. As we write this, the latest stable kernel is 6.7.4. As you read this, these numbers will be bigger, so check
https://kernel.org and feel free to embiggen the numbers in the code below. If you’re feeling adventurous, you might even want to try the latest release candidate (the first listing labelled Mainline on
Kernel.org). We are not feeling adventurous.
Manually download the kernel sources to a sensible place in your home directory with:
$ mkdir ~/linux-source
$ cd ~/linux-source
$ wget https://cdn.kernel.org/pub/linux/kernel/
v6.x/linux-6.7.tar.xz
$ wget https://cdn.kernel.org/pub/linux/kernel/
v6.x/patch-6.7.4.xz
Now unpack the kernel and apply the patch:
$ tar xvf linux-6.7.tar.xz
$ cd linux-6.7/
$ xzcat ../patch-6.7.4.xz | patch -p1
We’ll use the currently running Kali configuration as a starting point. And we’ll use a sneaky backtick substitution to align with that. Then we’ll enter the ncurses kernel configuration area:
$ cp /boot/config-`uname -r` .config
$ make menuconfig
Use the cursor and Tab keys to navigate this labyrinth. You can (and we have) spend days prodding various settings in here, but there’s no easy guide to optimising your custom kernel. We’re also very short on space, so let’s just compile using our current configuration. Exit and say yes to save the new .config file. Now build the thing, install the build modules and copy the freshly minted kernel image to the boot directory with:
$ make -j3
$ make modules_install
$ make install
The first command in particular takes a long time to run (the -j parameter tells it how many threads to use, so increase this if you have a massively multicore system), so now is a good time to make a cuppa.