Why doesn't Ubuntu remove old kernels automatically? [duplicate]
Solution 1:
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.
Solution 2:
There are two things going on here:
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.
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.
Solution 3:
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.
Solution 4:
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?
Solution 5:
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)