Kernel Cleanup Using YUM

Published Tuesday, Nov 28, 2017 by Tony Davis

This document will show you how to set the RHEL7 hostname (Redhat 7 or CentOS 7) based machine. If you have built your new RHEL7 based machine and have now got a bit stuck over how to change the hostname from localhost.localdomain to what ever you want this is the how to for you.

Check Installed Kernels

The command below will list all kernels that are currently installed on the system

[root@server ~]# rpm -q kernel
kernel-3.10.0-514.el7.x86_64
kernel-3.10.0-514.6.1.el7.x86_64
kernel-3.10.0-693.5.2.el7.x86_64
kernel-3.10.0-693.11.1.el7.x86_64
kernel-3.10.0-693.11.6.el7.x86_64
The uname command will show which kernel is currently Running

[root@server ~]# uname -r
3.10.0-693.11.6.el7.x86_64

Remove Old Kernels

Next we will install the yum-utils package which contains the tools we need to limit the number of installed kernels.

Install Utilities

[root@server ~]# yum install yum-utils

Set Kernels to Keep

Package-cleanup is used to set how many packages will be kept. The command below sets 2 old kernels to be kept.

[root@server ~]# package-cleanup --oldkernels --count=2
Loaded plugins: fastestmirror
--> Running transaction check
---> Package kernel.x86_64 0:3.10.0-514.el7 will be erased
---> Package kernel.x86_64 0:3.10.0-514.6.1.el7 will be erased
---> Package kernel.x86_64 0:3.10.0-693.5.2.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package       Arch          Version                     Repository        Size
================================================================================
Removing:
 kernel        x86_64        3.10.0-514.el7              @anaconda        148 M
 kernel        x86_64        3.10.0-514.6.1.el7          @updates         148 M
 kernel        x86_64        3.10.0-693.5.2.el7          @updates          59 M

Transaction Summary
================================================================================
Remove  3 Packages

Installed size: 355 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : kernel.x86_64                            1/3
  Erasing    : kernel.x86_64                            2/3
  Erasing    : kernel.x86_64                            3/3
  Verifying  : kernel-3.10.0-693.5.2.el7.x86_64         1/3
  Verifying  : kernel-3.10.0-514.6.1.el7.x86_64         2/3
  Verifying  : kernel-3.10.0-514.el7.x86_64             3/3

Removed:
  kernel.x86_64 0:3.10.0-514.el7 kernel.x86_64 0:3.10.0-514.6.1.el7 kernel.x86_64 0:3.10.0-693.5.2.el7

Complete!

Kernel Count Check

Next check how many kernels have been left installed, it should be 2

[root@server ~]# rpm -q kernel
kernel-3.10.0-693.11.1.el7.x86_64
kernel-3.10.0-693.11.6.el7.x86_64

Update Installed Kernels Permanently

Next we need to set the number of kernels to stay at two permanently.

Edit /etc/yum.conf or /etc/dnf/dnf.conf and set installonly_limit:

installonly_limit=2

Thats it, now when ever we update the system, there will only be the last two kernels left on the system.