In this quick tutorial, you will learn how to remove old unused Kernels on CentOS or RHEL servers.
Step 1./ Checking for the current Kernel
– To check which kernel is currently used by your server, it a bit easy just run the below command:
# uname -a
Linux ylclsrv02.yallalabs.com 3.10.0-862.3.3.el7.x86_64 #1 SMP Fri Jun 15 04:15:27 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Step 2./ Listing all the installed kernels
– To get all the installed kernels on your server, use the following command:
# rpm -qa kernel kernel-3.10.0-327.36.3.el7.x86_64 kernel-3.10.0-514.2.2.el7.x86_64 kernel-3.10.0-693.21.1.el7.x86_64 kernel-3.10.0-862.3.3.el7.x86_64
Step 3./ Removing old kernels
– To remove old Kernels, we can proceed manually by uninstalling old kernel pakages using the yum remove
or by using the package-cleanup
command which is a part of yum-utils
package:
Remove Manually
# yum remove kernel-3.10.0-327.36.3.el7.x86_64 kernel-3.10.0-514.2.2.el7.x86_64 kernel-3.10.0-693.21.1.el7.x86_64 Loaded plugins: fastestmirror Resolving Dependencies --> Running transaction check ---> Package kernel.x86_64 0:3.10.0-327.36.3.el7 will be erased ---> Package kernel.x86_64 0:3.10.0-514.2.2.el7 will be erased ---> Package kernel.x86_64 0:3.10.0-693.21.1.el7 will be erased Dependencies Resolved =======================================================================
Remove old unused kernel automatically using package-cleanup
– Using package-cleanup
command which is a part of yum-utils
package we can uninstall any number of old kernels automatically.
– First make sure to install the yum-utils
package as below:
# yum install yum-utils
– In the below example using --oldkernels --count=2
option with package-cleanup
command we will remove all unused kernel while keeping last two most recent kernel versions installed
# package-cleanup --oldkernels --count=2 Loaded plugins: fastestmirror --> Running transaction check ---> Package kernel.x86_64 0:3.10.0-327.36.3.el7 will be erased ---> Package kernel.x86_64 0:3.10.0-514.2.2.el7 will be erased --> Finished Dependency Resolution Dependencies Resolved =========================================================================== =========================================================================== Remove 2 Packages Installed size: 284 M Is this ok [y/N]:
Configure yum to auto-remove old kernels
– To Configure yum to auto-remove old kernels, update the /etc/yum.conf configuration file by changing the value of installonly_limit
option with the desired number of old kernels on your system to be kept after every system update, The minimum value to be set is 2
# vi /etc/yum.conf
[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=5
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