Keeping your Linux system clean could be a tedious and intimidating task, but if you know the right tools this could turn into a fun activity. In this article, I am going to share the best way to remove old unused kernels from your CentOS or Red Hat Linux servers.
While updating my CentOS Linux server, I encountered the problem of space in boot partition. Yes, I have created only 400M boot partition and please don’t criticize me for that as I don’t like to waste the space.
Transaction check error:
installing package kernel-3.10.0-1062.12.1.el7.x86_64 needs 3MB on the /boot filesystem
Error Summary
-------------
Disk Requirements:
At least 3MB more space needed on the /boot filesystem.
[root@cent-server ~]# df -hP /boot
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 397M 367M 30M 93% /boot
I went straight ahead to delete some of the files present in /boot/, but that is not advised and not a good way to clean /boot partition as it contains important files to boot your system. Also, deleting “initramfs, vmlinuz, System.map” and other related files manually is not the ideal solution.
So, what is the best and safest way to clean up boot partition?
The best way to remove unused kernels is to remove the kernel package through the yum command as it removes all the related files and dependencies.
The other best option is to use the in-built command “package-cleanup” which comes by default with the yum-utils package. This command will remove more than 1 kernel package without your intervention again and again.
Let us first know what is package-cleanup command and what options we are going to use. So, “package-cleanup” is a command which cleans up locally installed, duplicate or orphaned packages from the system.
This command can also be used for searching duplicate packages or any problem in the servers’ local RPM database. So I will recommend you to check its man page.
I am going to use the options “–oldkernels” and “–count” for this article and have listed their use below.
package-cleanup - clean up locally installed, duplicate, or orphaned packages --oldkernels Remove old kernel and kernel-devel packages. --countNumber of duplicate/kernel packages to keep on the system (default 2)
Now, first of all run below command and find out all the kernels installed in your Linux server. You can also check /boot, but this is the easiest command to do that.
[root@cent-server boot]# rpm -qa kernel --last kernel-3.10.0-1062.9.1.el7.x86_64 Wed 15 Jan 2020 02:40:36 AM GMT kernel-3.10.0-1062.4.3.el7.x86_64 Fri 29 Nov 2019 06:24:43 PM GMT kernel-3.10.0-1062.1.2.el7.x86_64 Mon 21 Oct 2019 02:54:19 PM BST kernel-3.10.0-957.27.2.el7.x86_64 Tue 17 Sep 2019 11:00:09 AM BST kernel-3.10.0-957.el7.x86_64 Mon 16 Sep 2019 04:18:13 AM BST
Now as you can see, I have 5 different kernel versions installed and want to keep only the latest 3 kernel versions, so I will be using “-count=3”.
Below is the command and its output.
[root@cent-server]# package-cleanup --oldkernels --count=3 Loaded plugins: fastestmirror, langpacks --> Running transaction check ---> Package kernel.x86_64 0:3.10.0-957.el7 will be erased ---> Package kernel.x86_64 0:3.10.0-957.27.2.el7 will be erased --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================ Package Arch Version Repository Size ============================================================================================================ Removing: kernel x86_64 3.10.0-957.el7 @anaconda 63 M kernel x86_64 3.10.0-957.27.2.el7 @updates 63 M Transaction Summary ============================================================================================================ Remove 2 Packages Installed size: 126 M Is this ok [y/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Warning: RPMDB altered outside of yum. Erasing : kernel.x86_64 1/2 Erasing : kernel.x86_64 2/2 Verifying : kernel-3.10.0-957.el7.x86_64 1/2 Verifying : kernel-3.10.0-957.27.2.el7.x86_64 2/2 Removed: kernel.x86_64 0:3.10.0-957.el7 kernel.x86_64 0:3.10.0-957.27.2.el7 Complete! [root@cent-server]#
The above output shows the old kernel versions have been removed, but you can also verify it by again running the “rpm -qa kernel –last” command and also verifying our boot partition.
[root@cent-server ~]# rpm -qa kernel --last kernel-3.10.0-1062.9.1.el7.x86_64 Wed 15 Jan 2020 02:40:36 AM GMT kernel-3.10.0-1062.4.3.el7.x86_64 Fri 29 Nov 2019 06:24:43 PM GMT kernel-3.10.0-1062.1.2.el7.x86_64 Mon 21 Oct 2019 02:54:19 PM BST [root@cent-server ~]# [root@cent-server ~]# df -hP /boot Filesystem Size Used Avail Use% Mounted on /dev/sda1 397M 274M 124M 69% /boot
So this is the best way to remove the old unused kernels from your Red Hat or CentOS Linux servers without worrying.
I hope you have learned something new today and if you are using any different technique to remove the unused kernels, please let me know through your comments. Also, do not forget to share your views about this article and share it with your friends.