How can we remove the unused linux kernels and at the same time have our boot configuration (grub’s entries in the grub cfg menu) updated?
First we want to list all the kernel-related packages, like linux-image and linux-headers, linux-generic, etc:
$ dpkg -l linux-*
On my 10.04 Ubuntu system the above command results in the following list of packages:
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Cfg-files/Unpacked/Failed-cfg/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Description
+++-======================-======================-============================================================
ii linux-alsa-driver-modu 2.6.32-28.201103131600 Ubuntu-supplied Linux modules for version 2.6.32-28-generic
un linux-backports-module (no description available)
un linux-doc-2.6.32 (no description available)
ii linux-firmware 1.34.7 Firmware for Linux kernel drivers
ii linux-generic 2.6.32.38.44 Complete Generic Linux kernel
un linux-headers (no description available)
un linux-headers-2.6 (no description available)
un linux-headers-2.6-686 (no description available)
un linux-headers-2.6-amd6 (no description available)
ii linux-headers-2.6.32-2 2.6.32-21.32 Header files related to Linux kernel version 2.6.32
ii linux-headers-2.6.32-2 2.6.32-21.32 Linux kernel headers for version 2.6.32 on x86/x86_64
ii linux-headers-2.6.32-2 2.6.32-28.55 Header files related to Linux kernel version 2.6.32
ii linux-headers-2.6.32-2 2.6.32-28.55 Linux kernel headers for version 2.6.32 on x86/x86_64
ii linux-headers-2.6.32-3 2.6.32-30.59 Header files related to Linux kernel version 2.6.32
ii linux-headers-2.6.32-3 2.6.32-30.59 Linux kernel headers for version 2.6.32 on x86/x86_64
ii linux-headers-2.6.32-3 2.6.32-33.72 Header files related to Linux kernel version 2.6.32
ii linux-headers-2.6.32-3 2.6.32-33.72 Linux kernel headers for version 2.6.32 on x86/x86_64
ii linux-headers-2.6.32-3 2.6.32-38.83 Header files related to Linux kernel version 2.6.32
ii linux-headers-2.6.32-3 2.6.32-38.83 Linux kernel headers for version 2.6.32 on x86/x86_64
ii linux-headers-2.6.32-3 2.6.32-39.86 Header files related to Linux kernel version 2.6.32
ii linux-headers-generic 2.6.32.38.44 Generic Linux kernel headers
un linux-image (no description available)
un linux-image-2.6 (no description available)
ii linux-image-2.6.32-21- 2.6.32-21.32 Linux kernel image for version 2.6.32 on x86/x86_64
ii linux-image-2.6.32-28- 2.6.32-28.55 Linux kernel image for version 2.6.32 on x86/x86_64
ii linux-image-2.6.32-30- 2.6.32-30.59 Linux kernel image for version 2.6.32 on x86/x86_64
ii linux-image-2.6.32-33- 2.6.32-33.72 Linux kernel image for version 2.6.32 on x86/x86_64
ii linux-image-2.6.32-38- 2.6.32-38.83 Linux kernel image for version 2.6.32 on x86/x86_64
ii linux-image-generic 2.6.32.38.44 Generic Linux kernel image
un linux-initramfs-tool (no description available)
un linux-kernel-headers (no description available)
un linux-kernel-log-daemo (no description available)
ii linux-libc-dev 2.6.32-38.83 Linux Kernel Headers for development
un linux-restricted-commo (no description available)
ii linux-sound-base 1.0.22.1+dfsg-0ubuntu3 base package for ALSA and OSS sound systems
un linux-source-2.6.32 (no description available)
un linux-tools (no description available)
These are all the “linux-” packages that dpkg knows of at the moment (installed and not installed). Let’s say we want to remove all the kernels except the most recent one. Actually, leaving one of the older ones, just in case, is usually a good idea 😉
We’ll use awk and grep to filter the above list, getting just the 2nd column (which contains the actual names of packages) for those rows which contain the unwanted packages. The easiest thing to do (especially if you’re not a grep/gawk guru) is to use the unwanted kernel versions as a grep search string at the same time telling awk to print just the 2nd column, e.g. this will print all the packages of the 2.6.32-30 kernel:
$ dpkg -l linux-* | awk '{ print $2 }' | grep -w 30
linux-headers-2.6.32-30
linux-headers-2.6.32-30-generic
linux-image-2.6.32-30-generic
We can now give all of these files to apt-get (via xargs) for removal:
$ dpkg -l linux-* | awk '/^ii/{ print $2 }' | grep -w 30 | xargs sudo apt-get -y purge
… do the same for all the other kernel packages that you no longer want on your system.
Dude, keep up the good work! You’re helping alot of people, even tough most wont leave a comment.
thank you.
~afijgæjfg