Removing Old Linux Kernels on CentOS - Adventures in Switching to Linux

Friday, February 8, 2008

Removing Old Linux Kernels on CentOS

This blog is mostly about desktop Linux but I also help manage a handful of Linux server for work. We mostly run CentOS servers or the occasional Fedora on internal sites but use CentOS exclusively for any public sites. I was finally getting around to updating a CentOS 3 server today with good old yum update when I ran into an unwelcome surprise.

Running test transaction:
Errors reported doing trial run
installing package kernel-smp-2.4.21-53.EL needs 53KB on the /boot filesystem
D'oh! I knew this day would come eventually when my tiny little 90MB /boot partition would get full!

So what to do? Easy, just uninstall some of those old kernels. But how do I do that? It turns out, that is also easy.

First I figure out what kernel I am actually running now so I don't try and delete it. I am not sure what would happen if I tried but I am not going to find out. To determine the running kernel, use uname -r. I am running 2.4.21-52.ELsmp .

Next we want to see what other kernels are installed to find what we can delete. Do this like so:
[root@localhost root]# rpm -q kernel

I see that we have 7 old kernels installed. Now to delete them, just use rpm -e like so:

rpm -e kernel-2.4.21-37.EL

And it is gone. Rinse and repeat for the other kernels. I only plan on getting rid of a few. This is what was left:
[root@localhost root]# rpm -q kernel
Now I have 30M instead of 5.6M free on my /boot partition and I can continue the update. Unfortunately that didn't clean up /boot/grub/grub.conf to remove the old kernels from the grub boot menu. Maybe that has been fixed in CentOS 4 or 5 (well, RHEL).


Robert Oschwald said...

You can use "yum remove kernel-x.y.z" to remove the kernel, which also removes the grub.conf entry for it.

Ådne said...

Or, for the more adventurous. Modify to taste.

yum remove $(rpm -qa | grep kernel | grep -v `uname -r`)

boragora said...

Your line is very wrong. You can destroy half your OS with it. Do the following:

yum remove $(rpm -q kernel | grep -v `uname -r`)

omostan said...

Great post!
I realize you left the 3 latest versions or your kernels. I Think it could have be done much easier by using the following commands:

yum install yum-utils
package-cleanup --oldkernels --count=3

that should remove all but the latest three kernels version regardless how many you may have installed.

You could even make this settings permanent by editing the /etc/yum.conf file and change this line as follows: