CentOS7上删除旧内核

centos7 上有专门的清理工具,package-cleanup来执行内核的清理。

[root@centos7 ~]# package-cleanup --oldkernels --count=1
Loaded plugins: fastestmirror, langpacks
--> Running transaction check
---> Package kernel.x86_64 0:3.10.0-1160.el7 will be erased
---> Package kernel.x86_64 0:3.10.0-1160.88.1.el7 will be erased
--> Finished Dependency Resolution
base/7/x86_64                                                                                                                                                                              | 3.6 kB  00:00:00     
base/7/x86_64/group_gz                                                                                                                                                                     | 153 kB  00:00:00     
base/7/x86_64/primary_db                                                                                                                                                                   | 6.1 MB  00:00:00     
updates/7/x86_64                                                                                                                                                                           | 2.9 kB  00:00:00     
updates/7/x86_64/primary_db                                                                                                                                                                |  21 MB  00:00:01     

Dependencies Resolved

==================================================================================================================================================================================================================
 Package                                       Arch                                          Version                                                       Repository                                        Size
==================================================================================================================================================================================================================
Removing:
 kernel                                        x86_64                                        3.10.0-1160.el7                                               @anaconda                                         64 M
 kernel                                        x86_64                                        3.10.0-1160.88.1.el7                                          @updates                                          66 M

Transaction Summary
==================================================================================================================================================================================================================
Remove  2 Packages

Installed size: 130 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/2 
  Erasing    : kernel.x86_64                                                                                                                                                                                  2/2 
  Verifying  : kernel-3.10.0-1160.88.1.el7.x86_64                                                                                                                                                             1/2 
  Verifying  : kernel-3.10.0-1160.el7.x86_64                                                                                                                                                                  2/2 

Removed:
  kernel.x86_64 0:3.10.0-1160.el7                                                                       kernel.x86_64 0:3.10.0-1160.88.1.el7    

CentOS8上删除旧内核

centos8以上则通过定义/etc/dnf/dnf.conf 来清理内核。

[root@centos8 ~]# dnf -y remove --oldinstallonly --setopt installonly_limt=1 kernel
Main config did not have a installonly_limt attr. before setopt
Repository extras is listed more than once in the configuration
Main config did not have a installonly_limt attr. before setopt
Dependencies resolved.
=====================================================================
 Package          Arch     Version                 Repository   Size
=====================================================================
Removing:
 kernel           x86_64   4.18.0-425.3.1.el8      @anaconda     0  
 kernel           x86_64   4.18.0-425.19.2.el8_7   @baseos       0  
 kernel-core      x86_64   4.18.0-425.3.1.el8      @anaconda    70 M
 kernel-core      x86_64   4.18.0-425.19.2.el8_7   @baseos      70 M
 kernel-modules   x86_64   4.18.0-425.3.1.el8      @anaconda    24 M
 kernel-modules   x86_64   4.18.0-425.19.2.el8_7   @baseos      24 M

Transaction Summary
=====================================================================
Remove  6 Packages

Freed space: 187 M
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                             1/1 
  Erasing          : kernel-4.18.0-425.19.2.el8_7.x86_64         1/6 
  Running scriptlet: kernel-4.18.0-425.19.2.el8_7.x86_64         1/6 
  Erasing          : kernel-4.18.0-425.3.1.el8.x86_64            2/6 
  Running scriptlet: kernel-4.18.0-425.3.1.el8.x86_64            2/6 
  Erasing          : kernel-modules-4.18.0-425.3.1.el8.x86_64    3/6 
  Running scriptlet: kernel-modules-4.18.0-425.3.1.el8.x86_64    3/6 
  Erasing          : kernel-modules-4.18.0-425.19.2.el8_7.x86_   4/6 
  Running scriptlet: kernel-modules-4.18.0-425.19.2.el8_7.x86_   4/6 
  Running scriptlet: kernel-core-4.18.0-425.19.2.el8_7.x86_64    5/6 
  Erasing          : kernel-core-4.18.0-425.19.2.el8_7.x86_64    5/6 
  Running scriptlet: kernel-core-4.18.0-425.19.2.el8_7.x86_64    5/6 
  Running scriptlet: kernel-core-4.18.0-425.3.1.el8.x86_64       6/6 
  Erasing          : kernel-core-4.18.0-425.3.1.el8.x86_64       6/6 
  Running scriptlet: kernel-core-4.18.0-425.3.1.el8.x86_64       6/6 
  Running scriptlet: kernel-core-4.18.0-425.3.1.el8.x86_64                                                                                                                                                    6/6 
  Verifying        : kernel-4.18.0-425.3.1.el8.x86_64                                                                                                                                                         1/6 
  Verifying        : kernel-4.18.0-425.19.2.el8_7.x86_64                                                                                                                                                      2/6 
  Verifying        : kernel-core-4.18.0-425.3.1.el8.x86_64                                                                                                                                                    3/6 
  Verifying        : kernel-core-4.18.0-425.19.2.el8_7.x86_64                                                                                                                                                 4/6 
  Verifying        : kernel-modules-4.18.0-425.3.1.el8.x86_64                                                                                                                                                 5/6 
  Verifying        : kernel-modules-4.18.0-425.19.2.el8_7.x86_64                                                                                                                                              6/6 

Removed:
  kernel-4.18.0-425.3.1.el8.x86_64             kernel-4.18.0-425.19.2.el8_7.x86_64  kernel-core-4.18.0-425.3.1.el8.x86_64  kernel-core-4.18.0-425.19.2.el8_7.x86_64  kernel-modules-4.18.0-425.3.1.el8.x86_64 
  kernel-modules-4.18.0-425.19.2.el8_7.x86_64 

Complete!

参考:How to properly remove old kernels RHEL/CentOS 8