Removing old kernels

On Debian/Ubuntu we would need to search for installed kernel packages, something like this:

root@server:~# dpkg --list |grep linux-image
ii  linux-image-3.2.0-23-generic         3.2.0-23.36                                      Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-56-generic         3.2.0-56.86                                      Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-57-generic         3.2.0-57.87                                      Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-58-generic         3.2.0-58.88                                      Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-59-generic         3.2.0-59.90                                      Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-60-generic         3.2.0-60.91                                      Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-61-generic         3.2.0-61.93                                      Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-63-generic         3.2.0-63.95                                      Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-64-generic         3.2.0-64.97                                      Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-65-generic         3.2.0-65.99                                      Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-67-generic         3.2.0-67.101                                     Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-server                   3.2.0.67.79                                      Linux kernel image on Server Equipment.

Then we would want to see which kernel we are running:

root@server:~# uname -a
Linux imi01 3.2.0-61-generic #93-Ubuntu SMP Fri May 2 21:31:50 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

Then we would remove all linux-image packages except for the exact version running and the latest one (theese may differ if you are running a mission critical server and you have not restarted since the laste upgrade).

root@imi01:~# apt-get remove --purge linux-image-3.2.0-23-generic linux-image-3.2.0-56-generic linux-image-3.2.0-57-generic linux-image-3.2.0-58-generic linux-image-3.2.0-59-generic linux-image-3.2.0-60-generic linux-image-3.2.0-63-generic linux-image-3.2.0-64-generic linux-image-3.2.0-65-generic
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
  linux-image-3.2.0-23-generic* linux-image-3.2.0-56-generic* linux-image-3.2.0-57-generic* linux-image-3.2.0-58-generic* linux-image-3.2.0-59-generic*
  linux-image-3.2.0-60-generic* linux-image-3.2.0-63-generic* linux-image-3.2.0-64-generic* linux-image-3.2.0-65-generic*
0 upgraded, 0 newly installed, 9 to remove and 0 not upgraded.
After this operation, 1,349 MB disk space will be freed.
Do you want to continue [Y/n]?

On RHEL/CentOS it is even more simple, we need to install yum-utils

[root@server ~]# yum install yum-utils
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.mirrors.linux.ro
 * epel: ftp.ines.lug.ro
 * extras: centos-mirror.cyberhost.ro
 * updates: centos.mirrors.linux.ro
base                                                                                        | 1.1 kB     00:00
epel                                                                                        | 3.7 kB     00:00
extras                                                                                      | 2.1 kB     00:00
updates                                                                                     | 1.9 kB     00:00
updates/primary_db                                                                          | 536 kB     00:00
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package yum-utils.noarch 0:1.1.16-21.el5.centos set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================
 Package                   Arch                   Version                               Repository            Size
===================================================================================================================
Installing:
 yum-utils                 noarch                 1.1.16-21.el5.centos                  base                  74 k

Transaction Summary
===================================================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 74 k
Is this ok [y/N]:

then we run the following command, that takes care of everything:

[root@server ~]# package-cleanup --oldkernel
Setting up yum
I will remove the following 3 kernel related packages:
kernel-2.6.18-371.8.1.el5
kernel-2.6.18-371.1.2.el5
kernel-2.6.18-371.el5
Is this ok [y/N]:

For more information about package-cleanup utility check Fedora Documentation.

Leave a Reply

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box
Name *
Email *
Website