How to Compile Linux Kernel on CentOS 7

The kernel is the core of a computer’s operating system that has complete control over the system and provides basic services for all other parts. The default kernel available in any distribution cannot be customized. We cannot enable or disable any feature on it. Running a custom-compiled Linux Kernel becomes very helpful in cases when the hardware does not support the standard kernel.

Kernel Compilation and Installation 

We are currently running the default kernel supplied by the distribution. You can check the kernel version by typing the following command.

uname -r

 

Requirements before Kernel Compilation

Step 1: For compiling the kernel first we need to update our software package repositories and install the development tools and the ncurses library. Type the command below to proceed.

yum update -y
yum install -y ncurses-devel make gcc bc bison flex elfutils-libelf-devel openssl-devel grub

Installing Requirements

Requirements Installed

Step 2: Now we have the required packages for kernel compilation. We need to install the latest stable version of the Kernel (v.6.0.9) from the source using the wget command under the  /usr/src/ directory. Type the commands below to start downloading the latest kernel.

cd /usr/src/
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.0.9.tar.xz

Downloading the Stable Kernel Version

Step 3: Now extract the downloaded tarball file and then move to the main Linux repository.

tar -xvf linux-6.0.9.tar.xz
cd linux-6.0.9

After Extracting Tarball

After moving into the main Linux repository we need want to copy our existing kernel configuration into this directory. The configuration file of the current kernel will be inside the boot directory. Type the command below to see the configuration files.

ls -alh /boot/config*

Configuration Files in Boot Directory

To see the contents of the kernel configuration file type the following commands.

less /boot/config/-3.10.0-1160.76.1.el7.x86_64

Contents of Kernel Configuration file

Step 4: Now for copying the existing kernel configuration file type the following.

cp -v /boot/config/-3.10.0-1160.76.1.el7.x86_64 .config

Step 5: Now the next step is to configure the options in our kernel before compilation and we can do that by running.

make menuconfig

This will bring up a kernel configuration menu where we can select the options we want to enable or disable on the Linux kernel.

Kernel Configuration Menu

Note: Be careful while configuring the option, if you choose any configuration that’s not supported by your hardware then you might  run into something called Kernel Panic. An internal fatal error from which the system cannot safely recover.

Step 6: Now once you completed it, choose Exit and then Ok to save the configuration. The .config file is updated for the new kernel.

Kernel Configuration Menu

Compiling the Kernel 

Now the next step is to compile the kernel into an rpm package. Type the command below to start the compilation.

make rpm-pkg

Compiling the Kernel into RPM Package

Note : Before starting kernel compilation, make sure your system has more than 25GB of free disk space. To check the disk space type df -h”.

This process can take a while depending on the specification of your server. We can see from the screenshot below that the process is consuming CPU.

System Monitor CentOS7

The output screen when the compilation is over will look like this:

Kernel Compiled Successfully

Once the compilation completes, install the compiled Kernel using the yum package manager and then reboot the system.



Contact Us