The Safest Way To Remove Old Kernels In Ubuntu
The Ubuntu Kernel team releases new Kernel updates, bug fixes, and security patches every month. If you are a long-time Ubuntu user, and regularly updating your Ubuntu system, then it might be accumulated with lot of unused and old Kernels. So, there are chances that the /boot partition in your hard disk will run out of disk space over a period of time. If your /boot partition is full, you can’t upgrade the system or you can’t even upgrade your Kernel to any latest available version. This is no good, and it is very important to regularly update your Kernel and software. To do so, you should have enough free space in the /boot partition. This is why it is always a good practice to remove unused Kernels, unnecessary packages, dependencies, and system cache periodically. Keep in mind that in order to function Ubuntu system properly, you must have enough free space in /boot and /root partitions.
Of course, We can easily wipe out the system cache, unneeded packages and dependencies using commands:
$ sudo apt-get autoremove
$ sudo apt-get autoclean
But the problem is this works only if you have installed the kernels automatically, or if you have installed them using Software updater. These commands will not remove manually installed kernels. However, you can use some third-party utilities such as Ubuntu Tweak to remove obsolete Kernels, packages, and dependencies which are no longer necessary. But Ubuntu Tweak is a GUI utility, so we can’t use it servers that has no Graphical mode.
No worries, there is an another utility called ‘purge-old-kernels’ to safely remove old Kernels from your Ubuntu systems. This utility is created by Dustin Kirkland from Ubuntu Product and Strategy team. It will safely remove the old and unused Kernels and header packages which are not necessary anymore from your Ubuntu desktop and server.
Remove Old Kernels In Ubuntu using purge-old-kernels utility
The purge-old-kernels utility comes preinstalled with Ubuntu 16.04 LTS and newer versions by default. In older versions, you have to install ‘byobu’ or ‘bikeshed’ to use this utility.
$ sudo apt-get install bikeshed
$ sudo apt-get install byobu
To remove the old Kernels safely, just run:
$ sudo purge-old-kernels
Reading package lists... Done Building dependency tree Reading state information... Done The following package was automatically installed and is no longer required: linux-headers-4.4.0-21 Use 'sudo apt autoremove' to remove it. The following packages will be REMOVED: linux-headers-4.4.0-21-generic* linux-image-4.4.0-21-generic* linux-image-extra-4.4.0-21-generic* 0 upgraded, 0 newly installed, 3 to remove and 0 not upgraded. After this operation, 224 MB disk space will be freed. Do you want to continue? [Y/n]
As you see in the above output, the old kernels have occupied 224 MB in my Ubuntu 16.04 server. Press ‘Y’ to get rid of the unused Kernels to free up some space.
If there is no old Kernels in your system, this utility will display the following message.
No kernels are eligible for removal
Obviously, it will never remove the currently running kernel. By default, it will keep at least two latest kernels, but the user can override that value using the —keep parameter.
For example, to remove all old kernels except 3 latest Kernels, run:
$ sudo purge-old-kernels --keep 3 -qy
For more details, refer man pages.
$ man purge-old-kernels
Important: It is highly recommended to keep at least two Kernels (Current Kernel and one old Kernel). In case, you had any problems, such as hardware or software compatibility issues with the current Kernel, you can immediately switch back to the old Kernel. This way you can still have a chance to fix what could be the problem with the current Kernel. So, do not remove all old Kernels. It is always good practice to have at least one old Kernel for any emergency backup.
- Linux Kernel Utilities – Scripts To Compile And Update Latest Linux Kernel For Debian And Derivatives
And, that’s all for now. Hope this guide helps. If you find our guides useful, please share them on your social, professional networks and support OSTechNix.
More good stuffs to come. Stay tuned!