This post records the procedure to change the kernel for Linux. The main processes are downloading source, configuring, building, installing and booting.

1. Install a default kernel

1.1 Download the source code

You can find the required version of Linux kernel from kernel/git/stable/linux.git - Linux kernel stable tree. And we could clone it, we can specify the version:

1
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git --depth 1 --branch v6.1.4

1.2 Configuration

There are different ways to do this configuration:

1
2
3
make config # The most basic traditional configuration interface
make menuconfig # Configuration interface based on text menu
make xconfig # Configuration interface based on graphical window mode

No matter which kind of methods you choose to do the configure, they are just used to generate the .config file for the following building. I choose the .make menuconfig, it will look like:

1-configuration

There will have three choices for each function list in the above menu:

built-in: compile the related function in the kernel image, this choice could add functions into the kernel image

excluded: do not compile the related function in the kernel image

module: M compile the related functions in the modules, and these modules could be dynamically inserted into the kernel when needed.

Users can customize the kernel on demand. But now we do not change anything, we’ll install a default kernel. Just exiting the configure interface.

1.3 Build the kernel and modules

  • build the kernel
1
2
make -j 4 # Allocate more resources to speed up this process
make -j $(nproc) # Allocate as many resources as possible

This process is really slow, it may costs a pretty long time. So you could use the nohup or related instructions to avoid the interruption of process.

You may meet some errors related to missing of dependency, and you must solve them at first.

Besides, you may meet the problem like:

2-buildProblem

They could be solved through Compiling the kernel 5.11.11 - Ask Ubuntu:

1
2
scripts/config --disable SYSTEM_TRUSTED_KEYS
scripts/config --disable SYSTEM_REVOCATION_KEYS
  • build the modules
1
make modules_install
  • install the new kernel
1
make install

The installed kernel image could be find in ./arch/x86/boot/.

1.4 Boot into new kernel

The new kernel has been installed, but there are still some processes to boot into this new kernel. The bootloader should be seted up before rebooting.

The file /boot/grub/grub.cfg will decider the order of OS booting, but it’s just a automatically generated file. Just changing the settings file /etc/default/grub, and using the instruction to regenerate the grub.cfg file.

The changed settings in /etc/default/grub, GRUB_DEFAULT will determine the default kernel:

4-grub

Then, using the following instruction to regenerate grub.cfg:

1
sudo update-grub

1.5 Reboot

1
sudo reboot

After rebooting, checking the version of our kernel:

1
uname -a

5-linuxVersion

2. The method to deal with crash

It’s not unusual to build a kernel which can not be started normally, especially for the beginner. If you try to install it at your physical machine directly, it may cause crash. So it’s recommended to try your customised kernel in the virtual machine Oracle VM VirtualBox or QEMU.

And for the headless Virtual Box, you can set it as Using the headless Virtual Box | HDY blog (hudongyue.com).