There is a yum package called yum-utils which contains the package-cleanup command. This command will allow you to pass arguments to it which clean up old kernels.
If you use package-cleanup –oldkernels –count=2 the command will remove all unused kernel while keeping last three most recent kernel versions installed.
# package-cleanup –oldkernels –count=2
Loaded plugins: fastestmirror
–> Running transaction check
—> Package kernel.x86_64 0:3.10.0-514.2.2.el7 will be erased
—> Package kernel-devel.x86_64 0:3.10.0-327.el7 will be erased
—> Package kernel-devel.x86_64 0:3.10.0-327.36.3.el7 will be erased
—> Package kernel-devel.x86_64 0:3.10.0-514.2.2.el7 will be erased
–> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================================================================
Package Arch Version Repository Size
===============================================================================================================================================
Removing:
kernel x86_64 3.10.0-514.2.2.el7 @updates 148 M
kernel-devel x86_64 3.10.0-327.el7 @anaconda 33 M
kernel-devel x86_64 3.10.0-327.36.3.el7 @updates 33 M
kernel-devel x86_64 3.10.0-514.2.2.el7 @updates 34 M
Transaction Summary
===============================================================================================================================================
Remove 4 Packages
Installed size: 248 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing : kernel-devel.x86_64 1/4
Erasing : kernel-devel.x86_64 2/4
Erasing : kernel-devel.x86_64 3/4
Erasing : kernel-3.10.0-514.2.2.el7.x86_64 4/4
Verifying : kernel-3.10.0-514.2.2.el7.x86_64 1/4
Verifying : kernel-devel-3.10.0-514.2.2.el7.x86_64 2/4
Verifying : kernel-devel-3.10.0-327.36.3.el7.x86_64 3/4
Verifying : kernel-devel-3.10.0-327.el7.x86_64 4/4
Removed:
kernel.x86_64 0:3.10.0-514.2.2.el7 kernel-devel.x86_64 0:3.10.0-327.el7 kernel-devel.x86_64 0:3.10.0-327.36.3.el7
kernel-devel.x86_64 0:3.10.0-514.2.2.el7
Complete!
You can configure yum to automatically remove old kernels. By default CentOS will keep last 5 kernels installed on your system. This behavior is defined by installonly_limit=5 line within /etc/yum.conf file. You can update the /etc/yum.conf file to keep the appropriate number of old kernels on your system after an update. The minimum value to be set is 2.
Here is an example of a /etc/yum.conf file to keep only last two kernel versions:
[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=2
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release