Compile a Kernel: How to do it on a Debian base Distro?
In our previous postCalled “The Linux Kernel: Kernel Basics” we address some essential theoretical bases on Operating System Kernels, usually; and the Linux kernel, specific.
And as we expressed in it, in this one, we will be completing said essential theoretical bases with others important concepts and information, in addition to showing the current procedure to achieve "compile a Linux Kernel" from scratch, on a Debian GNU/Linux 11 distribution (Bullseye) or based on it.
The Linux Kernel: Kernel Basics
And, before you start reading this post about “compile a kernel” Linux in general, we will leave some links to previous related posts for later reading:

Build a Kernel: Build Tutorial
What does it mean to compile a program?
Basically this technical process (also called packaging) consists in achieving conversion of the source code of a program or portion of software, from its source (programming language used to write itself) to a readable product (high-level programming language interpretable) by a computer.
That is, achieve transform from your source code until it becomes a executable and functional program, by using a processor (compiler software) for the conversion of the programming language used towards a code of binary and assembler type.
And when it comes to develop and compile any kind of package, application and program, basic and native, like the kernels about Debian GNU / Linux, the following packages are suitable and necessary to obtain a good development support base, and these are:
apt install autoconf automake autotools-dev build-essential dh-make debhelper debmake devscripts dpkg fakeroot file gfortran git gnupg fp-compiler lintian patch pbuilder perl python quilt xutils-dev
Advantages and disadvantages of compiling a Linux Kernel from scratch
When we custom compile and configure, from scratch, a specific Kernel for a certain computer, we can obtain the following advantages:
- Achieve better performance and lower CPU consumption.
- Obtain a better optimization and a lower consumption of RAM Memory.
- Improve the adaptability and compatibility of the operating system.
- Increase the efficiency and productivity of the operating system.
While, on the contrary, we could generate the following Disadvantages:
- Operational and availability failures of the desired resources and services on the computer used, due to bad configurations and problems caused during the compilation.
- A constant, long and tedious job at first to generate it, and then to later, to keep it updated manually. Which can go from minutes to hours, depending on the hardware used.
- Long hours of study and tests, since a high level of knowledge is needed both about the configuration options available in the Kernel and about the computer (final hardware), where it will be executed.

Available Kernel Categories
To compile a kernel, the first thing we need to know is which Kernel to choose. To do this, we must go to Official website of the kernels, and choose one among the existing categories. Which are the following:
- Main line of development (Mainline): This category includes those Kernels in the development stage, therefore, they contain new features and functions that must be tested before they can be put into a stable version. These are maintained and released directly by Linus Torvalds, and are released every 2-3 months on average.
- Stable: This category includes those Kernels that, after passing the tests of the long development process, become stable, so they are only subject to corrections through a designated official maintainer. Also, they only have a few bug fix releases until the next Mainline is available.
- Long term: This category includes those Kernels that, after passing the tests of the long development process, become stable, but are supported by bug fixes and maintenance for an extremely long time (years). Reason for which, important bug fixes are applied, which can become very frequent.

How to compile a Kernel in a GNU/Linux Debian Bullseye Distro?
Having chosen one specific kernel version, on the official website, and already knowing (copying) your download path by tarball button of the same, it only remains to carry out the following procedure, which we will show taking as an example, the Stable Linux kernel version 6.0.8:
Stage 1
cd /usr/src
wget -c https://mirrors.edge.kernel.org/pub/linux/kernel/v6.x/linux-6.0.8.tar.xz
sudo unxz linux-6.0.8.tar.xz
sudo tar xvf linux-6.0.8.tar
sudo ln -s linux-6.0.8 linux
cd /usr/src/linux
sudo make clean && make mrproper
sudo cp /boot/config-`uname -r`* .config
make menuconfig
Running this last command starts the "Kernel configuration menu", where you can configure (customize) parameters your preference or need. In addition, here it is essential not to forget that it is necessary check or uncheck the 64-bit kernel option, depending on what is desired or required. And also, after having made all the changes made, you must press the Save button And then the Exit button.

Stage 2
At this point there are 2 possible paths to take:
Kernel installation only
sudo make
sudo make modules_install
sudo make install
sudo update-grub; sudo update-grub2; sudo update-initramfs -u
sudo apt clean; sudo apt autoclean; sudo apt autoremove; sudo apt remove; sudo apt purge

Kernel installation and generation of .deb files
To execute this step, it is relevant to have the installation of the package called kernel-package. Therefore, the procedure is as follows and starts as follows:
sudo wget -c http://ftp.us.debian.org/debian/pool/main/k/kernel-package/kernel-package_13.018+nmu1~bpo9+1_all.deb
sudo apt install ./kernel-package_13.018+nmu1~bpo9+1_all.deb
fakeroot make-kpkg --initrd --append-to-version=-custom kernel_image kernel_headers
cd /usr/src
sudo dpkg -i *.deb
In case, during the compilation process, you get a error related to kernel certificates, it remains as an option to be able to execute the following command order to fix it automatically:
sed -i '/CONFIG_SYSTEM_TRUSTED_KEYS/s/^/#/g' .config
Yes, everything has ended well, all that remains is to restart our computer and test how our operating system runs with the new Kernel. As he is shown before and after, in the following screenshots:
Before Kernel Installation

After Kernel Installation


Summary
In summary, we hope that this tutorial will be very useful, both for Advanced users as Developers of Distros or Respins. which usually require “compile a kernel” specific on a currently installed GNU/Linux Operating System or one that is being generated from scratch, in order to incorporate a better performance and optimization on specific hardware. Mostly, with the aim of achieving, a lower CPU and RAM consumption.
Although, if anyone knows of any another useful way to carry out said procedure or know any suggestion, recommendation or correction to what is provided here, you are welcome to do so through the comments. And yes, you simply liked this publication, do not stop commenting on it and sharing it with others. Also, remember to visit our «homepage» to explore more news, as well as join our official channel of Telegram from DesdeLinux, West group for more information on today's topic.