How to Limit the Number of Kernels Installed

Many Linux distributions do not clean up old kernels when new ones are installed.  This can become problematic in situations where /boot is on its own partition with a limited amount of space.  While you can easily clean up the boot partition of old kernels, this is a task that you may prefer to automate, especially if you’re dealing with multiple servers.

First, install the yum-utils package, if it isn’t already there:

yum install yum-utils

Once this is installed, you can run the following command to remove old kernels:

package-cleanup --oldkernels --count=3

The count parameter defines how many kernels you want to keep.  So in the above example, the last three kernels will be retained.  You do not have to run this command every time you update the kernel version though, because there’s a parameter you can add to /etc/yum.conf to do this automatically: