Why doesn't Ubuntu remove old kernels automatically?

I wonder why Ubuntu won’t remove the old kernels automatically.

Surely nothing is perfect and things can go wrong, so in case a update doesn’t work, having a backup kernel maybe great. But it would also suffice to just keep the latest kernel and the one before the latest and delete all the older ones.

Is there a reason why Ubuntu won’t do this automatically?.

Asked By: Registered User

||

Ubuntu only auto-removes items that are no longer needed or are a security risk…So I assume that the reason for this is security purposes. Lets say for some odd reason a new kernel becomes insecure…Then you would be rerouted back to the old kernel temporarily while the new one is being fixed. Also new kernels can come with changes so therefore some kernels are opt-in for example updating 14.04.1 to 14.04.2 you can opt-in for a new kernel, but this April when the new LTS version 15.04 comes out you will automatically have you kernels updates. So I reckon that its for security purposes.

but if you want to remove it try running this command line

sudo apt-get autoremove

This should remove any unneeded software such as your old kernel if not try

sudo apt-get remove (kernels-name)
Answered By: Tactux

Is there a reason why Ubuntu wont do this automatically?

I can see only 1 reason: it does not work flawlessly; there is no clean way to currently decide what the definition of "old kernels" is. "old" does not mean "unused" nor does it mean "unwanted". And any mistake in this will kill a users’ machine.

So up to now the manual method is preferred since this puts the power of removing into the users’ hands.


Resources for this conclusion:

Ubuntu WIKI: Proposal for removing old kernels

Last-good-boot is implemented fully in Intrepid/8.10 final, however it has been disabled because it was not considered stable enough. The setting is a single line in the file /etc/default/kernel-helper-rc.


Launchpad: Should aptitude provide a way to remove old kernel versions ?

apt-get has an autoremove feature that uninstalls all packages that are not needed as dependencies and have not been installed manually. Since Ubuntu 14.04 all obsolete kernels and headers should automatically be flagged as no more needed, and thus can be purged with the apt-get autoremove command. (There are reports that this does not yet fully work). I am not aware of a feature in aptitude that is similar to apt-get’s autoremove.


Ubuntu-devel: Distro-provided mechanism to clean up old kernels

While agreeing that it would be quite helpful and seems appropriate to have the
cleanup automatic, there is a slight potential pitfall (or two). There are
various flavours of kernels and people may or may not deliberately have those
installed in parallel. Also various releases had sometimes a changing set of
depending packages. For a while this should be only linux-backports-modules
(there had been linux-ubuntu-modules and linux-restricted-modules). Though this
is not so much of a problem.
From a pattern matching point of view the generic-pae kernels are a bit of a
pain as they tend to ruin the "use the last part of a split by "-" for the flavor".
But anyway, I think the main issue is the various flavours, so a cleanup that is
automatic should retain the last three of each, even though this may tend to
leave more kernels around.

Answered By: Rinzwind

There are two things going on here:

  1. New kernels are completely new packages, not updated packages with a newer version number, so installing a new kernel image doesn’t replace any older ones. They coexist.

  2. Ubuntu uses a bunch of apt magic to protect the last two kernel versions from ‘apt-get autoremove’ (the latest one, and the one that was last booted). See /etc/apt/apt.conf.d/01autoremove-kernels for details.

What this all means is that once you’ve rebooted into the latest kernel, ‘apt-get autoremove’ should remove all but the current and last booted kernel packages.

Old kernels still accumulate in /boot because autoremove is not enabled in Ubuntu by default – you must either run it manually every so often, or you must enable it.

This may change in 16.04 – a bugfix to the unattended-upgrades package will enable autoremove of old kernels by default.

Answered By: andrewdevans

The simple-minded way to remove old kernels is to use ubuntu-tweak. You select the Janitor tag and tick the Old Kernel box. That gives you a list of kernels it thinks you don’t need any more. You can select them all and click Clean and, after a while, the job’s done. Being of a nervous disposition I checked which kernel I was running using uname -a but it didn’t figure on the list. This worked on my wife’s laptop (which has a rather small boot partition and was showing warnings) and mine (which was working fine). Does everyone agree that this is safe?

Answered By: Chris_Dorking

This script will do the job. It ll remove all kernels that are not in use.

sudo dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/(.*)-([^0-9]+)/1/")"'/d;s/^[^ ]* [^ ]* ([^ ]*).*/1/;/[0-9]/!d' | xargs sudo apt-get -y purge

Other then that, not removing kernels must be a fairly new thing. I have one Ubuntu 14 system after the other running into this issue of a full boot partition due to old kernel garbage.

It can make the life of less experienced users extremely complicated. It is actually a noob trap. Canonical should fix this.

Answered By: psnizek

I recommand use Ubuntu Tweak to remove old kernels. look here:

How do I remove old kernel versions to clean up the boot menu?

Answered By: skonsoft

I think the latest release of Ubuntu 15.10 could remove old kernels automatically, if you edit /etc/apt/apt.conf.d/50unattended-upgrades file:
Change line

//Unattended-Upgrade::Remove-Unused-Dependencies “false”;

to

Unattended-Upgrade::Remove-Unused-Dependencies “true”;

I haven’t tested this, though.

Answered By: jarno

I had a problem with being unable to install software. Apt-get was unable to install linux-headers, reporting the disk was full.$ df -i reported that IUse% was at 100%: The disk had free space but had run out of inodes. $ sudo dpkg --configure -a failed, & so did $ sudo apt-get -f install and $ sudo apt-get autoremove

The problem solved when I manually removed several of the older kernel folders from /usr/src – just $ rm or shift-delete the folders. This freed up enough inode space to allow $ apt-get -f install to complete.

After that I had only 10 linux kernels. I ran $ sudo apt-get autoremove which deleted 1.4 GB of files. Which begs the question: Really?? Really?? But honestly, is the this the right way to run a user friendly system?

Answered By: Gareth
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.