Linux Kernel: Building and configuring

The Linux kernel is the foundation of Unix-like operating systems. The kernel is responsible for the communication between hardware and software and for the allocation of available resources.


All Linux distributions are based on a predefined kernel. However, if you want to disable various options and drivers, or test experimental patches, you will need to compile a kernel.


This step-by-step guide shows you how to build and compile a Linux kernel from scratch.

linux kernel

Prerequisities

  • A system running Linux
  • Access to the terminal
  • A user account with sudo/root privileges
  • 12-15GB of available space on the hard drive

Building Linux Kernel

The process of building a kernel consists of some easy steps. However, the process takes a considerable amount of time depending on the speed of the system.


Follow the steps below to compile the latest Linux kernel at the time of writing.

1. Install the necessary packages to build Linux kernel

Install important packages before building a kernel. To do so, run this command on Debian/Ubuntu/pop-OS:

sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison
linux kernel

2. Downloading the source code

  1. Visit the official kernel website and download the latest version of the kernel. The downloaded file contains a compressed source code.

2. Open Terminal and use wget command to download kernel source code:

wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.9.6.tar.xz
linux kernel

3. Extracting the kernel source code

In the folder you downloaded the file in, enter this command to extract the tar file

tar xvf linux-5.9.6.tar.xz
build linux kernel

4. Configuring the Linux kernel code

The Linux kernel source code comes with the standard configuration. However, you can customize it to suit your needs. To do this, follow these steps:

  1. Navigate to linux5.9.6. Directory with the command cd:
cd linux-5.9.6

2. Copy the existing configuration file with the command cp:

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

3. To make changes to the configuration file, run the make command:

make menuconfig

4. The configuration menu contains options such as firmware, file system, network settings, and storage. Use the arrows to make a selection or select HELP for more information on the options. When you have finished making changes, select SAVE and then exit the menu.

configure
Note: Changing the settings of some options may cause the kernel to not work. If you are not sure what to change, leave the default settings.

5. Building the kernel

  1. Start compiling the kernel by running the following command:
make
  • Compiling and compiling the Linux kernel will take some time

  • The terminal lists all the Linux kernel components: memory management, hardware device drivers, file system drivers, network drivers, and process management.
  • build

    2. Install the necessary modules.

    sudo make modules_install
    
    module

    3. Now we will finally install the kernel

    sudo make install 
    
    make

    6. Update the bootloader

    The GRUB boot loader is the first program to run when the system is on.


    The make install command does this for you, but you can also do it manually.

    1. Update initramfs to the installed kernel version:
    sudo update-initramfs -c -k 5.9.6
    

    2. Update grub to list the kernel in boot menu

    sudo update-grub
    
    initramfs

    7. Reboot and verify the kernel

    When the system boots up, verify the kernel version using the uname command:

    uname -mrs
    uname

    Conclusion

    In this step-by-step guide, you learned how to build a kernel from scratch and install the required packages.
    If you follow the instructions carefully, the process will be completed successfully on your Linux computer.


    The Linux kernel has a modular structure. The functionality can be expanded with modules or controllers. Learn how to use the modprobe command to add or remove modules in Linux.

    Leave a Comment