Every time I install a new Linux kernel, it gets left in the grub_config, making the boot menu longer each time.
I know I can manually search through the installed packages and remove them.
Does Ubuntu provide any easier way to clean them up or keep them from showing in the boot list?
16.04 and newer versions of Ubuntu
This command removes packages that were automatically installed to resolve a dependency, but are now no longer depended on. This includes old versions of
linux-headers-*
andlinux-image-*
. (It’s also smart about this process, leaving one spare version of the kernel around as a fallback!)11.10 and newer versions of Ubuntu
GRUB2 and its display of all kernels
The latest versions of Grub2 installed in Ubuntu automatically display the latest kernel and hides the older kernels that you may have installed.
If you do not see your grub - then remember to press Shift whilst booting.
As you can see, only the latest kernel is displayed.
If you select the option shown (press Enter) then all the old kernels become visible and available to boot from.
How to permanently delete older kernels
First boot with the latest available kernel.
There are a number of ways to delete old kernels. Personally, I wouldn't touch Computer Janitor since this is acknowledged to break your computer with its suggestions.
synaptic
An alternative is Synaptic (
sudo apt install synaptic
)search for linux-image, right-click a kernel and choose complete removal and finally click the Apply button to delete the kernel.
Repeat the search but this time for linux-header - you can delete the associated headers for the kernel image chosen previously.
Synaptic though will not attempt to verify what you are trying to remove... you could inadvertently delete your newest kernel - or even delete all of your kernels via this tool leaving you with an unbootable Ubuntu!.
Remember to check which kernel you are using type:
The result would be similar to:
Remember the result and the number - make sure you don't delete the corresponding image or header.
Recommendation
My recommendation is to keep at least two or preferably three kernels including the latest. The reason for the recommendation is that you will have at least one/two other kernels to boot with, if for what-ever reason the latest kernel you are unable to boot with or introducing a regressed capability such as broken wireless.
First, reboot your System to be sure it is using the latest kernel. Then open terminal and check your current kernel:
DO NOT REMOVE THIS KERNEL!
Next, type the command below to view/list all installed kernels on your system.
Find all the kernels that lower than your current kernel. When you know which kernel to remove, continue below to remove it. Run the commands below to remove the kernel you selected.
Finally, run the commands below to update grub2
Reboot your system.
My one-liner to remove old kernels (this also frees up disk space)
Explanation (remember,
|
uses the output of the previous command as the input to the next)dpkg --list
lists all installed packagesgrep linux-image
looks for the installed linux imagesawk '{ print $2 }'
just outputs the 2nd column (which is the package name)sort -V
puts the items in order by version numbersed -n '/'`uname -r`'/q;p'
prints the lines before the current kernelxargs sudo apt-get -y purge
purges the found kernelsUnwinding the
sed
invocation:-n
tellssed
to be quiet`uname -r`
outputs the current installed kernel release - we include it in backticks so that the output is includes as part of the command (you might also see this as$(uname -r)
/something/q
says stop when you match 'something' (in this case, something is output ofuname -r
) - the/
surround a regular expressionp
is print;
is the command separtor, so/something/q;p
says quit when you match something, else printaltogether,
sed -n '/'`uname -r`'/q;p'
is print the lines until it matches with the current kernel name.If you're paranoid (like me), you can make the last part
xargs echo sudo apt-get -y purge
so that the command to purge the old kernels is printed, then you can check that nothing unexpected is included before you run it.Modified version to remove headers:
Note: the
sed
invocation is modified."$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"
extracts only the version (e.g. "3.2.0-44") , without "-generic" or similar fromuname -r
All-in-one version to remove images and headers (combines the two versions above):
Ubuntu 16.04+:
Ubuntu 15.10 and below:
I find this to be the easiest and quickest way. It keeps the latest kernel as well as two more:
To change the number of additional kernels that are kept:
Removing Entries from Grub 2 Entries should be removed by editing or removing files in the /etc/grub.d folder. The /boot/grub/grub.cfg file is read-only and should not normally require editing.
Too Many Kernels?
If you are not sure of the kernel you are currently using, in a terminal type uname -r.
Kernels removed via APT (Synaptic, "apt-get remove", etc.) will automatically update grub.cfg and no user action is required.
A great tool for removing kernels (and menu entries) is Ubuntu-Tweak, a safe and easy-to-use GUI app.
Install ubuntu tweak
Ubuntu-Tweak will be available under Applications > System Tools.
Remove Older Kernel Entries
Select "Package Cleaner" on the left and "Clean Kernel" from the right panel.
Press the "Unlock" button at the lower right, enter your password.
Select from the displayed list the kernel images and headers you wish to remove. The kernel in use is not listed.
Press the "Cleanup" button at the lower right to remove the selected kernel images and headers.
Remove Operating Systems from the Grub menu
Other Operating Systems which have been removed from the computer will also be removed from the menu once "update-grub" is run as root.
Menu items are placed on the Grub2 menu by scripts. If you don't want other Operating Systems to be entered in the menu, disable /etc/grub.d/30_osprober
Run this command to stop the script from running
sudo chmod -x /etc/grub.d/30_os-prober
DISABLE_30_OS-PROBER='true' in /etc/default/grub
Remove Memtest86+ from the Grub Menu
sudo chmod -x /etc/grub.d/20_memtest86+
Source
Note: After kernel updates a new entry is added to the GRUB menu.You can remove the older one if you want.However, most experienced users will advise you to keep at least one spare entry in case something goes wrong with an upgrade and you need to boot an older kernel version for troubleshooting purposes.
Alternate way to remove Kernel entries (prior to 10.04)
for GRUB not GRUB2
startupmanager
You can find it under System>>Administration>>
You see in the second screenshot you can select how many kernels to show? I generally just keep it on 1, but when I get a kernel upgrade I always change it to 2 before restarting so I can select the older kernel if the new kernel has problems with my hardware. Once I know the new kernel is working well I change it back to 1.
Purely commandline, this will remove all but the current and second most current (via the "-2" in the head command below):
Update:
purge-old-kernels
is deprecated nowadays.I made a script to purge kernels even in tricky conditions. It is called
linux-purge
and you can find it here.If you just want to purge the kernels (and the related packages) that are older than the currently used kernel, when system is not broken, you could use this script.
There is also an Ubuntu documentation page that I have contributed to concerning removing old kernels here.
You can follow the Using the "unattended-upgrades" package section of Automatic Security Updates article on Ubuntu Wiki to perform this.
You need to change the following line in /etc/apt/apt.conf.d/50unattended-upgrades file;
with
to automatically remove old packages, including kernels.
Also remove or comment the line
in the "NeverAutoRemove" section of the file /etc/apt/apt.conf.d/01autoremove.
The fastest/simpler way (Applicable at least since 12.04) possible that already comes with Ubuntu is
apt-get
. Do the following if you wish to remove all older kernel versions that are not in use (Except the previous one that you are no using. This is to make sure that if the current kernel version fails in some way, you have a way to go back to a previous state). Do the following:This will eliminate any old files (Including kernel versions) you may have. Note that if you have many old versions, it will take a while since it has to make sure that removing the kernel version has no issues. For me, removing the last 12 kernel versions took about 2 minutes. You can also do the following:
Which will eliminate everything downloaded and stored in the cache folder of apt. Lastly you have:
which would check for any unused packages and remove them if necessary. This is great for those libraries and dependency packages that are no longer needed byt any app installed.
To figure out what kernels and headers are installed use
You can then remove them one by one or together, just make sure to keep the most recent.
There are also some handy commands and scripts to automate the removal.
http://ubuntuforums.org/showthread.php?t=1658648
The following claims to remove all unused kernels and headers:
(use
apt-get -y
to remove without question)Here is what happens when run on 18.04.1: