Remove old kernels in CentOS, RHEL and Fedora

Your system to many old kernels version. How to remove old kernels in CentOS, RHEL and Fedora. For example, I have three kernel,and to keep two kernel on system. How to do it!

To check kernel release on centos/RHEL and Fedora

[huupv@huupv ~]$ uname -sr
The output kernel release as below:
Linux 4.15.15-300.fc27.x86_64

Checking installed kernel

[huupv@huupv ~]$ rpm -q kernel
The output three kernel as below:
kernel-4.15.13-300.fc27.x86_64
kernel-4.15.14-300.fc27.x86_64
kernel-4.15.15-300.fc27.x86_64

Delete / Remove Old Kernels

To install yum-utils
[root@huupv huupv]# yum install yum-utils
The output as below:
Last metadata expiration check: 2:54:35 ago on Sat 14 Apr 2018 07:14:37 PM +07.
Dependencies resolved.
================================================================================
 Package                  Arch        Version                 Repository   Size
================================================================================
Installing:
 yum-utils                noarch      1.1.31-513.fc27         fedora      117 k
Installing dependencies:
 python-urlgrabber        noarch      3.10.1-12.fc27          fedora      111 k
 python2-pycurl           x86_64      7.43.0-9.fc27           fedora      247 k
 pyxattr                  x86_64      0.5.3-12.fc27           fedora       34 k
 yum                      noarch      3.4.3-513.fc27          fedora      1.2 M
 yum-metadata-parser      x86_64      1.1.4-20.fc27           fedora       39 k

Transaction Summary
================================================================================
Install  6 Packages

Total download size: 1.8 M
Installed size: 7.4 M
Is this ok [y/N]: y
Downloading Packages:
(1/6): python-urlgrabber-3.10.1-12.fc27.noarch. 201 kB/s | 111 kB     00:00   
(2/6): yum-utils-1.1.31-513.fc27.noarch.rpm     203 kB/s | 117 kB     00:00   
(3/6): pyxattr-0.5.3-12.fc27.x86_64.rpm         427 kB/s |  34 kB     00:00   
(4/6): yum-metadata-parser-1.1.4-20.fc27.x86_64 375 kB/s |  39 kB     00:00   
(5/6): python2-pycurl-7.43.0-9.fc27.x86_64.rpm  934 kB/s | 247 kB     00:00   
(6/6): yum-3.4.3-513.fc27.noarch.rpm            1.1 MB/s | 1.2 MB     00:01   
--------------------------------------------------------------------------------
Total                                           868 kB/s | 1.8 MB     00:02    
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : python2-pycurl-7.43.0-9.fc27.x86_64                    1/6
  Installing       : python-urlgrabber-3.10.1-12.fc27.noarch                2/6
  Installing       : yum-metadata-parser-1.1.4-20.fc27.x86_64               3/6
  Installing       : pyxattr-0.5.3-12.fc27.x86_64                           4/6
  Installing       : yum-3.4.3-513.fc27.noarch                              5/6
  Running scriptlet: yum-3.4.3-513.fc27.noarch                              5/6
  Installing       : yum-utils-1.1.31-513.fc27.noarch                       6/6
  Running scriptlet: yum-utils-1.1.31-513.fc27.noarch                       6/6
Running as unit: run-r93b673459ba7488f95be27fd4a5c28f3.service
  Verifying        : yum-utils-1.1.31-513.fc27.noarch                       1/6
  Verifying        : yum-3.4.3-513.fc27.noarch                              2/6
  Verifying        : python-urlgrabber-3.10.1-12.fc27.noarch                3/6
  Verifying        : pyxattr-0.5.3-12.fc27.x86_64                           4/6
  Verifying        : yum-metadata-parser-1.1.4-20.fc27.x86_64               5/6
  Verifying        : python2-pycurl-7.43.0-9.fc27.x86_64                    6/6

