Remove Old Kernels on CentOS Linux and other distros

After a few years and many yum updates later, the hard drive begins to fill up. There may be many versions of the Linux kernel. Many of which may be removed and to do this the yum-utils package is needed.

Install the yum-utils package.

yum install -y yum-utils

Check your running version.

[root@server01 opt]# uname -a

Linux server01 2.6.32-504.8.1.el6.x86_64 #1 SMP Wed Jan 28 21:11:36 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

Check for installed kernels.

rpm -q kernel

The result may look something like this.

[root@server01 opt]# rpm -q kernel

Note that the running kernel is older than the latest kernel that has been downloaded.  Before cleaning up your kernels you may want to reboot to the new kernel to see if everything is still working as expected before cleaning out the older kernels.

Remove the old kernels, except two.  Keep the current one and at least the one more to fall back to if the current should fail you.

package-cleanup --oldkernels --count=2

Follow the instructions and you can reverify the number of kernels with the command above.

To make this permanent, update the /etc/yum.conf and change the installonly_lime value to 2.