Cleanup After Linux Kernel Updates 1
Update May 2021
dpkg -l 'linux*' | awk '/^rc/{print $2}' | xargs sudo apt purge -y
Original Article
If you run Ubuntu Server LTS releases like I do, you are probably wasting disk space and, because of that, backup storage because old files tied to old kernels aren’t cleaned up automatically.
After locating and cleaning up the old kernel files, I regained over 1GB of space. I was getting low on storage on / otherwise I wouldn’t have bothered.
First Method – Package Manager
When you use a package manager for installations, you want to use it for removals too.
First, I want a list of installed kernel specific packages
sudo dpkg-query -l | egrep -i ‘2.6.17|2.6.2[0-3]’
Ouch. That’s a big list with many old, unused packages still installed. If you have Synaptic installed, using the search in that tool will let you easily multi-select packages for removal. Without X/Windows, you’ll be at the command line with me. Time to start removing them with cmds like this.
sudo apt-get remove linux-source-2.6.17 vmware-player-kernel-modules-2.6.17-10 xen-image-xen0-2.6.17-6-generic-xen0 linux-image-2.6.20-17-generic linux-restricted-modules-2.6.20-17-generic
Package managers remove libraries and programs, but avoid removing configuration files, since the next version of a tool probably needs them. If you are truly removing an application, you’ll probably want to purge the install to remove the conf files too.
Second Method – Find and Locate
I use locate and updatedb. I can’t imagine running servers without these tools. To start, I wanted a list of locations to look through and determine how much waste I had. On the server, we are using kernel 2.6.24-26-generic today. To find almost all the files, use
locate 2.6.24 | egrep -vi /backup > /tmp/old-kernel-files
These commands return a list of files, remove backups from that list and put the list into a /tmp file for reference.
On my Ubuntu system, files were located in:
/boot
/lib/modules
/lib/firmware
/linux-restricted-modules/
/usr/src
I had about 10 old kernels lying around beginning with 2.6.17. Because I wanted to be very careful with removing these files, I manually typed the cleanup commands in each directory. A few examples:
sudo rm -rf 2.6.17-*
sudo rm -rf 2.6.24-2[3-4]*
sudo rm -rf linux-headers-2.6.17-1*
sudo rm linux
sudo rm -rf linux-source-2.6.17 orig-linux-headers-2.6.17-10*
Definitely be careful. It is easy to remove the wrong files by accident with a bad pattern.
Good luck getting that space back!
Trackbacks
Use the following link to trackback from your own site:
https://blog.jdpfu.com/trackbacks?article_id=469
To be clear, the find/locate method needs to be avoided. Using it without already cleaning out the APT database will get that DB is an undesirable state.
Using dpkg, apt-get, and aptitude to get the DB in a good place before dropping down to the rm -rf commands.