Canonical provides regular updates for Ubuntu 18.04 system, including kernel updates to manage system resources in the best possible way.
When you install a new kernel, the old ones are not removed because it can help you get started if you make a mistake with the new one or for some other reason. Note that the old kernel consumes some spaces that can be useful for something else.
Check old cores
Before starting to remove those old kernels from the system, we must know our current version of this, for this we must open a terminal and execute the following command:
uname -r
Already knowing the version of the most current Kernel, now we are going to know the old kernels which we can list with the following command:
dpkg -l | grep -E 'imagen-linux- [0-9] +' | grep -Fv $ (uname -r)
This will exclude the most current.
You can see that you do have an old kernel installed marked ii. You can see more old kernels if you have done any manual updates or installs.
De this command we will be able to appreciate the following:
- ii: means that the kernel and packages are installed can be installed
- rc: indicates that the kernel has already been removed.
- UI: it is something like a warning tells you DO NOT REMOVE. That means it is not installed but is queued for installation.
Removing old kernels
There are some useful commands to remove old kernels, whether you installed them manually or directly through system updates.
These are usually installed through regular system updates, but for some reason they may have decided to do a manual installation.
Already knowing the old kernels and the current one, we can safely proceed to eliminate those we no longer need.
For this we are going to use apt to be able to remove the old kernels. We only have to execute in the terminal:
sudo apt remove linux-image-4.xx.xx.
It is process what we must do one by one since we could have errors with the grub later.
Now we must rebuild the Grub with:
sudo update-grub
Now you can check again if it has been removed:
dpkg -l | grep -E 'imagen-linux- [0-9] +' | grep -Fv $ (uname -r) rc linux-image-4.xx.xx.x-generic 4.xx.x-xx.xx amd64 Imagen del núcleo de Linux para la versión 4.xx.xx.x en 64 bit x86 SMP
They can see the rc status showing it has been removed. You can also check in the boot menu and see that only the current boot kernel appears.
Done this we must remove all those packages that were installed automatically to satisfy the dependencies of other packages and they no longer need to be orphaned and are only generating a consumption of space on our disk.
sudo apt autoremove --purge
As well We can verify all those Kernels that have been installed manually or automatic this can be separated with the following commands.
Can verify the ones that were installed automatically as follows:
sudo apt-mark showauto 'linux-image -. *'
And this will return all those Kernels that were installed automatically.
linux-image-4.15.0-13-generic
While for those that we install manually, we can know them with the command:
sudo apt-mark showmanual 'linux-image -. *'
How to remove old kernels graphically?
Commonly performing this process is usually preferred through a graphical interface and with the help of a program. So for this work we have several tools that can help us in this.
The first one and the most used is Ubuntu Cleaner in it we will be able to select and eliminate all those kernels of the system that we consider obsolete.
To install this tool we can do it by adding its repository to the system with:
sudo add-apt-repository ppa:gerardpuig/ppa
We update the list of packages and repositories with:
sudo apt-get update
And finally we install the tool with:
sudo apt-get install ubuntu-cleaner
Once installed, we proceed to open the application and in the system section, it will show us the Kernels that we can eliminate, select them, and clean.
Another way to carry out this process is with Synaptic, in it we position ourselves in "Modules and Kernel" and we can select which Kernel to uninstall.
Only here we must be careful not to eliminate the one we have in use.
Good tip that ubuntu-cleaner, I did not know. I checked the repository and it always stays up to date. As it is an execution that is carried out from time to time, learning the commands is complicated, doing it through Ubuntu Cleaner is the most practical and safe with just a few clicks. Thanks a lot.
for me it's better this way
$uname -r
$dpkg --list | grep linux-image
$ sudo apt-get install byobu
$ sudo purge-old-kernels
leaves you the last two kernels….
Very clear explanation. Excellent work!. Keep it up!.
hi, i'm very new to linux, i have xubuntu installed, and i saw this article,
as I do not know the correct syntax for all this that they explain I copied and pasted in the terminal
dpkg -l | grep -E 'linux-image- [0-9] +' | grep -Fv $ (uname -r)
it returns the following error message to me;
bash: syntactic error near unexpected element `('
I had no problem identifying the version:
$uname -r
4.15.0-112-generic
If you can help me with this I appreciate it, maybe I do not have any other kernel since I installed it less than a month ago. This sums up my brief experience with Linux.
Thank you very much.
Eliminate the space between $ and (
A greeting.