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

  • Free Up Disk Space: Old kernels can occupy significant disk space. Removing them can free up space, especially important on systems with limited storage.
  • Reduce Clutter: Keeping only the necessary kernels reduces system clutter, making it easier to manage and navigate your file system.
  • Improve Boot Time: A shorter list of kernels in the GRUB menu can make boot selection quicker and more straightforward.
  • Security: Ensures that only the most up-to-date kernels, which include the latest security patches, are in use, reducing the risk of vulnerabilities.

Cons

  • Rollback Issues: Removing old kernels eliminates the possibility of rolling back to a previous kernel version if the current one has issues. This can be problematic if you encounter hardware or software compatibility problems with a new kernel.
  • Potential for Mistakes: If not done carefully, you might accidentally remove the current or necessary kernels, leading to system boot issues.
  • Complexity: The process might be complex for beginners, requiring careful attention to ensure that only unnecessary kernels are removed.
  • Dependency Problems: There can be cases where certain drivers or modules are only compatible with specific kernel versions, and removing those kernels might lead to functionality issues.

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

745772 0021

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

Video: How to remove Old Linux Kernels

YouTube player

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.

4 thoughts on “How to remove Old Linux Kernels”
  1. you’re truly a good webmaster. The site loading pace is incredible.

    It kind of feels that you are doing any unique trick.

    Also, The contents are masterpiece. you’ve performed a fantastic process in this subject!

Leave a Reply