Sunday, March 31, 2019

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

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 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*

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:


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

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

Saturday, March 30, 2019

Recovering or restoring grub in CentOS or RHEL 7

What if GRUB is corrupted on your RHEL or CentOS 7 ?

GRUB corruption is a common problem in RHEL or CentOS, which could occur due these reasons:
  1. If any file is missing or deleted from "/boot/grub2/" folder e.g. "grub.cfg"
  2. Mistakenly ran "dd" utility to clear boot sector
  3. Due to bad sector or corrupted disk
  4. Bad or incompatible firmware/driver installation  e.t.c.
In case GRUB is corrupted, usually after powering on your server you see below kind of shell, also known as GRUB shell:

Once you get to this prompt there is a way to restore or recover GRUB so that your system can boot up properly. To recover GRUB we need to have RHEL or CentOS 7 ISO image, which we usually use for OS installation. Make sure you mount same OS version iso image which is already installed on system. Below procedure will work only on BIOS based X86 servers and virtual machines.

Follow these steps to recover GRUB:

Step 1 : Mount RHEL or CentOS 7 ISO image on your physical server and boot from it. In case you are using HPE Prolient server you can mount this ISO image on iLO, if this is virtual environment then mount it accordingly and reboot server or VM from ISO. Once rebooted you will see below options:

Step 2 : Scroll down and select "Troubleshooting" option from menu and PRESS "Enter"

Step 3 : Select "Rescue a CentOS system" and press ENTER:

Step 4 : Press ENTER key again to continue:

Step 5 : To continue, Type "1" and press ENTER, this will bring you to rescue mode, chroot sysimage filesystem using below command:

# chroot /mnt/sysimage

Step 6 : Next step is to install GRUB2 on the boot disk this will be usually "/dev/sda":

# grub2-install /dev/sda

On this step you should not see any error. In case "grub2-install" command is not available then you will have to install it through "grub2-tools" package which can be found in ISO image.

Step 7 : Once GRUB2 is installed, next step is to generate "grub.cfg" in "/boot/grub2" directory. 

# grub2-mkconfig -o /boot/grub2/grub.cfg

File is generated, you can check as shown in above screenshot.

Once done, exit from the chroot and again exit to reboot the server.

Friday, March 29, 2019

Recovering root user password in CentOS or RHEL 7

What if you forget root user password? 

RHEL/CentOS 7 has a way to recover root user password, but this will need downtime of your linux host.

To recover your root user password follow below procedure as shown in screenshots:

Step 1 :  Reboot host, come to GRUB screen and select Kernel without "rescue" option:


Step 2 :  Scroll your cursor down till you see line starting from "linux16".  Come  to the end of line after "UTF-8" give a space and write "rd.break":

PRESS "CTRL + x", this will interrupt the boot process and leave you to the shell prompt :

Step 3 : Once you come to shell prompt, remount "/sysroot" with "rw" permission i.e. read/write and then chroot to "/sysroot" directory:

# mount -o remount,rw /sysroot/
# chroot /sysroot/

Step 4 :  Now we can go ahead and reset "root" user password using "passwd" command:

Step 5 : Once new root password is set and SELinux is ENABLED then create "/.autorelabel" file using touch command, so that it will relabel shadow file:

# touch /.autorelabel

Press "Exit"
Press "Exit"

This will reboot system and once it comes up, you would be able to login as root user with new password.