How to Compile the Linux Kernel

by david-0609 in Circuits > Computers

1175 Views, 5 Favorites, 0 Comments

How to Compile the Linux Kernel

Final Design - Copy-min.png

Knowing how to compile the Linux kernel is a key step to understand the inner workings of Linux. Compiling the kernel can bring various benefits such as personalisation and performance advantages on certain hardware. A self compiled kernel allows the user to install versions of the kernel that are not necessarily supported by their distribution or in the distro’s repository. This Instructable will outline the simple steps needed to compile and install your very own kernel.

I have also made a video on this topic, which is attached to this instructable on step 1.

Supplies

  • Working Computer running Linux w/ preferably GNU Coreutils
  • sudo access
  • Internet Access
  • Stable power supply
  • A few hours of time

Watch the Video

How to Compile the Linux Kernel 2021

Watch the video to see the full process of compiling the kernel!

Downloading the Kernel

2021-11-06_11-46.png
2021-11-09_12-18.png

The source code of each release of the Linux kernel is uploaded to kernel.org, and dev versions of the kernel are also on GitHub at

https://github.com/torvalds/linux/tags

Only download source codes and binaries from a trusted source, as kernel level malware is near impossible to detect and grants an attacker top-level access as a backdoor.

There are many versions of the kernel available to download, there are the mainline, stable and longterm releases. Most distros use the stable release, which contains both new features and stability. Longterm kernels are more targeted to servers that require high stability and do not need as many new features, meanwhile avoiding the necessity of updating the kernel frequently. Linux-next is the dev version of the kernel, which is only targeted to kernel developers. All long-term release kernels have an End of Life, which is the time when security updates and support would end for the kernel. The EoL schedule is attached to this step.

The kernel can be downloaded using rsync, git and normal http through the browser and wget. The kernel is approximately 100 MB, make sure the internet connection is stable to successfully download the kernel.

Extracting the Kernel

2021-11-08_17-16.png

The kernel is stored in a tarball. Meaning, you would have to extract the kernel before continuing. This can be done in various ways

  1. Using the tar command
  2. Using a graphical tool e.g. Xarchiver

The tar command can be quite complicated, but with a quick look at the manpage, the command is:

tar -xvf linux.tar.xz

With -x meaning extract, -v meaning verbose mode and -f specifying the file.

Once the kernel is extracted, use the cd command to change into the kernel directory.

Installing Dependencies

There are some dependencies to install in order to compile the kernel. I recommend using a distro with GNU Coreutils and GCC in order to prevent any potential problems. Here are the dependencies needed:

git 
fakeroot
build-essential
ncurses-dev
xz-utils
libssl-dev
bc
flex
libelf-dev
bison

Some distributions name these packages differently, consult your distro’s documentation first. Install the packages with a package manager such as apt, pacman and dnf. The package ncurses is needed for the graphic configuration menu in the next step.

Configuring the Kernel

2021-11-06_11-49.png

Configuring the kernel can be a long process, depending on the complexity of the configuration. For brevity and generalisation, this Instructable will use a pre-existing config file of the current kernel, which will be copied using the command once in the root directory of the kernel:

cp /boot/config-$(uname -r) .config

The kernel can be configured in a ncurses interface with the command:

make menuconfig

There are various options in the config menu, such as driver options and networking configurations. For example, Bluetooth can be disabled in the option configuring Networking Options, as many do for security concerns.

To navigate around the config menu, use arrow keys to select and to enter a specific menu, to exit a menu, press 2 times. Instructions on the controls are clearly documented on the top of the menu. Configuring the kernel can take a long time and a lot of effort, however, unless there is a specific purpose, I do not recommend spending too much time configuring the kernel as some configurations may cause problems, therefore it is important to be highly vigilant on the settings, as well as consulting documentation of the options beforehand.

For the compile to work, these 3 options in the config file (.config) that needs to be commented out:

CONFIG_SYSTEM_TRUSTED_KEY
CONFIG_MODULE_SIG_KEY
CONFIG_DEBUG_SECTION_MISMATCH

If any confirmation was asked for in the compilation process concerning these variables, always select the defaultoption if in doubt.

Compiling the Kernel

To compile the kernel, the “make” command is used. Compiling the kernel can take quite a long time depending on the setup. To maximise the efficiency, specify the number of threads with the “-j” flag. The rule of thumb for this flag is the number of cores of the CPU, time 2 and plus 1, as seen in the formula below:

Number of CPU Cores*2+1
Example for 8 cores:
make -j 17

If make throws an error relating to access being denied, use the sudo command to run the command again as the Superuser. During this process, some questions may be asked about the signature key, when prompted, press enter to select the default method. On a virtual machine with 8 GB of RAM and 4 cores, it would take approximately 3 hours to compile. During this time, go outside, get a drink or read a book. The key is that the computer should not be used during the compilation process, make sure a power supply is plugged in and for devices with overheating problems, set the computer a few centimetres above the table for better ventilation.

Installing the Kernel

2021-11-06_12-45.png
2021-11-06_12-47.png

If everything worked out, we can now install the kernel. The command to install the kernel, alongside with the modules, is:

sudo make INSTALL_MOD_STRIP=1 modules_install && sudo make install

This command is going to take a few minutes. The command will also add the compiled kernel to the GRUB bootloader, allowing it to be booted into at the next reboot, as seen in the picture on the right, where the kernel version that was just installed shows up on the GRUB menu. The system should boot without problems and into the desktop.

Once into the desktop, open a terminal and execute the command.

uname -a

The name of the new kernel should be shown on the terminal, meaning the kernel compile was successful.