Keeping your server secure and updated is one more step from keeping out malicious users.
Even if CentOS won't be the standard in Web Hosting for the next years, for now it's the most used OS so we are going to show you how you can remove old kernels from your system.
This practice will also keep your storage space free.
Your first step is to make sure you have the latest kernel installed.
If no update is needed then check if the latest kernel is loaded and if not restart your server.
uname -a
Linux xxxxx.xxxxxxxx.net 3.10.0-1160.25.1.el7.x86_64 #1 SMP Wed Apr 28 21:49:45 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
What we know from the response above is the loaded Kernel on your system, in order to be sure you have the latest installed, loaded check how many Kernels you have on your server and compare versions.
rpm -q kernel
kernel-3.10.0-1160.15.2.el7.x86_64
kernel-3.10.0-1160.25.1.el7.x86_64
kernel-3.10.0-1160.31.1.el7.x86_64
kernel-3.10.0-1160.36.2.el7.x86_64
As you can see, this system was just updated and there are already old kernels saved on our OS drive, this is why we will perform a server restart first (to make sure everything runs smooth) and then we will remove unused Kernels that we no longer need on our server.
Once your system is back you need to connect again via SSH and remove the unused Kernels
package-cleanup --oldkernels --count=2
We prefer to keep 1 extra kernel saved for …unwanted situations.