Dec 282015
Notes Tidbits

This post will hold little notes and tidbits that I want to ensure I don’t lose yet don’t require their own dedicated blog post to discuss.

Removing and cleaning up old linux kernels

I run a lot of ubuntu based linux servers and depending how they were partitioned out, over time, the unused and older linux kernels can chew up a ton of space. I used to just remove the old images and headers from the /boot partition and run update-grub2 to update the grub menu with what is left. That does free up space on the actual /boot partition but those unused kernels and images are still installed on your system. I’ve googled for the best way to remove these and found one command I’ve been using for a while now that works flawlessly and causes no issues as long as you follow one rule. The one rule: Before executing this command it is important to update your system to the latest kernel (aptitude update && aptitude full-upgrade), and then REBOOT to actually boot into it. Once safely on your latest kernel run this command to clean up every older version
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
It looks long and scary, but it really isn’t that bad once you break it down, which someone gratefully did in this post. That’s it, follow the one rule and run the one command and your server’s kernels will be nice and lean.