DEV Community

Cover image for Exploring Linux Kernels
Waji
Waji

Posted on

Exploring Linux Kernels

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β€œ

Kernle


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.

    ➜  ~ 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.

    ➜  ~ 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.

    ➜  ~ mkdir /test
    ➜  ~ mount /dev/sdb1 /test
    
  • Moving the /usr/src contents to /test while our partition is mounted in it.

    ➜  ~ 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

    ➜  ~ 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.

    ➜  ~ 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,

    ➜  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,

    ➜  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,

    ➜  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,

    ➜  src cd ./linux-4.11.7 
    ➜  linux-4.11.7
    
  • We will now reset and manage some files using,

    ➜  linux-4.11.7 make mrproper
    ➜  linux-4.11.7 make clean
    
  • Copying the boot config file and installing the configs,

    ➜  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

Selecting β€˜Load’ from the option.
Enter fullscreen mode Exit fullscreen mode
  • It will ask for the name of the config

Gui2

Going with β€˜Ok’.
Enter fullscreen mode Exit fullscreen mode
  • Going in β€˜General Setup’,

Gui3

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

Enable

Save and exit
Enter fullscreen mode Exit fullscreen mode

Save

  • We just need to update our kernel using the make command.

    ➜  linux-4.11.7 make all
    
  • Next, we will install the kernel modules and add them,

    ➜  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,

    ➜  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,

    ➜  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-mkconfigcommand.

    ➜  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,

    ➜  ~ uname -sr
    Linux 4.11.7
    
  • We can remove the installation files for the Linux 4.11 kernel to free up some space,

    ➜  ~ 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.

    ➜  ~ 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,

    ➜  ~ 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

    # 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,

    ➜  ~ 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,

    ➜  ~ 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

    ➜  ~ 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,

    ➜  ~ 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

    ➜  ~ 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,

    ➜  ~ 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,

    ➜  ~ 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,

    ➜  ~ 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

    ➜  ~ 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,

    ➜  ~ uname -sr
    Linux 3.10.0-1160.81.1.el7.x86_64
    
  • We can remove the latest kernel that we installed,

    ➜  ~ yum remove kernel-ml
    

Top comments (0)