Removing old kernels

The longer you have your server you may notice that the /boot partition has less and less space available. It is very possible that your server is never removing old kernels and is only installing new ones each kernel version release. If you have rebooted your server and everything is operating correctly, there is a very good chance you can remove most of those old kernels and clear up some extra space.

First let’s look at how many kernels you currently have installed.

1
dpkg --list | grep linux-image

This should return text similar to this.

1
2
3
4
5
6
rc  linux-image-4.2.0-42-generic           4.2.0-42.49~14.04.1                        amd64        Linux kernel image for version 4.2.0 on 64 bit x86 SMP
ii  linux-image-4.4.0-36-generic           4.4.0-36.55~14.04.1                        amd64        Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii  linux-image-4.4.0-79-generic           4.4.0-79.100~14.04.1                       amd64        Linux kernel image for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.2.0-42-generic     4.2.0-42.49~14.04.1                        amd64        Linux kernel extra modules for version 4.2.0 on 64 bit x86 SMP
ii  linux-image-extra-4.4.0-36-generic     4.4.0-36.55~14.04.1                        amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
ii  linux-image-extra-4.4.0-79-generic     4.4.0-79.100~14.04.1                       amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP

There is a handy tool for removing old kernels automatically in Ubuntu. After you install bikeshed (random useful tools for Ubuntu without a home – More information here.) it will be available.

1
sudo apt-get install bikeshed

After this has installed you can run a command similar to the following command. Adjust the number to change the number of old kernels to keep.

1
sudo purge-old-kernels --keep 2 -qy

First let’s look at how many kernels you currently have installed.

1
sudo rpm -q kernel

This should return text similar to this.

1
2
3
4
kernel-2.6.32-279.el6.x86_64
kernel-2.6.32-279.2.1.el6.x86_64
kernel-2.6.32-279.5.2.el6.x86_64
kernel-2.6.32-279.9.1.el6.x86_64

There is a handy tool for removing old kernels automatically in CentOS. After you install yum-utils (a collection of utilities and plugins extending and supplementing yum in different ways – More information here.) it will be available.

1
sudo yum install yum-utils

After this has installed you can run a command similar to the following command. Adjust the number to change the number of old kernels to keep.

1
sudo package-cleanup --oldkernels --count=2

To make the number of old kernels permanent, change /etc/yum.conf to contain the following line:

1
installonly_limit=2

About Brian Aldridge

I am a software developer and podcaster. Catch me weekly on Infection - The Survival Podcast at https://infectionpodcast.com

Leave a Reply