The post title begs the question: Why should you customize the Linux kernel? There are three main reasons why you might want to do this:
- Performance: by removing unnecessary features and by tuning the kernel to work on your particular machine, you could make your machine more performant;
- Patching: rather than waiting for an official updated of the kernel, you can apply any patches you are interested in anytime;
- Additional features: not everything is compiled into the kernel by default, mostly to keep it as lean as possible. If there is something you really use, however, you can add it to your kernel so the features are built in.
Perhaps surprisingly, building your own Linux kernel is far from a complicated thing. You just need a lot of patience as the build process can last quite a bit. Having said this, let’s take a look at how you can customize your Linux kernel.
Something you need to understand before you begin
Do not do this on your main Linux machine if you are not comfortable with the process, do it on a test box first. While you can normally use Grub to choose another kernel version if your machine doesn’t boot with the customized one, playing with the kernel could potentially leave you with a non-working machine.
I am not responsible in any way to any damage that might come from you customizing your own Linux kernel, simply because I have no idea what you actually end up doing. Virtual machines are your best friend here.
For this tutorial, I have used a brand new Ubuntu 17.10 distro, but the instructions should work just as well even for older Ubuntu versions (or any other Ubuntu derivative, for that matter).
After you have your system installed, you will need a couple of packages in order to compile the kernel later on:
sudo apt-get update sudo apt-get install build-essential libssl-dev
Customizing the Linux kernel
After you have downloaded the tar.xz archive from the official Linux kernel webpage, you are ready to configure it. First of all, let’s change to /usr/src (Linux headers are found here, so it will be better to do all the work here so the build process will be able to find all it needs):
cd /usr/src sudo tar xvf linux-4.15.7.tar.xz cd linux-4.15.7 sudo make menuconfig
menuconfig is a utility that helps you navigate all the kernel options and enable/disable them as you wish. The advantage of menuconfig is that it is text-based and therefore can be run even on machines without X installed (unlike other options to customize the kernel such as xconfig or gconfig).
After running the last command you will see this window:
Now it is just a matter of exploring the options and choosing how to make the kernel your own. Do not enable everything though, or the kernel would become too huge. It’s better to include just the fundamental things and add everything else as modules (which are external files, which will be loaded when requested rather than being loaded by default).
The default configuration file is found under /boot, and menuconfig (or any other utility like this) will use that configuration file as the basis for your configuration. This is so you have a lower chance of enabling features that might not work on your particular machine.
As a quick test for the sake of this tutorial, let’s change the processor family so it’s optimized for the latest Intel CPUs (which is very likely what you are running right now). To do this, navigate to the Processor type and features menu:
Here, look for the Processor family entry and choose Core 2/newer Xeon option to build the kernel so that it is specifically tuned for this family of processors:
When you are done, select Save (you can get there by using the Tab key) and save the .config file when prompted.
Building the Linux kernel
This is where things can get boring. Building a Linux kernel can take a long time (even hours inside a virtual machine, although on bare metal it shouldn’t take more than an hour on a reasonably performant machine).
Also, remember that, while the final, compiled kernel will not be very big in size (in this test, just 8 MBs for the resulting vmlinuz file, and 528 MBs for the uncompressed image), the compiled files will be huge: the build process used up 17,7 GBs in this test, so definitely make sure you have a bit of hard drive space left for this.
Luckily, you can make the build process faster by telling it to use all your available CPU cores. For example, on a quad-core machine, run:
sudo make -j 4
Now go grab several coffees.
Installing the new Linux kernel
If the build process completed successfully, you are now ready to install your new Linux kernel so that you will be able to select it the next time you boot your machine.
To install the kernel and modules in the correct directories, run the following commands:
sudo make modules_install sudo make install
The first command copies kernel modules in /lib/modules/kernelversion. The second command copies the kernel and support files in /boot: vmlinuz-version, which is the compressed kernel image, which will be executed by the bootloader grub.
Speaking of grub, there is no need to do anything manually on that front: sudo make install also updates grub (you can check the updated configuration in /boot/grub/grub.cfg).
Using your new kernel
You are now ready to use the new kernel! When you reboot your machine, select Advanced options for Ubuntu in the grub menu:
Then select the kernel version you have just installed:
You will see that your machine will start booting the new kernel:
In this post, I have explained how to customize, build and install your own version of the Linux kernel. It’s not a difficult process at all, it just takes some patience for the build process to finish, quite a bit of hard drive space, and knowledge of the kernel options if you feel adventurous and want to try playing with some of the settings.
There are several benefits to using your own Linux kernel, the main ones being increased performance, quicker patching and additional features. Whatever your reason is, however, I recommend doing this as it’s a fun process.
What are your favourite kernel customizations? Feel free to let me know in the comments below so I can build a new kernel of my own ;)