Since release 18.04, Ubuntu only keeps the last three kernels in /boot and deletes the older kernels at each kernel upgrade. It keeps the latest kernel and the two previous versions that were installed and removes all the rest. To do that manually on any Ubuntu version you can run:

sudo apt --purge autoremove

If you are on a newer Ubuntu version, the above command will most likely not remove any additional kernels, since by default Ubuntu has already removed the older ones and left the latest three kernels. It can remove other packages that are left on the system and are unused, but that will not help much in the cases where you don’t have enough space in /boot.

So, if you do not have enough space in /boot, read on.

If you had installed Ubuntu a while back the /boot partition might be a bit too small. For example one of my old laptops only has a 200MB boot partition. That means that every time I have a kernel upgrade, it fails due to insufficient space. In order to be able to install the new kernel, I need to purge all previous kernels and only leave the one that it is currently booted into. Here is a one line command to delete all unused Linux kernels:

echo $(dpkg -l | grep linux-image | awk '{ print $2 }' | sort -V | sed -n '/'`uname -r`'/q;p') $(dpkg -l | grep linux-headers | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p') | xargs sudo apt-get -y purge

Note: A special caution is due here- run this at your own risk.

The above command lists all installed Linux kernels and Linux kernel headers, then it excludes the version that is currently running on the system and then pipes that into the purge command, which goes though all the packages in that list and removes them.

If you are not absolutely sure how that works, I recommend doing a “dry run” first. To do that, run this command to find out your currently running Linux kernel:

uname -r

This will give you the version of the currently running kernel. Make a note of it.

Next, run this command, which will list all the kernels and kernel headers that will be removed:

echo $(dpkg -l | grep linux-image | awk '{ print $2 }' | sort -V | sed -n '/'`uname -r`'/q;p') $(dpkg -l | grep linux-headers | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p')

Now go through that list and make sure that the version that you are currently running (the one from the output of “uname -r”) is not in that list.

If you want to be extra, extra careful, you can omit the “-y” flag from the apt-get command, which will ask you to confirm the removal of each an every kernel before deleting it:

echo $(dpkg -l | grep linux-image | awk '{ print $2 }' | sort -V | sed -n '/'`uname -r`'/q;p') $(dpkg -l | grep linux-headers | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p') | xargs sudo apt-get purge

Now you should have enough space freed up in /boot to upgrade the kernel.

Ubuntu / Debian – Remove all unused Linux kernels
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *

*