CentOS Remove Old Kernels

Tonight when upgrading a CentOS Linux server it was noted by YUM that 15MB more disk space was needed to upgrade grub and the kernel itself. Below are four quick steps to verify what kernels are installed, install yum-utils if it is not installed already, delete previous kernels and set YUM to remove old kernels when new kernels are installed. Depending on the size of your /boot directory you should make a decision on how many previous kernels you want to keep installed though I would recommend always keeping at least one previous kernel at a minimum. In the steps below we go off that recommendation and set YUM to always keep two kernels which would include the current kernel and one previous kernel.

YUM Warning Regarding /boot Disk Space:

As you can see below during the transaction test YUM runs during package installation, upgrades, etc. there is a warning that stops the update process because there is not enough room on the /boot partition.

Total                                                                                                                                                                          8.5 MB/s | 167 MB     00:19
Running rpm_check_debug
Running Transaction Test
Transaction Check Error:
  installing package kernel-2.6.32-431.17.1.el6.centos.plus.x86_64 needs 15MB on the /boot filesystem
  installing package grub-1:0.97-83.el6.x86_64 needs 15MB on the /boot filesystem
Error Summary
-------------
Disk Requirements:
  At least 15MB more space needed on the /boot filesystem.

Once you receive that warning it is always good to manually check the disk space left on the server to verify YUM is reporting accurately. You can see in the below output from a CentOS Linux server that there is only 14MB of disk space available and YUM is reporting that it needs at least 29MB of disk space to complete the grub and kernel upgrades.

Show Disk Usage Per Partition on CentOS Linux:

[root@dev ~# df -kh
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 228G 22G 194G 11% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/sda1 99M 81M 14M 87% /boot
[root@dev ~]#

Follow the steps below to list currently installed kernels on your CentOS server, install yum-utils for the package-cleanup command, remove the oldest kernels on the CentOS Linux server and configure YUM to automatically remove older kernels when new kernels are installed in the future.

Remove Previous CentOS Linux Kernels:

  1. List Installed CentOS Linux Kernels: Use the RPM command listed in the example below to list the set of kernels currently installed on your CentOS Linux server.
    [root@dev ~]# rpm -q kernel
    kernel-2.6.32-358.el6.x86_64
    kernel-2.6.32-358.6.2.el6.x86_64
    kernel-2.6.32-358.18.1.el6.x86_64
    [root@dev ~]#
  2. Install YUM Utilities Package On CentOS Linux: Now install the yum-utils package on Linux as shown in the example command below.
    [root@dev ~]# yum install yum-utils
    Loaded plugins: fastestmirror, security
    Loading mirror speeds from cached hostfile
     * atomic: www8.atomicorp.com
     * epel: mirror.compevo.com
     * rpmforge: mirror.team-cymru.org
    Setting up Install Process
    Resolving Dependencies
    --> Running transaction check
    ---> Package yum-utils.noarch 0:1.1.30-14.el6 will be updated
    ---> Package yum-utils.noarch 0:1.1.30-17.el6_5 will be an update
    --> Finished Dependency Resolution
    Dependencies Resolved
    ===============================================================================================================================
     Package                    Arch                     Version               Repository                             Size
    ===============================================================================================================================
    Updating:
     yum-utils                  noarch                   1.1.30-17.el6_5       updates                                102 k
    Transaction Summary
    ===============================================================================================================================
    Upgrade       1 Package(s)
    Total size: 102 k
    Is this ok [y/N]: y
    Downloading Packages:
    Running rpm_check_debug
    Running Transaction Test
    Transaction Test Succeeded
    Running Transaction
      Updating   : yum-utils-1.1.30-17.el6_5.noarch                             1/2
      Cleanup    : yum-utils-1.1.30-14.el6.noarch                               2/2
      Verifying  : yum-utils-1.1.30-17.el6_5.noarch                             1/2
      Verifying  : yum-utils-1.1.30-14.el6.noarch                               2/2
    Updated:
      yum-utils.noarch 0:1.1.30-17.el6_5                                                                                             Complete!
    [root@dev ~]#
  3. Remove Older CentOS Linux Kernels: Once the CentOS yum-utils package has been installed you will now have access to the package-cleanup command which will allow us to easily remove previous CentOS Linux kernels as shown in the below example command output.
    [root@dev ~]# package-cleanup --oldkernels --count=2
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * atomic: www8.atomicorp.com
     * epel: mirror.steadfast.net
     * rpmforge: mirror.team-cymru.org
    --> Running transaction check
    ---> Package kernel.x86_64 0:2.6.32-358.el6 will be erased
    --> Finished Dependency Resolution
    Dependencies Resolved
    ===============================================================================================================================
     Package        Arch       Version           Repository                                                       Size
    ===============================================================================================================================
    Removing:
     kernel         x86_64    2.6.32-358.el6     @anaconda-CentOS-201303050102.x86_64/6.4                         116 M
    Transaction Summary
    ===============================================================================================================================
    Remove        1 Package(s)
    Installed size: 116 M
    Is this ok [y/N]: y
    Downloading Packages:
    Running rpm_check_debug
    Running Transaction Test
    Transaction Test Succeeded
    Running Transaction
      Erasing    : kernel-2.6.32-358.el6.x86_64            1/1
      Verifying  : kernel-2.6.32-358.el6.x86_64            1/1
    Removed:
      kernel.x86_64 0:2.6.32-358.el6                                                                                               Complete!
    [root@dev ~]#
  4. Update YUM Configuration to Automatically Remove Old Kernels: Once we have the old CentOS Linux kernels removed we should now update the yum.conf file located in the /etc directory on CentOS Linux to automatically remove the oldest kernel every time a new kernel is installed. Again the minimum you should set the install_only limit is two so you can always roll back to the older kernel is need be. Use your favorite editor such as “vi” to edit the /etc/yum.conf file and modify the install_only configuration line from the default 5 to 2 as shown in the below examples.Default yum.conf install_only Configuration:
    installonly_limit=5

    Default yum.conf install_only Configuration:

    installonly_limit=2
  5. Verify CentOS Linux /boot Directory Free Disk Space: As you can see in the output below there is 42MB of disk space available even after using “yum update” to update packages that included a new kernel.
    [root@dev ~]# df -kh
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3       228G   22G  194G  11% /
    tmpfs           1.9G     0  1.9G   0% /dev/shm
    /dev/sda1        99M   53M   42M  57% /boot
    [root@dev ~]#

The end result will be that disk space for the /boot directory should always be under control. In my personal opinion the /boot directory should always be installed with more than 100MB of space so you can keep numerous older kernels or kernels for different tasks but many times you might take over administration of a server that is already configured and running.