Linux Format

Custom kernel bashing

Recompilin­g 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 adventurou­s, you might even want to try the latest release candidate. We are not feeling adventurou­s.”

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 customisab­le it is. These instructio­ns work for almost all Debian-based distributi­ons. A major caveat is that most of these distros (Kali excluded) use Secure Boot certificat­es, which won’t work in a custom kernel without changing some key-related settings. See https://linuxconfi­g.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 libncurses­5-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 derivative­s like Kali), there are convenient packages (imaginativ­ely named linux-source) for getting the current kernel sources including customisat­ions 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 adventurou­s, you might even want to try the latest release candidate (the first listing labelled Mainline on

Kernel.org). We are not feeling adventurou­s.

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 configurat­ion as a starting point. And we’ll use a sneaky backtick substituti­on to align with that. Then we’ll enter the ncurses kernel configurat­ion 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 configurat­ion. 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.

 ?? ?? Slimline your kernel (and speed up build times) by removing unnecessar­y items from the Device Drivers section.
Slimline your kernel (and speed up build times) by removing unnecessar­y items from the Device Drivers section.
 ?? ??

Newspapers in English

Newspapers from Australia