Friday, November 20, 2015

Purging Old Kernels and Headers for Ubuntu Linux

I run my home server on Ubuntu Precise LTS i386 since my server is ancient. All Ubuntu systems will build up old kernel versions due to automated updates (or in my case, semi-automated). Keeping as little as five generations of these will consume over a gigabyte of storage, so it is important to regularly trim these back. This is my manual procedure for performing this task periodically. Before you start you should install the "Boot Repair" package as I use that to see how many images are actually in place so I can determine what needs to be purged.


  1. Run boot repair on my gnome desktop menu: System Tools -> Administration -> Boot Repair
  2. Provide admin password. Wait for scan to complete.
  3. Click lower button: "Create bootinfo summary". Wait for next dialog box to pop open.
  4. Copy URL given and paste into a browser address line.
  5. Scroll down to "menuentry" xml nodes in browser bootinfo display.
  6. Make notes of older kernel grub entries you want to do away with. I keep five generations on my system which is probably overkill. Note that the one I've highlighted in orange above is the first menuentry node in the "Previous Linux versions" section of the document. The ones you want to trim are going to be towards the bottom of that section of the document.
  7. Open a command prompt and from your notes (see step 6) create a command line like this:

          sudo apt-get purge linux-image-3.13.0-{62,61,59,58,57}-generic \
          linux-headers-3.13.0-{62,61,59,58,57} \
          linux-headers-3.13.0-{62,61,59,58,57}-generic

          09/20/2016 note....
          On my newer 64-bit xenial system it is now four modules per update as follows:

          sudo apt-get purge linux-image-4.4.0-{34,31,28}-generic \
          linux-image-extra-4.4.0-{34,31,28}-generic \
          linux-headers-4.4.0-{34,31,28} \
          linux-headers-4.4.0-{34,31,28}-generic \


Notes:

  • Your kernel version will very likely be different from mine.  That is the dot separated  digits (3.13.0) in my case.
  • Your purge numbers will definitely be different from mine. That is the the five numbers contained in the comma separated list in the curly braces (62,61,59,58,57) in all three of my cases. These are the numbers you noted down in step 6 above!!!!
  • The sudo command will of course ask for your administrative password again when you press enter. This is your last chance to prevent a catastrophe!!!!!!!!!!!!
  • If you are removing a single kernel then the entire curly brace regular expression should be removed and replaced by the digits of the kernel you want to remove (example: 62 rather than {62} ). The curly brace construct will not work for the single member case.
  • Finally, that there is no dang undo command, you better aim and fire carefully sir. Don't wreck your system, you have been warned. Your system is, well, your system after all. Don't blame me for your incompetence. You can always take the low road and use Windows products. Happy Motoring!