Reading Time: 4minutes
If you administer CentOS 7 / CentOS 8 bunch of servers it is very likely after one of the scheduled Patch days every 6 months or so, you end up with a multiple Linux OS kernels installed on the system.
In normal situation on a freshly installed CentOS machine only one rpm package is installed on the system with the kernel release shipped with CentOS / RHEL / Fedora distro:
The reason to remove the old unused kernels is very simple, you don't want to have a messy installation and after some of the updates to boot up in a revert back old kernel or if you're pedantic to simply save few megas of space.
Some people choose to have more than one kernel just to make sure, if the new installed one doesn't boot, after a restart from ILO / IDRAC remote console interface you can select to boot the proper kernel. I agree having the old kernel before the system *kernel* upgrade as backup recovery is a good thing but this is a good thing to the point the system gets booted after reboot (you know we sysadmins usually after each major system package upgrade), we like to reboot the system warmly praying and hoping it will boot up next time 🙂
1. Remove CentOS last XX kernels from the OS
Of course removal of old kernels could be managed by a simple
yum remove kernel
One more than one kernel is present you can hence leave only lets say the last 2 installed kernel on the CentOS host (some people prefer to have only one) but just for the sake of having a backup kernel I like more to have last two kernels installed present, to do so run package-cleanup which is contained in yum-utils rpm package CentOS – this is CentOS / Redhat ( RHEL) specific command.
[root@centos ~ ]:#package-cleanup –oldkernels –count=2
–count=number argument – tells how many from the latest version kernels to get removed.
Note if you don't have the package-cleanup command install yum-utils package:
[root@centos ~ :]# yum install -y yum-utils
2. RemoveOld kernels from Fedora Linux – leave only the latest 3 installed
This is done with dnf by setting the –-latest-limit arg to negative value to how many last kernels want to keep
[root@fedora ~ ]:# dnf remove $(dnf repoquery –installonly –latest-limit=-3 -q)
3. Set how many kernels you want to be present on system all the time after package upgrades
It is possible to tell CentOS / RHEL / Fedora's on how many kernels show be kept installed on the system, the default configured on Operating system install time is to keep the last 5 installed kernel on the OS. This is controlled from installonly_limit=5 value that is usually as of year 2020 RPM based distributions found under /etc/yum.conf (on CentOS / RHEL) and in /etc/dnf/dnf.conf (in Fedora) configuration file and sets the desired number of kernels present on system after issuing commands yum upgrade / dnf upgrade –refresh etc.
The minimum number to give to installonly_limit is 2.
4. Remove orphan rpm packages from server
The next thing to do is to check the installed orphan packages to see if we can safely remove them; by orphaned packages we mean all packages which no longer serve a purpose of package dependencies.
Orphan packages are packages who left over from some old dependencies that are no longer needed on the system but just take up space and impose a possible security risk as some of them might end up with time with a public well known and hacked CVE vulnearbility.
Let me try to explain this concept with a quick example: package A is depended on package B, thus, in order to install package A the package B must also be installed. Once the package A is removed the package B might still be installed, hence the package B is now orphaned package.
Here’s how we can safely see the orphan packages we do have on our system:
[root@centos ~ :]# package-cleanup –quiet –leaves –exclude-bin
And here’s how we can delete them:
[root@centos ~ :]# package-cleanup –quiet –leaves –exclude-bin | xargs yum remove -y
The above commands should be launched multiple times, because the packages deleted with the first batch could create additional orphan packages, and so on: be sure to perform these tasks until no orphan packages appear anymore after the first package-cleanup command.
5. Delete Old Kernels and keep only last three ones on Debian / Ubuntu Linux
To do the same on a debian based distribution there is a command is provided by a deb package byobu, if you want to clean up old kernels on Debians :
$ sudo purge-old-kernels –keep 3
That's all folks enjoy ! 🙂