How to recompile and customize your Ubuntu 23.10 kernel
This guide will show you how to recompile and customize your current Ubuntu 23.10 kernel. Reasons you may wish to do this include learning, changing kernel settings or just for fun. There are a number of different ways to do this, I like simple and easy to manage (see Debian kernel handbook chapter 4) and in this guide we will download the Ubuntu linux-source using apt install and compile the kernel as a *.deb package.
At the time of writing in Ubuntu 23.10, the current linux-source is 6.5.0. Updates may change that, therefore take this into account when reading this guide. Also as a side note, this guide assumes secure boot is disabled.
Before you begin
I strongly recommend changing the default grub boot behavior so that you have a grub menu on boot and can select your kernel. By default the grub boot menu is hidden. This will make it easy to boot into a working kernel should your custom kernel result in a system that fails to boot.
Using a text editor edit the default grub configuration file in /etc/default/grub
sudo gnome-text-editor /etc/default/grub
Change GRUB_TIMEOUT_STYLE=hidden to GRUB_TIMEOUT_STYLE=menu
Change GRUB_TIMEOUT=0 to GRUB_TIMEOUT=20
This will enable the grub boot menu and give you 20 seconds to choose a kernel you wish to boot from. Once you have changed those settings, save and close the editor.
Now you need to update your grub configuration.
sudo update-grub
Finally reboot your system to test the change, make sure it works.
Let's begin
Start by opening a terminal, I would recommend you stay in home directory.
Install required packages
sudo apt install build-essential libncurses-dev flex bison debhelper libssl-dev libelf-dev
Install your Ubuntu kernel source
sudo apt install linux-source
Extract the kernel source to your current directory
tar xfv /usr/src/linux-source-6.5.0.tar.bz2
When we installed the linux-source package it installed various files in /usr/src which is a location that requires root privileges. We do not want to be working as root or in this location. By extracting the linux-source files to our home directory (and you should be in your home directory) we can build the kernel without needing to be root.
Go into your linux-source directory
cd linux-source-6.5.0
Generate and config your kernel options
make menuconfig
This will bring up a console / text based configuration screen, allowing you to browse all the various sections and options.
Configure your kernel as you wish but before you save and exit it is important you delete the two security keys specified by Ubuntu, these being debian/canonical-certs.pem and debian/canonical-revoked-certs.pem your kernel build may fail if you do not.
These can be found under:
-*- Cryptographic API ---> Certificates for signature checking ---> (debian/canonical-certs.pem) Additional X.509 keys for default system keyring
-*- Cryptographic API ---> Certificates for signature checking ---> (debian/canonical-revoked-certs.pem) X.509 certificates to be preloaded into the system blacklist keyring
I also recommend disabling debug information as this will speed up the build and not build an additional debug kernel package. Unless it is something you need.
Kernel hacking ---> Compile-time checks and compiler options ---> Debug information (Disable debug information) ---> (X) Disable debug informationCompile your kernel *.deb package
Depending on your processor and kernel configuration options the compilation process may take some time. For the shortest possible compile time using concurrent threads (multi-threading) is recommended, the general rule of your processor count + 1 works very well. Please make sure your processor has efficient cooling, compiling the kernel is a CPU intensive task that will make your processor run hot and consume a lot of power. Therefore an inefficient cooling system and / or power supply may result in compile errors or a system crash.
time make -j $(($(nproc)+1)) EXTRAVERSION=-rade bindeb-pkg
This command will begin the process of compiling your kernel *.deb package with concurrent threads described above. It will also time how long it takes, useful if you wish to compare compile times. The EXTRAVERSION= string will append an additional identifier name to your kernel, useful to help you distinguish between multiple kernels and not to confuse it with others.
Alternatively you can omit this bit and use the config editor above instead to append a custom string to your kernel, just browse to:
General setup ---> () Local version - append to kernel release
Edit this setting and type anything you wish. In this guide we won't be editing this option. If you wish to specify the number of concurrent threads instead, use make -j X where X is a number. E.g. make -j 12.
Once complete your custom Ubuntu kernel package will be built and placed in a directory level one above from your linux-source directory, so if you are following this guide 100%, your current working path should be /home/username/linux-source-6.5.0. Your custom Ubuntu kernel package will therefore be in /home/username.
To quickly go back to your home directory type:
cd
Type dir to list files in your current directory:
dir
You should see files that are similar to the below (note how EXTRAVERSION=-rade affected the file name).
linux-headers-6.5.13-rade_6.5.13-rade-1_amd64.deb
linux-image-6.5.13-rade_6.5.13-rade-1_amd64.deb
linux-upstream_6.5.13-rade-1_amd64.buildinfo
linux-libc-dev_6.5.13-rade-1_amd64.deb
linux-upstream_6.5.13-rade-1_amd64.changes
As you can see there are three *.deb packages. A good explanation of these three files can be found in the Debian kernel handbook but to simplify this, the linux-image package is your customized Ubuntu kernel package and the linux-headers package is what is called header files, which are needed if you wish to build kernel modules for your custom kernel after it has been installed. An example would be if you wanted to install Nvidia GPU drivers. The other *.deb package, linux-libc-dev, provides Linux kernel headers for use by userspace programs,
Install your custom Ubuntu kernel *.deb packages
This next bit will require sudo.
sudo dpkg -i linux-image-6.5.13-rade_6.5.13-rade-1_amd64.deb
sudo dpkg -i linux-headers-6.5.13-rade_6.5.13-rade-1_amd64.deb
sudo dpkg -i linux-libc-dev_6.5.13-rade-1_amd64.deb
Once you have installed the packages, reboot your system, and if everything went well your custom kernel will boot.
It is worth noting installing the linux-image package will cause Ubuntu to automatically update your grub boot loader entries, adding your custom kernel. It may not necessarily default to booting into your custom kernel, names and version numbers will impact this. Having enabled the grub boot menu as described above, will allow you to verify this.
How to remove your custom kernel
In a terminal simply type the following:
sudo apt remove linux-image-6.5.13-rade
sudo apt remove linux-headers-6.5.13-rade
sudo apt remove linux-libc-dev_6.5.13-rade
Misc information
If you need to make your custom kernel boot by default, edit the grub configuration file as described above, change the GRUB_DEFAULT=0 option to GRUB_DEFAULT=X where X is the boot entry number of your custom kernel.
Hope you found this guide useful.
Comments
Post a Comment
If you enjoyed this article please let me know!