Exploring Linux Kernels
Tags: linux, bash, kernels
Published: 2023-02-23 14:22:45

## Introduction

> A Kernel is the lowest level of software that can interface with the computer hardware.

The Linux kernel is the core component of the Linux operating system. It is responsible for managing system resources such as CPU, memory, and input/output devices, as well as providing services to other software running on the system.

As with any software, the Linux kernel requires regular updates to fix bugs, address security vulnerabilities, and introduce new features and functionality. 

However, updating the kernel can be a complex process, and there are several different approaches to managing kernel updates depending on your needs and level of expertise.

✨ In this blog post, we'll explore some of the best practices for managing Linux kernel version updates, including how to install and maintain different versions of the kernel, and how to ensure that your system remains secure and stable after an update.

---

## Checking the Kernel Version 

- To check current kernel version information
    
   
    ➜  ~ uname -sr
    Linux 3.10.0-1160.81.1.el7.x86_64
   
    

- To find the latest kernel for Linux we can navigate to “[https://www.kernel.org](https://www.kernel.org/)“
 


![Kernle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72lme7bqbgvc9j77gugs.png)

---

## Manually updating the Kernel


> 👉 Before we carrying on with this practical, we need to create a new 30GB HDD for our VM



- Let’s create a new partition for this new HDD that we added.
    
    ```bash
    ➜  ~ fdisk /dev/sdb
    .
    .
    .
    Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048    62914559    31456256   83  Linux
    ```
    

- Formatting and adding auto mount to this partition.
    
    ```bash
    ➜  ~ mkfs.xfs /dev/sdb1
    meta-data=/dev/sdb1              isize=512    agcount=4, agsize=1966016 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0, sparse=0
    data     =                       bsize=4096   blocks=7864064, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal log           bsize=4096   blocks=3839, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    
    ➜  ~ vi /etc/fstab
    # /etc/fstab
    # Created by anaconda on Tue Jan 10 11:36:10 2023
    #
    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    #
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=23c31983-af1e-48ed-8d0a-ce25c13dd641 /boot                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    
    /dev/sdb1       /usr/src                xfs     defaults        0 0
    ```
    
- Creating a new directory under root and mounting our new partition in it.
    
    ```bash
    ➜  ~ mkdir /test
    ➜  ~ mount /dev/sdb1 /test
    ```
    

- Moving the `/usr/src` contents to `/test` while our partition is mounted in it.
    
    ```bash
    ➜  ~ mv /usr/src/* /test
    ➜  ~ ls -l /test
    total 0
    drwxr-xr-x. 2 root root 6 Apr 11  2018 debug
    drwxr-xr-x. 2 root root 6 Apr 11  2018 kernels
    ```
    
- Unmounting our partition from `/test` and mounting it in `/usr/src`
    
    ```bash
    ➜  ~ umount /test
    ➜  ~ rm -rf /test
    ➜  ~ mount /dev/sdb1 /usr/src
    ➜  ~ ls -l /usr/src
    total 0
    drwxr-xr-x. 2 root root 6 Apr 11  2018 debug
    drwxr-xr-x. 2 root root 6 Apr 11  2018 kernels
    ```
    
- Downloading the latest kernel from the kernel mirror.
    
    ```bash
    ➜  ~ cd /usr/src
    ➜  src wget https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/linux-4.11.
    7.tar.gz
    ```
    
- After getting the download,
    
    ```bash
    ➜  src ls -l linux-4.11.7.tar.gz 
    -rw-r--r-- 1 root root 145716393 Jun 24  2017 linux-4.11.7.tar.gz
    ```
    
- Now we will unzip this tar.gz file,
    
    ```bash
    ➜  src tar xvfz linux-4.11.7.tar.gz
    ➜  src ls
    debug  kernels  linux-4.11.7  linux-4.11.7.tar.gz
    ```
    
- Installing the required packages for updating the kernel,
    
    ```bash
    ➜  src yum -y groupinstall "Development Tools"
    ➜  src yum -y install gcc* ncurses-devel hmaccalc zlib-devel binutils-devel
    ➜  src yum -y install elfutils-libelf-devel qt-devel openssl-devel
    ```
    
- Going to the linux-4.11.7 directory under root,
    
    ```bash
    ➜  src cd ./linux-4.11.7 
    ➜  linux-4.11.7
    ```
    
- We will now reset and manage some files using,
    
    ```bash
    ➜  linux-4.11.7 make mrproper
    ➜  linux-4.11.7 make clean
    ```
    
     
    
- Copying the boot config file and installing the configs,
    
    ```bash
    ➜  linux-4.11.7 cp /boot/config-3.10.0-1160.81.1.el7.x86_64 ./.config
    ➜  linux-4.11.7 make menuconfig
    ```
    
- We will be introduced to a little GUI lookalike interface,
    
 
![Gui](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ckavsxeffhp8j1coudz.png)


    
    Selecting ‘Load’ from the option.
    
- It will ask for the name of the config
    
   
![Gui2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8n986fwcvnxv9044ruao.png)


    
    Going with ‘Ok’.
    
- Going in ‘General Setup’,
    
 
![Gui3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8qbt657t2v2wgjl20me.png)


    
- Next selecting and enabling this option under under General Setup,
    

![Enable](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jeqc6w8rxjo6sbjmtvjv.png)


    
    Save and exit
    

![Save](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yw3tn1epayqrpqjsf06g.png)


    
- We just need to update our kernel using the `make` command.
    
    ```bash
    ➜  linux-4.11.7 make all
    ```
    
- Next, we will install the kernel modules and add them,
    
    ```bash
    ➜  linux-4.11.7 make modules_install
    
    ➜  linux-4.11.7 make install
    sh ./arch/x86/boot/install.sh 4.11.7 arch/x86/boot/bzImage \
    	System.map "/boot"
    ```
    
- We can now check the kernel file entries,
    
    ```bash
    ➜  linux-4.11.7 cat /boot/grub2/grub.cfg
    
    # New Kernel Entry
    menuentry 'CentOS Linux (4.11.7) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-1127.el7.x86_64-advanced-3f993749-9c4e-444d-a133-4759913f8e80'
    
    # Old Kernel Entry
    menuentry 'CentOS Linux (3.10.0-1160.81.1.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-1127.el7.x86_64-advanced-3f993749-9c4e-444d-a133-4759913f8e80'
    ```
    

- Making the new kernel as the default kernel using the vi editor,
    
    ```bash
    ➜  linux-4.11.7 vi /etc/default/grub
    GRUB_TIMEOUT=5
    GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
    **GRUB_DEFAULT=0**
    GRUB_DISABLE_SUBMENU=true
    GRUB_TERMINAL_OUTPUT="console"
    GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
    GRUB_DISABLE_RECOVERY="true"
    ```
    
- Finally, we need to apply this using the `grub2-mkconfig`command.
    
    ```bash
    ➜  linux-4.11.7 grub2-mkconfig -o /boot/grub2/grub.cfg
    Generating grub configuration file ...
    Found linux image: /boot/vmlinuz-4.11.7
    Found initrd image: /boot/initramfs-4.11.7.img
    Found linux image: /boot/vmlinuz-3.10.0-1160.81.1.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-1160.81.1.el7.x86_64.img
    Found linux image: /boot/vmlinuz-3.10.0-1127.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-1127.el7.x86_64.img
    Found linux image: /boot/vmlinuz-0-rescue-8b2b1431e0ca4c528028cc8b6f68bbac
    Found initrd image: /boot/initramfs-0-rescue-8b2b1431e0ca4c528028cc8b6f68bbac.img
    done
    ```
    

- After the reboot, we can check our kernel version,
    
    ```bash
    ➜  ~ uname -sr
    Linux 4.11.7
    ```
    
- We can remove the installation files for the Linux 4.11 kernel to free up some space,
    
    ```bash
    ➜  ~ rm -rf /usr/src/linux-4.11.7*
    ```

---

## Removing the Kernel


> 👉 We will now remove the kernel we installed and make the old kernel as the default kernel.


- First, we will edit the `grub` file to make the old kernel as the default kernel.
    
    ```bash
    ➜  ~ vi /etc/default/grub
    
    GRUB_TIMEOUT=5
    GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
    **GRUB_DEFAULT=saved**
    GRUB_DISABLE_SUBMENU=true
    GRUB_TERMINAL_OUTPUT="console"
    GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
    GRUB_DISABLE_RECOVERY="true"
    ```
    
- To apply this setting,
    
    ```bash
    ➜  ~ grub2-mkconfig -o /boot/grub2/grub.cfg
    Generating grub configuration file ...
    Found linux image: /boot/vmlinuz-4.11.7
    Found initrd image: /boot/initramfs-4.11.7.img
    Found linux image: /boot/vmlinuz-3.10.0-1160.81.1.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-1160.81.1.el7.x86_64.img
    Found linux image: /boot/vmlinuz-3.10.0-1127.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-1127.el7.x86_64.img
    Found linux image: /boot/vmlinuz-0-rescue-8b2b1431e0ca4c528028cc8b6f68bbac
    Found initrd image: /boot/initramfs-0-rescue-8b2b1431e0ca4c528028cc8b6f68bbac.img
    done
    ```
    
- Removing the newly installed kernel information
    
    ```bash
    # Removing the 4.11.7 kernel module
    ➜  ~ rm -rf /lib/modules/4.11.7 
    
    # Removing any file that is related to 4.11.7 kernel
    ➜  ~ cd /boot
    ➜  /boot ls | grep 4.11.7
    initramfs-4.11.7.img
    System.map-4.11.7
    vmlinuz-4.11.7
    ➜  /boot rm -rf System.map-4.11.7 initramfs-4.11.7.img vmlinuz-4.11.7
    ```
    
- Now running the `mkconfig` command,
    
    ```bash
    ➜  ~ grub2-mkconfig -o /boot/grub2/grub.cfg
    Generating grub configuration file ...
    Found linux image: /boot/vmlinuz-3.10.0-1160.81.1.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-1160.81.1.el7.x86_64.img
    Found linux image: /boot/vmlinuz-3.10.0-1127.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-1127.el7.x86_64.img
    Found linux image: /boot/vmlinuz-0-rescue-8b2b1431e0ca4c528028cc8b6f68bbac
    Found initrd image: /boot/initramfs-0-rescue-8b2b1431e0ca4c528028cc8b6f68bbac.img
    done
    
    # Using the 'cat' command
    
    menuentry 'CentOS Linux (3.10.0-1160.81.1.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-1160.81.1.el7.x86_64-advanced-3f993749-9c4e-444d-a133-4759913f8e80'
    ```
    

- After the reboot, we can confirm,
    
    ```bash
    ➜  ~ uname -sr
    Linux 3.10.0-1160.81.1.el7.x86_64
    ```

---

## ✨ Utilizing the ELRepository



### Updating the Kernel

> 👉 We will now update the Kernel using ELRepo. 

- First, we will download the KEY for ELRepo and also add the ELRepo repository
    
    ```bash
    ➜  ~ rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
    ➜  ~ rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
    ```
    
- To install from the fastest mirror and also install the kernel,
    
    ```bash
    ➜  ~ yum -y install yum-plugin-fastestmirror
    ➜  ~ yum --enablerepo=elrepo-kernel install kernel-ml
    ```
    
- Now, we just need to edit the `grub` file to change the default kernel
    
    ```bash
    ➜  ~ vi /etc/default/grub
    GRUB_TIMEOUT=5
    GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
    **GRUB_DEFAULT=0**
    GRUB_DISABLE_SUBMENU=true
    GRUB_TERMINAL_OUTPUT="console"
    GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
    GRUB_DISABLE_RECOVERY="true"
    ```
    
- Now running the `mkconfig` command,
    
    ```bash
    ➜  ~ grub2-mkconfig -o /boot/grub2/grub.cfg
    Generating grub configuration file ...
    Found linux image: /boot/vmlinuz-6.1.8-1.el7.elrepo.x86_64
    Found initrd image: /boot/initramfs-6.1.8-1.el7.elrepo.x86_64.img
    Found linux image: /boot/vmlinuz-3.10.0-1160.81.1.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-1160.81.1.el7.x86_64.img
    Found linux image: /boot/vmlinuz-3.10.0-1127.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-1127.el7.x86_64.img
    Found linux image: /boot/vmlinuz-0-rescue-8b2b1431e0ca4c528028cc8b6f68bbac
    Found initrd image: /boot/initramfs-0-rescue-8b2b1431e0ca4c528028cc8b6f68bbac.img
    done
    
    # Using the 'cat' command
    
    menuentry 'CentOS Linux (6.1.8-1.el7.elrepo.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-6.1.8-1.el7.elrepo.x86_64-advanced-3f993749-9c4e-444d-a133-4759913f8e80'
    ```
    

- After the reboot, we can confirm,
    
    ```bash
    ➜  ~ uname -sr
    Linux 6.1.8-1.el7.elrepo.x86_64
    ```

### Removing the Kernel


> 👉 We will now remove the kernel we installed using ELRepo and make the old kernel as the default kernel.


- To install from the fastest mirror and also install the kernel,
    
    ```bash
    ➜  ~ vi /etc/default/grub
    GRUB_TIMEOUT=5
    GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
    **GRUB_DEFAULT=saved**
    GRUB_DISABLE_SUBMENU=true
    GRUB_TERMINAL_OUTPUT="console"
    GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
    GRUB_DISABLE_RECOVERY="true"
    ```
    
- Now, we just need to edit the `grub` file to change the default kernel
    
    ```bash
    ➜  ~ grub2-mkconfig -o /boot/grub2/grub.cfg
    Generating grub configuration file ...
    Found linux image: /boot/vmlinuz-6.1.8-1.el7.elrepo.x86_64
    Found initrd image: /boot/initramfs-6.1.8-1.el7.elrepo.x86_64.img
    Found linux image: /boot/vmlinuz-3.10.0-1160.81.1.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-1160.81.1.el7.x86_64.img
    Found linux image: /boot/vmlinuz-3.10.0-1127.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-1127.el7.x86_64.img
    Found linux image: /boot/vmlinuz-0-rescue-8b2b1431e0ca4c528028cc8b6f68bbac
    Found initrd image: /boot/initramfs-0-rescue-8b2b1431e0ca4c528028cc8b6f68bbac.img
    done
    ```
    
- After the reboot, we can confirm,
    
    ```bash
    ➜  ~ uname -sr
    Linux 3.10.0-1160.81.1.el7.x86_64
    ```
    
- We can remove the latest kernel that we installed,
    
    ```bash
    ➜  ~ yum remove kernel-ml
    ```