LVM installs and encrypted installs use a small separate /boot partition. The small partition is capable of holding only four or five kernels, and fills to capacity quickly. To prevent /boot partition from getting full, you need to configure automatic removing of old kernels, or remove old kernels regularly manually, since automatic removing of old kernels is not enabled by default; see Bug #1357093.
Changing the kernel-containing packages on your system requires commands with root access, so please do read RootSudo.
Removing old kernels is easy to do on a properly-operating system. You can do it manually, or set unattended-upgrades to do it automatically. If you are receiving package management errors, regular maintenance may not work until the problem is fixed. See Problems below.
If your system is operating without error, you should be able to remove old kernels with a simple autoremove command in shell:
sudo apt-get autoremove --purge
The system keeps track of which kernels are older and marks them eligible for removal using this method; this also removes other automatically installed packages that (recursively) no manually installed package depends on. However, this works only, if you have installed the kernels automatically, or if you have used Sofware Updater for installing them in Wily or newer release; see Bug 1492709.
Alternatively, and to be able to remove even manually installed kernels, you could use
in Trusty or later; it works also in Precise, if you install byobu package from byobu PPA. Check that the presented set of packages to remove looks fine before ratifying the actual purge.
The unattended-upgrades package, included with the default install of all Ubuntu flavors, includes a setting to run autoremove automatically. Enabling this setting is a two-step process.
The first step is to enable unattended-upgrades. You can do this using the GUI or the shell:
Enable unattended upgrades using System Settings --> Software & Updates --> Updates Tab. Check the box for *-security (and/or any other repositories you wish) Automatically check for updates: Set to any frequency (except 'Never') When there are security updates: Set to Download and Install Automatically
sudo dpkg-reconfigure unattended-upgrades
The second step is to edit the file ls /etc/apt/apt.conf.d/50unattended-upgrades to enable completely automatic autoremove. It's owned by root, so remember to use sudo! Locate the line:
Uncomment the line AND change the value to "true"
Thereafter, system should remove old automatically installed kernels and packages associated to them - automatically. It does not purge then, however.
If your package management is broken, or if regular maintenance above is not working, any of several problems may have occurred. You may be out of storage space, or you may have a package version mismatch, or you may have another problem.
Safely removing old kernels
For users of LVM systems, encrypted systems or limited-storage systems, the most frequent problem is that the /boot partition is simply full. The package manager cannot install a pending upgrade due to lack of space. Besides, apt-get can not remove a package due to broken dependency.
This problem can be fixed quickly and easily from the shell. Simply identify one or two old kernels to remove manually, which will provide the package manager enough space to install the queued upgrade.
$ uname -r ## This command identifies the currently-running kernel 4.2.0-21-generic ## This is the current kernel. ## DO NOT REMOVE it! dpkg -l | tail -n +6 | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r) ## This command lists all the kernels excluding the booted ## kernel in the package database, and their status. rc linux-image-4.2.0-14-generic ## The oldest kernel in the database ## Status 'rc' means it's already been removed ii linux-image-4.2.0-15-generic ## The oldest installed kernel. Eligible for removal. ## Status 'ii' means Installed. ii linux-image-4.2.0-16-generic ## Another old installed kernel. Eligible for removal ii linux-image-4.2.0-18-generic ## Another old installed kernel. Eligible for removal ii linux-image-4.2.0-19-generic ## The previous good kernel. Keep iU linux-image-4.2.0-22-generic ## DO NOT REMOVE. Status 'iU' means it's not installed, ## but queued for install in apt. ## This is the package we want apt to install. ## Purge the oldest kernel package using dpkg instead of apt. $ sudo dpkg --purge linux-image-4.2.0-15-generic ## If the previous command fails, some installed package ## depends on the kernel. The output of dpkg tells the name ## of the package. Purge it first. ## Also purge the respective header package. $ sudo dpkg --purge linux-headers-4.2.0-15-generic ## Try also purging the common header package. $ sudo dpkg --purge linux-headers-4.2.0-15 ## Do not worry, if the previous command fails. $ sudo apt-get -f install ## Try to fix the broken dependency.
If the last command works without an error, continue to chapter Manual Maintenance to remove more kernels. Otherwise, if the last command still failed due to insufficient disk space in /boot, you have to purge another kernel same way. Many variations on this strategy exist. You can remove *all* the older kernels using dpkg. Some people have even scripted the process. Use a method that makes sense to you *and* to the package manager.
Warning: Do NOT use the 'rm' command to delete files that were placed by the package manager, including kernel files. It merely creates a new headache for you to solve when the package manager cannot remove packages due to 'file not found'. Always use the package manager to remove files that were placed by the package manager.
Unmet Dependency Errors
If you have ignored apt and dpkg errors for a while, then some packages may have upgraded while others did not. This is an expected follow-on effect. You will see mysterious, persistent version errors upon normal upgrades.
The simple way to fix most version mismatch errors is to update the package database, clean out the package cache, and download-and-reinstall the newer version of the offending package.
This is easier than it sounds. For example, if the 'hello' package is one of those unmet dependencies:
$ sudo apt-get update ## Update the package database $ sudo apt-get clean hello ## Delete the hello package from the local cache $ sudo apt-get install --reinstall hello ## Download and reinstall the latest version of hello
Oops, removed all kernels!
If you got a bit carried away and deleted all the kernels, you fall into the "You did WHAT?!?!" class. You will be pleased to know that you are not the first, nor will be the last, person to do this. Get your LiveCD and head over to GRUB reports no operating system.
Cryptic Shell Commands
There are various cryptic shell incantations floating around help sites and search engines that promise to remove older kernels. Copy-and-paste mysterious incantations into your system at your own risk.