Is there a way to setup ubuntu so that when a new kernel update is installed that it will remove the latest kernel +2? That is not my current kernel not the new kernel but the now useless older kernel. So that I only ever have 2 kernels new and stable.
Can this be set as a default in apt?
It isn't possible to set APT to do this.
This is part design choice, part limitation.
Ubuntu designates each new kernel as a different package name (the package name contains the kernel version) so APT treats them as separate packages. When they want you to download and install a new package, they create the new kernel package, and then point the generic kernel package (eg
linux-kernel-generic
) to depend on that new package. This is what causes the new kernel package to be installed.Now, it is (or at least, traditional wisdom says it is) generally a good idea to keep your previous kernel installed when installing a new one, in case there is any incompatibility or bug in the new one. This allows you to switch kernels from the boot loader. So Ubuntu do not mark the new kernel packages as "replacements" of the old ones, which means APT will simply leave the old ones installed.
There is no mechanism in APT or the Ubuntu repositories to specify "keep the last two kernel packages". There could be something like this hacked in, but it may not play very well with all situations, such as when people skip a kernel version.
Now, while I said this isn't possible to do in APT, there may exist third party software tools that can help you with this, if not on an automatic basis.
There is a tool called Ubuntu-tweak which is not available in Ubuntu, but can be installed from a PPA. This tool has a "computer janitor" feature that, among other things, helps you uninstall old kernels.
Or, you can simply use Synaptic. Synaptic makes it pretty easy to list installed kernels (search for packages containing
linux-image
) and from there you can uninstall them easily.This command has been posted in several locations:
See http://chr4.org/blog/2013/08/04/apt-get-cleanup-commands/