Site icon CentLinux

How to remove Old Linux Kernels

Share on Social Media

Learn how to remove old Linux kernels safely to free up disk space and keep your system organized. Follow our step-by-step guide to clean up unused kernels and maintain an efficient Linux environment. #centlinux #linux #linuxkernel

What 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.

Removing old Linux kernels can be beneficial but also comes with certain risks. Here are the pros and cons of removing old Linux kernels:

Pros

Cons

Removing old Linux kernels is a good practice to maintain an organized and efficient system, but it should be done with caution. Always ensure that you have at least one stable kernel that you can fall back on in case of issues.

Recommended Online Training: Learn Bash Shell in Linux for Beginners

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.

GRUB Menu before Removing Old Linux 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.

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

Check installed Kernels in your Linux OS

Connect with your Linux server as root user.

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

# 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

Removing 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.

GRUB Menu after Removing Old Linux Kernels

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.

Final Thoughts

Removing old Linux kernels is an essential maintenance task to free up disk space and keep your system tidy. By following this guide, you can safely clean up unused kernels and ensure your Linux environment runs efficiently. If you need personalized assistance or face any issues during the process, I offer professional services on Fiverr. Check out my Fiverr gig for expert help with managing your Linux system and other maintenance tasks.

Exit mobile version