Wednesday, October 2, 2013

Ubuntu Cleanup: How to Remove All Unused Linux Kernel Headers, Images and Modules

First do upgrade to the latest kernel and reboot with the new kernel, make sure everything works!

$ uname -a
Linux MDS 3.2.0-54-generic #82-Ubuntu SMP Tue Sep 10 20:09:12 UTC 2013 i686 i686 i386 GNU/Linux

Then check all the linux kernel installed:

$ dpkg -l 'linux-*'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Description
+++-=========================================-=========================================-==================================================================================================
un linux-doc-3.2.0 (no description available)
ii linux-firmware 1.79.6 Firmware for Linux kernel drivers
ii linux-generic 3.2.0.54.64 Complete Generic Linux kernel
un linux-headers (no description available)
un linux-headers-3 (no description available)
un linux-headers-3.0 (no description available)
un linux-headers-3.2.0-23 (no description available)
un linux-headers-3.2.0-23-generic (no description available)
ii linux-headers-3.2.0-52 3.2.0-52.78 Header files related to Linux kernel version 3.2.0
ii linux-headers-3.2.0-52-generic 3.2.0-52.78 Linux kernel headers for version 3.2.0 on 32 bit x86 SMP
ii linux-headers-3.2.0-54 3.2.0-54.82 Header files related to Linux kernel version 3.2.0
ii linux-headers-3.2.0-54-generic 3.2.0-54.82 Linux kernel headers for version 3.2.0 on 32 bit x86 SMP
ii linux-headers-generic 3.2.0.54.64 Generic Linux kernel headers
un linux-image (no description available)
un linux-image-3.0 (no description available)
ii linux-image-3.2.0-52-generic 3.2.0-52.78 Linux kernel image for version 3.2.0 on 32 bit x86 SMP
ii linux-image-3.2.0-54-generic 3.2.0-54.82 Linux kernel image for version 3.2.0 on 32 bit x86 SMP
ii linux-image-generic 3.2.0.54.64 Generic Linux kernel image
un linux-initramfs-tool (no description available)
un linux-kernel-headers (no description available)
un linux-kernel-log-daemon (no description available)
ii linux-libc-dev 3.2.0-54.82 Linux Kernel Headers for development
un linux-restricted-common (no description available)
ii linux-sound-base 1.0.25+dfsg-0ubuntu1.1 base package for ALSA and OSS sound systems
un linux-source-3.2.0 (no description available)
un linux-tools (no description available)


List all old kernels:


$ dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/(.*)-([^0-9]+)/1/")"'/d;s/^[^ ]* [^ ]* ([^ ]*).*/1/;/[0-9]/!d'

linux-headers-3.2.0-52
linux-headers-3.2.0-52-generic
linux-image-3.2.0-52-generic


Run this command to clean the old kernel:


$ 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



Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
linux-headers-3.2.0-52* linux-headers-3.2.0-52-generic* linux-image-3.2.0-52-generic*
0 upgraded, 0 newly installed, 3 to remove and 0 not upgraded.
After this operation, 181 MB disk space will be freed.
(Reading database ... 152425 files and directories currently installed.)
Removing linux-headers-3.2.0-52-generic ...
Removing linux-headers-3.2.0-52 ...
Removing linux-image-3.2.0-52-generic ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.2.0-52-generic /boot/vmlinuz-3.2.0-52-generic
update-initramfs: Deleting /boot/initrd.img-3.2.0-52-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.2.0-52-generic /boot/vmlinuz-3.2.0-52-generic
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-3.2.0-54-generic
Found initrd image: /boot/initrd.img-3.2.0-54-generic
Found memtest86+ image: /boot/memtest86+.bin
done
The link /vmlinuz.old is a damaged link
Removing symbolic link vmlinuz.old
you may need to re-run your boot loader[grub]
The link /initrd.img.old is a damaged link
Removing symbolic link initrd.img.old
you may need to re-run your boot loader[grub]
Purging configuration files for linux-image-3.2.0-52-generic ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.2.0-52-generic /boot/vmlinuz-3.2.0-52-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.2.0-52-generic /boot/vmlinuz-3.2.0-52-generic


This will generate a new GRUB menu, and when you reboot, you’ll see only the current kernel is listed.

4 comments:

  1. This is the standard way to remove packages. How about 'linux-headers-3.2.0-23-generic' in your example?
    How do you remove these types of old entries. I have many on my system

    ReplyDelete
  2. Hi JD,

    If you check my example, it also removed.
    Search "Removing linux-headers-3.2.0-52-generic ..."

    I have been running this remove command over 50 times on different ubuntu/xubuntu, it works very well.

    Let me know if you have any other questions.

    Best Regards,

    ReplyDelete
  3. it removed build-essential and haskell as well=-_-

    ReplyDelete
  4. Yes, and it causes me problem every time!

    I am using Nodejs to do some JavaScript programming and some Nodejs modules require build-essential to be installed.

    Every time I remove old linux-headers packages, apt-get automatically removes build-essential, and I have to reinstall it :-(

    ReplyDelete