How to Safely Remove Old Kernels from Ubuntu.
The Linux kernel is the heartbeat of your Ubuntu system, acting as a bridge between the software and hardware components. Over time, as you update your Ubuntu system, new kernels get installed, leaving the older ones behind. Although these old kernels serve as a fallback if the new ones encounter issues, they can accumulate and consume valuable disk space. This comprehensive guide will walk you through the process of safely removing these old kernels from your Ubuntu system.
Understanding Linux Kernels
The Linux kernel is an essential part of all Linux distributions, including Ubuntu. It is the core of the operating system, managing system resources and facilitating communication between the user and the hardware of the computer. When the kernel gets updated, the older version remains in the system as a safety net, in case the new one fails or encounters problems.
Why Remove Old Kernels?
Over time, your Ubuntu system may end up with several old kernels. While these kernels can serve as a useful backup if a new kernel causes a system issue, they can also consume significant storage space. This can particularly be a problem on systems with limited storage or on systems where the /boot
partition has been configured with insufficient space. Therefore, it is beneficial to know how to remove these old kernels safely.
Identifying the Current Kernel Version
Before proceeding to remove older kernels, it’s crucial first to identify the currently active kernel version. You should never remove the kernel that your system is currently using.
You can check the version of the currently active kernel using the uname -r
command in the terminal:
uname -r
The output will display your current kernel version.
Listing Installed Kernels
Now that you have identified the current kernel, it’s time to see which other kernels are installed on your system. You can list all installed kernels using the dpkg --list
command:
dpkg --list | grep -E -i --color 'linux-image|linux-headers'
This command will list all installed kernels, including the currently active one.
Removing Old Kernels Using apt autoremove
The simplest method to remove old kernels from your Ubuntu system is by using the apt autoremove
command. This command automatically removes all packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed:
sudo apt autoremove --purge
This command will keep the latest kernel and the one other most recent kernel version on the system, removing all others.
Removing Specific Kernels Using apt remove
If you want to remove a specific kernel rather than all old kernels, you can use the apt remove
command. This command allows you to specify the exact kernel version you wish to remove:
sudo apt purge linux-image-[version] sudo apt purge linux-headers-[version]
Replace [version]
with the exact version number of the kernel you wish to remove.
Using GUI Tools for Kernel Removal
If you prefer a graphical interface over the command line, you can use GUI tools like Synaptic Package Manager to manage your kernels. Synaptic is a powerful package management tool that provides detailed information about installed packages, allows you to search packages using filters, and lets you select multiple packages for installation or removal.
To use Synaptic for kernel removal, first, install it using the following command:
sudo apt install synaptic
After installing Synaptic, you can launch it from the application menu. In Synaptic, you can search for ‘linux-image’ or ‘linux-headers’ to list all kernel-related packages. You can then select the old kernels you want to remove and mark them for complete removal.
Automatic Kernel Removal
Modern Ubuntu systems (version 18.04 and above) have the ability to automatically remove old kernels when updating to a new one. This feature is part of the unattended-upgrades package and is enabled by default. When a new kernel is installed, the system automatically marks the oldest one for removal.
Safeguard Measures
While removing old kernels can free up disk space, it’s crucial to do so carefully. Always keep at least one or two old kernels as a backup in case the current one fails. Also, never attempt to remove the currently active kernel.
Conclusion
Proper management of Linux kernels is essential for maintaining a healthy and efficient Ubuntu system. Although old kernels serve as a backup, they can consume valuable disk space if they accumulate. Knowing how to safely remove these old kernels can help optimize your system’s performance. However, always proceed with caution and ensure you keep at least a couple of old kernels as a safety net.
Remember, the command line is a powerful tool that can greatly impact your system. Always double-check your commands, especially when performing operations with root privileges. Happy kernel management!