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β
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,
Selecting βLoadβ from the option.
- It will ask for the name of the config
Going with βOkβ.
- Going in βGeneral Setupβ,
- Next selecting and enabling this option under under General Setup,
Save and exit
-
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-mkconfig
command.
β 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)