Installed:
  yum-utils.noarch 1.1.31-513.fc27     python-urlgrabber.noarch 3.10.1-12.fc27 
  python2-pycurl.x86_64 7.43.0-9.fc27  pyxattr.x86_64 0.5.3-12.fc27            
  yum.noarch 3.4.3-513.fc27            yum-metadata-parser.x86_64 1.1.4-20.fc27

Complete!
How many old kernels you want left
[root@huupv huupv]# package-cleanup --oldkernels --count=2
The output as below:

Yum-utils package has been deprecated, use dnf instead.
See 'man yum2dnf' for more information.


--> Running transaction check
---> Package kernel.x86_64 0:4.15.13-300.fc27 will be erased
---> Package kernel-core.x86_64 0:4.15.13-300.fc27 will be erased
--> Processing Dependency: kernel-uname-r = 4.15.13-300.fc27.x86_64 for package: kernel-modules-4.15.13-300.fc27.x86_64
--> Processing Dependency: kernel-uname-r = 4.15.13-300.fc27.x86_64 for package: kernel-modules-extra-4.15.13-300.fc27.x86_64
---> Package kernel-devel.x86_64 0:4.15.13-300.fc27 will be erased
--> Running transaction check
---> Package kernel-modules.x86_64 0:4.15.13-300.fc27 will be erased
---> Package kernel-modules-extra.x86_64 0:4.15.13-300.fc27 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                  Arch       Version                Repository     Size
================================================================================
Removing:
 kernel                   x86_64     4.15.13-300.fc27       installed     0.0 
 kernel-core              x86_64     4.15.13-300.fc27       installed      58 M
 kernel-devel             x86_64     4.15.13-300.fc27       installed      48 M
Removing for dependencies:
 kernel-modules           x86_64     4.15.13-300.fc27       installed      26 M
 kernel-modules-extra     x86_64     4.15.13-300.fc27       installed     2.1 M

Transaction Summary
================================================================================
Remove  3 Packages (+2 Dependent packages)

Installed size: 134 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction (shutdown inhibited)
  Erasing    : kernel-4.15.13-300.fc27.x86_64                               1/5
  Erasing    : kernel-modules-extra-4.15.13-300.fc27.x86_64                 2/5
  Erasing    : kernel-devel-4.15.13-300.fc27.x86_64                         3/5
  Erasing    : kernel-modules-4.15.13-300.fc27.x86_64                       4/5
  Erasing    : kernel-core-4.15.13-300.fc27.x86_64                          5/5
warning: file /lib/modules/4.15.13-300.fc27.x86_64/updates: remove failed: No such file or directory
  Verifying  : kernel-core-4.15.13-300.fc27.x86_64                          1/5
  Verifying  : kernel-4.15.13-300.fc27.x86_64                               2/5
  Verifying  : kernel-modules-extra-4.15.13-300.fc27.x86_64                 3/5
  Verifying  : kernel-modules-4.15.13-300.fc27.x86_64                       4/5
  Verifying  : kernel-devel-4.15.13-300.fc27.x86_64                         5/5

Removed:
  kernel.x86_64 0:4.15.13-300.fc27        kernel-core.x86_64 0:4.15.13-300.fc27
  kernel-devel.x86_64 0:4.15.13-300.fc27

Dependency Removed:
  kernel-modules.x86_64 0:4.15.13-300.fc27                                     
  kernel-modules-extra.x86_64 0:4.15.13-300.fc27                               

Complete!

Fedora 31

# dnf remove $(dnf repoquery --installonly --latest-limit=-2 -q)

Another method, to remove old kernels in CentOS, RHEL and Fedora set installonly_limit in /etc/yum.conf file. and next time update!
[root@huupv huupv]# cat /etc/yum.conf | grep installonly_limit=2
The output as below:
installonly_limit=2
The result, to keep two kernel as below:

Conclusion

Thought the aricle, you have removed old kernel. If you have any question then comment below:

Comments

Popular posts from this blog

zimbra some services are not running [Solve problem]

Bash script list all IP addresses connected to Server

How to install php7 on centos 6