Share on Social Media

This article will show you how to remove old Linux kernels from your CentOS / RHEL 8 and similar Linux distros. #centlinux #linux #linuxkernel

Whats is a Linux Kernel? :

The Linux kernel is a free and open-source, monolithic, modular, multitasking, Unix-like operating system kernel. It was conceived and created in 1991 by Linus Torvalds for his i386-based PC, and it was soon adopted as the kernel for the GNU operating system, which was created as a free replacement for UNIX. Since then, it has spawned a large number of operating system distributions, commonly also called Linux.

Linux is deployed on a wide variety of computing systems, such as embedded devices, mobile devices (including its use in the Android operating system), personal computers, servers, mainframes, and supercomputers.

Video to remove Old Linux Kernels:

YouTube player

Read Also: How to Safely Remove Old Kernels in Rocky Linux 9

Problem Definition:

If you updated your Linux operating system, then you have notice that, after each upgrade of Linux Kernel the GRUB menu will add a new boot entry while keeping the previous entries intact. Have a look at below screenshot.

linux grub menu before removing kernels

In default CentOS / RHEL 8 configurations, the Yum Package Manager installs maximum 3 versions of Linux Kernel. Therefore, the Kernel boot entries are limited to 3 only.

In this article, you will see how to remove old Linux kernels from your CentOS / RHEL 8 operating system.

Check Installed Linux Kernels:

Connect with your Linux server as root user.

Execute the following rpm command, to list the installed kernels on your Linux operating system.

# rpm -q kernel
kernel-4.18.0-240.10.1.el8_3.x86_64
kernel-4.18.0-240.22.1.el8_3.x86_64
kernel-4.18.0-305.12.1.el8_4.x86_64

Usually the latest Linux Kernel should be active on your operating system. Unless someone has explicitly set the default kernel to an older version.

You can confirm it by using following commands.

# uname -r
4.18.0-305.12.1.el8_4.x86_64

# grubby --default-kernel
/boot/vmlinuz-4.18.0-305.12.1.el8_4.x86_64

If the above command returns a kernel other than the latest one, then you have to set the default kernel for GRUB.

Set the Default Linux Kernel:

List down the available Linux kernels in /boot directory.

# ls /boot/vm*
/boot/vmlinuz-0-rescue-25d4accaa6754a5e97616dd5774f723b
/boot/vmlinuz-4.18.0-240.10.1.el8_3.x86_64
/boot/vmlinuz-4.18.0-240.22.1.el8_3.x86_64
/boot/vmlinuz-4.18.0-305.12.1.el8_4.x86_64

Execute the following command to set a default kernel for GRUB.

# grubby --set-default /boot/vmlinuz-4.18.0-305.12.1.el8_4.x86_64
The default is /boot/loader/entries/25d4accaa6754a5e97616dd5774f723b-4.18.0-305.12.1.el8_4.x86_64.conf with index 0 and kernel /boot/vmlinuz-4.18.0-305.12.1.el8_4.x86_64

Remove Old Linux Kernels:

By default, Linux kernels are always installed separately by the Yum Package Manager while keeping the old versions intact for fallback support.

This behavior is controlled by two Yum Package Manager directives.

installonlypkgs – Space separated list of packages who will never update by package manager.

installonly_limit – Maximum number of versions that can be installed simultaneously for any single package listed in the installonlypkgs directive.

Check the current value of installonly_limit directive

# grep limit /etc/dnf/dnf.conf
installonly_limit=3

Execute following command on Linux Bash prompt to remove the old versions of Linux kernels.

# dnf -y remove --oldinstallonly --setopt installonly_limit=1 kernel
...
Removed:
  kernel-4.18.0-240.10.1.el8_3.x86_64
  kernel-4.18.0-240.22.1.el8_3.x86_64
  kernel-core-4.18.0-240.10.1.el8_3.x86_64
  kernel-core-4.18.0-240.22.1.el8_3.x86_64
  kernel-modules-4.18.0-240.10.1.el8_3.x86_64
  kernel-modules-4.18.0-240.22.1.el8_3.x86_64

Complete!

You may notice that, instead of altering the installonly_limit directive value in dnf.conf file, we are setting it at the runtime for current dnf command only.

Reboot your Linux operating system now.

# reboot

Check the GRUB boot menu, The old kernels entries has been removed.

linux grub menu after removing kernels

Conclusion:

In this article, you have learned how to safely remove old Linux kernels in CentOS / RHEL 8 or similar Linux distros. If you face difficulty in understanding the above article than you should buy and read Practical Guide to Linux Commands, Editors, and Shell Programming (PAID LINK) by Mark Sobell & Matthew Helmke.

2 thoughts on “How to remove Old Linux Kernels”

Leave a Reply

Your email address will not be published. Required fields are marked *