Linux Kernel

Compile Linux Kernel on CentOS7

Image

Compile the Latest Linux Kernel from Source on CentOS 7

In this article I will show you how to download the latest Linux kernel source from the official website of Linux kernel, compile Linux kernel from source and use the compiled kernel on CentOS 7. Let’s get started.

Checking Currently Used Kernel:

You can see from the following screenshot that I am using CentOS 7.

Image

And the current kernel version is 3.10

Image

Installing the prerequisites:

To compile the latest Linux kernel from source on CentOS 7, you must have a build tool and some other packages installed on your CentOS 7 operating system.

Before you install anything, run the following command to update package cache:

$ sudo yum makecache

Image

Now you can install the compilers and libraries required for compiling the kernel with the following command:

$ sudo yum install ncurses-devel make gcc bc openssl-devel

Image

Press ‘y’ and then press <Enter> to continue.

Image

The build tools should be installed.

Image

Now you have to install elfutils package. Run the following command to install it:

$ sudo yum install elfutils-libelf-devel

Image

Press ‘y’ and press <Enter> to continue.

Image

‘elfutils’ should be installed.

Image

Now you have to install rpm-build with the following command:

$ sudo yum install rpm-build

Image

Press ‘y’ and then press <Enter> to continue.

Image

‘rpm-build’ package should be installed.

Image

Downloading the Linux Kernel Source:

Go to the official website of Linux Kernel at https://www.kernel.org and you should be the following page.

Image

Click on the “Latest Stable Kernel” button as marked on the screenshot below.

Image

Your browser should prompt you to save the file. Just click on “Save File” and click on “OK”.

Image

Your download should begin.

Image


Compiling the Kernel:

Once the download is complete, navigate to the directory where you downloaded the file. In my case it is the Downloads directory in my user’s HOME directory.

$ cd ~/Downloads

From the output of ‘ls’ you can see that the downloaded file is ‘linux-4.14.10.tar.xz’. Which is a compressed tar file.

Image

Now extract the compressed tar file with the following command:

$ tar xvf linux-4.14.10.tar.xz

Image

tar is extracting the compressed file.

Image

Once the file is extracted, you should see the following window.

Image

After extraction, you should see a new directory as marked red in the screenshot below. Navigate to the directory with the following command.

$ cd linux-4.14.10

Image

If you run the following command, you should see a list of config files used by the kernels that are installed on your system. You can run ‘uname -r’ command to find the one that you need. The filename should match with the output of the ‘uname -r’ command.

Image

Then copy the config file to the linux-4.14.10 directory with the following command:

$ sudo cp -v /boot/config-3.10.0-693.el7.x86_64 .config

Image

Now run the following command:

$ make menuconfig

Image

You should see the following window. This is where you enable or disable certain kernel features. If you don’t know what to do here, then just leave the defaults.

Image

Once you’re done, press <Left Arrow> button several times and go to “< Save >” as shown in the screenshot below. Then press <Enter>.

Image

Then press <Enter> again.

Image

Press <Enter> again.

Image

Now navigate to “< Exit >” and press <Enter>

Image

The .config file is updated for the new kernel.

Image

Before you start compiling the new kernels, make sure you have more than 20GB of free space on the filesystem where you’re compiling the kernel.

You can check how much space you have available with the following command:

$ df -h

Image

 

Now run the following command to start the compilation process:

$ make rpm-pkg

Image

The kernel should be compiling just fine. It should take a long time.

Image

On completion, you should see the following window. Some rpm package file was created on the user’s home directory as you can see from the screenshot.

Image

The generated rpm package files.

Image

Now you can run the following command to install the rpm packages:

$ sudo rpm -iUv ~/rpmbuild/RPMS/x86_64/*.rpm

Image

Once the installation is complete, run the following command to restart your computer.

$ reboot

Image

Once your computer starts, you can run the following command to check the version of the kernel that you’re currently using.

$ uname -r

You should see that it’s the version that you just installed. For me, it is ‘4.14.10’.

Image

So that’s how you compile the latest kernel from source and use it on CentOS 7. Thanks for reading this article.

About the author

Image

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.