[How To] Remove Old and Unused Kernels
|From time to time Ubuntu updates the GRUB menu, which may list a lot of previous Linux kernels and eat a lot of hard-disk space. In this article, we will discuss how we can remove old and unused kernels from our system safely and effectively.
- Check the kernel currently in use.
- List all the kernels installed.
- Remove the kernel parts.
- Update the GRUB configuration.
- Reboot.
Check the kernel currently in use.
It is important to know which kernel is currently in use before we proceed to remove any old kernels. To check what version of the kernel is currently in use, we can use the following command:
uname -r
List all the kernels installed.
In order to list all the kernels present in our system, we can use the following commands:
dpkg --list | grep linux-image
dpkg --list | grep linux-headers
dpkg --list | grep linux-modules
[adinserter block=”6″]
Remove the kernel and its parts.
Once we have a list of all the kernels present in our system, we can choose which kernel to remove. For example, I will remove kernel 4.15.0-135 and it’s all its components.
WARNING! Don’t forget to change the version number as required for your needs.
apt-get purge linux-headers-4.15.0-135-generic
apt-get purge linux-image-4.15.0-135-generic
apt-get purge linux-image-extra-4.15.0-135-generic
apt-get purge linux-modules-4.15.0-135-generic
apt-get purge linux-modules-extra-4.15.0-135-generic
Update the GRUB configuration.
The GRUB configuration needs to be updated in order for the changes to take effect. To update the GRUB configuration, we can use the following command:
sudo update-grub2
Reboot
Once we have updated the GRUB configuration, we can reboot the system to apply the changes. To reboot the system, we can use the following command:
reboot
By following the steps discussed in this article, we can safely and effectively remove old and unused kernels from our Ubuntu system. We should always keep our system up-to-date by periodically checking for and removing old kernels.
Test Environment
The commands discussed in this article were tested on an Ubuntu 20.04 LTS (Focal Fossa) system hosted on VMware Workstation 15.1.0.
Conclusion
Removing old and unused kernels from our Ubuntu system is a simple and straightforward process. With a few commands, we can easily list and remove old kernels from our system and reclaim disk space. It is a good practice to keep our system up-to-date by periodically checking for and removing old kernels.