Open In App

How to Compile Linux Kernel on CentOS 7

Last Updated : 24 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Most Linux distributions have a generic pre-built kernel by default, which might not have been specifically optimized for your hardware or use. Compiling a custom Linux kernel lets you enable or disable features, add new modules, optimize the system performance, and resolve compatibility issues—something that is useful for advanced users, programmers, and system administrators.

Here we understand how to compile and install the Linux kernel in Linux systems. From installing dependencies required to kernel configuration and building from source, everything is explained in easy, beginner-friendly language. This guide will also assist in enhancing your knowledge of the Linux kernel architecture.

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
Checking Kernel Version
 

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 ncurses library
Installing Requirements
Installaion Done Successfully
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 wgetcommand 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
Getting latest Kernel version using wget
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
Extracting package
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*
Viewing configuration files
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
Viewing Contents of kernel config file
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 bringup a kernel configuration menu where we can select the options we want to enable or disable on the Linux kernel.

Opening Configuration
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.

Saving Kernal config file
Kernel Configuration Menu

Compiling the Kernel 

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

make rpm-pkg
Compile kernel into rpm package
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.

CPU history info
System Monitor CentOS7

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

Succesfull Compilation
Kernel Compiled Successfully

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

How to Enable the Compiled Linux Kernel for Boot

After you’ve successfully compiled and installed your custom Linux kernel, the next step is to make sure your system can boot with it. Here's how to do it in a few simple steps:

Step 1: Generate Initramfs for the New Kernel

The initramfs is a temporary file system that your system needs during boot. Run the command below to create it:

sudo update-initramfs -c -k 6.0.9

Step 2: Update the GRUB Bootloader

GRUB is the bootloader that shows up when you start your computer and lets you choose which kernel to boot off of. Update GRUB so it will see the new kernel after you have created the initramfs:

sudo update-grub

Step 3: Reboot and Boot into Your New Kernel

Reboot your system now that GRUB has been updated:

sudo reboot

Conclusion

Compiling the Linux kernel can be rough at first, but once you're familiar with the process and tools used, it's a huge way of fine-tuning and customizing your Linux system. If you need to tweak performance, enable special hardware drivers, or observe how the system boots up and runs—having your own kernel gives you more control and understanding.

By following this article, you now know how to download the newest kernel source, configure it according to your existing setup, compile it, and install it correctly. Just remember: always double-check your configuration, use sufficient disk space, and back up critical files before making system modifications.


Next Article

Similar Reads