Compiling and installing latest kernel from source on RHEL or CentOS 7

Compile and install latest kernel from source on RHEL or CentOS 7 OS ...


In this blog i will show you, how you can compile and install latest kernel from source on RHEL 7 Or CentOS 7 Operating system.

Check currently installed kernel version :

# uname -r
3.10.0-957.el7.x86_64

As you can see current kernel version is 3.10, but i want to upgrade it to the latest stable available version, which you can find on https://www.kernel.org/ official website. As of today current stable available version is "5.0.5"

To download this "5.0.5" kernel source, you can click either the big Yellow download button or you can click on the "tarball" link as shown in above screenshot. You can also download this version from below link:



Step 1 : Install build tools and other mandatory dependencies which are needed to compile and install new kernel from source.

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

# yum makecache

Install build tools and other needed packages :

# yum install make gcc bc openssl-devel ncurses-devel rpm-build elfutils-libelf-devel


Step 2 : Once these tools are installed, we will need latest kernel source code which we will download as shown below:


# ls -lrth | grep linux
-rw-r--r--. 1 root root 101M Mar 27 10:57 linux-5.0.5.tar.xz

Extarct "linux-5.0.5.tar.xz" compressed file:

# tar xvf linux-5.0.5.tar.xz

Once file is extracted you will see "linux-5.0.5" folder in the same directory:

# ls -alrt | grep linux | grep ^d
drwxrwxr-x. 25 root root      4096 Mar 31 15:44 linux-5.0.5


Step 3 : Generate a new ".config" file inside linux-5.0.5

# cd linux-5.0.5

Now we will run following command to list config files which are already in use with currently installed kernel:

# ls /boot/config*
/boot/config-3.10.0-957.el7.x86_64

Currently i have only one config file available on this system so we will use same:

# cp -v /boot/config-3.10.0-957.el7.x86_64 .config
‘/boot/config-3.10.0-957.el7.x86_64’ -> ‘.config’

Now we generate new .config file with following command:

# make menuconfig

Once you hit ENTER key, you will see this lagacy blue window on the terminal:



Here you can enable or disable certain kernel module or feature and then you can go ahead with compilation once done SAVE required configuration in the .config file :



Once configuration is written to the .config file and you press "Exit", it will again come to same window


This time select "Exit" and press ENTER key. This will bring you again to the shell prompt, where you can now go ahead and compile source code. 



Step 4 : The following command will compile source code and generate .rpm kernel packages. But before this please make sure that you should have alteast 20GB space available in "/" directory:

# df -kh | grep centos-root
/dev/mapper/centos-root   46G  6.8G   39G  15% /


# make rpm-pkg

OR instead of "make rpm-pkg" you can run below command :

# make -j `nproc` && make modules_install && make install  (Once done reboot server)

NOTE: This command will take long time depending upon the CPU and memory assign to particular Linux server.

Once "make rpm-pkg" command is completed you will notice that it has generated kernel/devel/headers rpm packages as shown below:


/root/rpmbuild/RPMS/x86_64/kernel-5.0.5-1.x86_64.rpm
/root/rpmbuild/RPMS/x86_64/kernel-headers-5.0.5-1.x86_64.rpm
/root/rpmbuild/RPMS/x86_64/kernel-devel-5.0.5-1.x86_64.rpm

As these rpm packages are ready, let's install these ...

# rpm -ivh /root/rpmbuild/RPMS/x86_64/kernel-headers-5.0.5-1.x86_64.rpm

# rpm -ivh /root/rpmbuild/RPMS/x86_64/kernel-devel-5.0.5-1.x86_64.rpm

# rpm -ivh /root/rpmbuild/RPMS/x86_64/kernel-5.0.5-1.x86_64.rpm

Once these rpm packages are installed, check if "kernel 5.0.5" is set to "0", i.e. for default boot-up.

# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux (5.0.5) 7 (Core)
1 : CentOS Linux (3.10.0-957.el7.x86_64) 7 (Core)
2 : CentOS Linux (0-rescue-53eb794d5d2944f9987b0ce3e421dd42) 7 (Core)


# grubby --set-default /boot/vmlinuz-5.0.5

# grubby --default-kernel
/boot/vmlinuz-5.0.5


Step 5 : Reboot your Linux server, so that newly installed kernel can load:

# reboot

Once server rebooted check for the running kernel version :

# uname -r
5.0.5



Comments

Popular posts from this blog

Recover or restore initramfs file in RHEL or CentOS 7

Space reclamation / UNMAP on RHEL or CentOS 7

How to recover /boot partition on RHEL or CentOS 7?