Aug 082015
 

Remove Old Kernels in CentOS 7If you’ve been using a CentOS 7 or RedHat 7 system for some time and updating it regularly, you’re bound to accumulate multiple kernels.

In this post, we’ll learn how to check the number of installed kernels on a CentOS, RedHat or Fedora box and remove the older ones.

Check Installed Kernels

Go ahead and check how many versions of the kernel you have on your CentOS, Red Hat or Fedora system with the below command.

# rpm -q kernel
kernel-3.10.0-123.20.1.el7.x86_64
kernel-3.10.0-229.1.2.el7.x86_64
kernel-3.10.0-229.4.2.el7.x86_64
kernel-3.10.0-229.7.2.el7.x86_64
kernel-3.10.0-229.11.1.el7.x86_64

On my CentOS 7 system (see above), there were five versions of the kernel.

Now that’s three too many if you ask me.

How to Remove Old Kernels

Removing old kernels is an easy task with the below procedure.

If you don’t already have it on your system, install yum-utils.

$ sudo yum install yum-utils

The next step is to set the package-cleanup count to as many old kernels you want left on the system. Let’s keep it at two.

# package-cleanup --oldkernels --count=2
Loaded plugins: fastestmirror, langpacks
--> Running transaction check
---> Package kernel.x86_64 0:3.10.0-123.20.1.el7 will be erased
---> Package kernel.x86_64 0:3.10.0-229.1.2.el7 will be erased
---> Package kernel.x86_64 0:3.10.0-229.4.2.el7 will be erased
---> Package kernel-devel.x86_64 0:3.10.0-123.20.1.el7 will be erased
---> Package kernel-devel.x86_64 0:3.10.0-229.1.2.el7 will be erased
---> Package kernel-devel.x86_64 0:3.10.0-229.4.2.el7 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
..........
Remove  6 Packages
Installed size: 484 M
Is this ok [y/N]: y
.........
Removed:
  kernel.x86_64 0:3.10.0-123.20.1.el7          kernel.x86_64 0:3.10.0-229.1.2.el7           kernel.x86_64 0:3.10.0-229.4.2.el7     kernel-devel.x86_64 0:3.10.0-123.20.1.el7    
  kernel-devel.x86_64 0:3.10.0-229.1.2.el7     kernel-devel.x86_64 0:3.10.0-229.4.2.el7    
Complete!

Voila, as simple as that.

Let’s again run the rpm -q kernel command to see how many kernels are left on our CentOS system.

$ rpm -q kernel
kernel-3.10.0-229.7.2.el7.x86_64
kernel-3.10.0-229.11.1.el7.x86_64

As expected, there are only two kernels, which is what we wanted.

I have tested the above commands on CentOS 7 and CentOS 6 and they work fine on both.

They should work without issues on Red Hat and Fedora too.

Permanent Fix

To avoid accumulation of kernels again over time, let’s edit the /etc/yum.conf file.

To do this, first run the below command on the terminal and check the installonly_limit.

$ cat /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5

On my CentOS 7 desktop, the installonly_limit was set to 5.

Now let’s open Vim or Nano and reduce the installonly_limit from 5 to 2.

You’re now set and don’t have to worry about old kernels accumulating on your CentOS or Red Hat system again.

At any time, there’ll just be two kernels on your Linux box.

 Posted by at 7:06 pm  Tagged with:

Sorry, the comment form is closed at this time.