remove old kernels in rhel

How to Remove Unused Kernels in RHEL/Fedora/CentOS

Whenever Linux systems upgrade kernels, they tend to retain older versions of kernels on your disk. As a result, over time, you will find many old/unused kernels lying idle on your system and occupying a lot of disk space. It is enough to keep the last one or two old kernels and remove the rest from your system, to save on disk space. They will be useful in case an update fails. In this article, we will look at how to remove unused kernels in RHEL/Fedora/CentOS systems.

How to Remove Unused Kernels in RHEL/Fedora/CentOS

Here are the steps to remove unused kernels in RHEL/Fedora/CentOS.

List Kernel Version

Open terminal and run the following command to get the latest kernel version in Linux.

$ uname -sr
Linux 3.10.0-327.10.1.el7.x86_64

List all installed kernels

Run the following command to list installed kernels in your system.

# rpm -q kernel


Remove Old/Unused Kernels from RHEL/CentOS

You can always remove the above kernel file using rm command. But it can be error prone because if you delete the wrong kernel version, your system may stop working. Also if you delete the last kernel version and your update fails, then you may not be able to switch back to the last working kernel. So it is recommended to install yum-utils utility that allows you to conveniently control which kernel versions to be deleted. Here is the command to install it.

# yum install yum-utils

yum-utils contains many useful tools, including package cleanup that allows you to specify how many old kernels to be deleted. Here is an example of command to keep the last 2 kernel versions and delete the rest.

# package-cleanup --oldkernels --count=2

The above command will keep the current running and the last kernel versions on your system, and delete all other kernel versions from your system.

Remove Old/unused kernels from Fedora

You can run the following command to delete older kernel versions except the current running one and the last one.

# dnf remove $(dnf repoquery --installonly --latest-limit=-2 -q) 

Alternatively, you may also change yum.conf file to specify that you want to keep only last 2 versions of kernel. In this case, yum will automatically retain only the latest kernel and the one before, and delete the rest, every time it upgrades your kernel.

installonly_limit=2		#set kernel count

Save and close the file.

In this article, we have learnt how to delete old kernel versions from system, while keeping the current one and the last one. In fact, you can run the above commands as a cronjob that runs every 3-4 months. This way your system will automatically cleanup all unused kernel versions as and when your system gets upgraded.

Also read:

How to Install Sublime Text in Linux
HAProxy Load Balancer Configuration in Linux
How to Clone Partition or Hard Disk in Linux
How to Assign Multiple IP Addresses to Network Interface
How to Find Public IP Address of Server

2 thoughts on “How to Remove Unused Kernels in RHEL/Fedora/CentOS

  1. Your Remove Old/unused kernels from Fedora command is incorrect.

    Your recommended command is:
    # dnf remove $(dnf repoquery –installonly –latest-limit 2 -q)
    which will try to remove the latest two installed kernels, including the running kernel.

    The correct command is:
    # dnf remove $(dnf repoquery –installonly –latest-limit=-2 -q)
    which will keep the latest two installed kernels and remove any older than those.

Leave a Reply

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