Useful commands #2: Removing old kernels

Leave a comment

Successfully tested on Ubuntu Server 12.04 LTS 64-bit

In a recent post, I explained how important it is to keep your operating system up to date. If you followed my advices, you may have noticed that Ubuntu’s package manager does not delete any old kernel when upgrading to a new one. It’s on purpose, just to have a known good kernel around in case the upgrade fails. While this is a rather reassuring behavior, you could also want to get rid of these old kernels (to free up some space on your hard disk for example). Let’s see how.

Before doing any cleaning, first write down your current kernel release. You’ll find it by running:

uname -r

 
Next, simulate the old kernels removal:

dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get --dry-run remove

 
Here, the –dry-run parameter allows to see the events that would occur during real removal but no change is actually done to the system. Check all the informations displayed on screen to make sure there was no error and verify that your current kernel release was not listed anywhere.

If everything looks fine, you can run the command for real:

dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get -y purge

 
Old kernels are history now.

That’s all Folks!


For further reading, see the Ubuntu wiki to understand why this removal is not so easy to automate.

Advertisements

Useful commands #1: Preventing the update of a package

Leave a comment

Successfully tested on Ubuntu Server 12.04 LTS 64-bit

Doing frequent updates of your favorite OS and programs is always recommended. Not only does it allow you to take advantage of new features but it also minimizes the security risks by correcting the possible flaws spotted since the installation or the previous update.

In Ubuntu, software updates can be either automatic (see Automatic Updates in the official documentation for details) or… manual. If you choose to go the manual way, you’ll have to make sure to do the updates by yourself on a regular basis (Ubuntu’s welcome screen advises you of updates availability) using two commands:

sudo apt-get update

 
first, to re-synchronize the package index files from their sources (the repositories are listed in /etc/apt/sources.list), followed by:

sudo apt-get upgrade

 
to finally install the newest versions of all packages currently installed on the system.

This full update in one shot is very handy of course, but what to do if you don’t want to update a specific package? For instance, let say you patched and rebuilt a program for whatever reason and you don’t want it to be replaced by the new latest official version. What to do? This is where today’s useful command comes in:

sudo apt-mark hold package_name

 
Now, this package will never be updated automatically as well as manually as long as it is on “hold” status. To display this status, you can use:

dpkg --get-selections | grep "package_name"

 
Finally, the day you decide to allow the upgrade to the latest version available in the official repository, just type:

sudo apt-mark unhold package_name

 
to put the package back on “install” status.

That’s all Folks!


For further reading, take a look at Package Management in Ubuntu’s official documentation.