In Ubuntu (I believe this also applies to many other distributions), when the package manager updates most other packages installed in your operating system, the old package will be replaced. That is, the package with older version is uninstalled first. So that it does not conflict with the newer version of the package. Then the newer version is installed to finish the update.
However, kernels are updated in a different manner. As the package manager updates your kernel, your old kernel is preserved. This is because that in the old times, using a new version of kernel may cause your system to not boot at all. So many times in the boot loader you may see a long list of all available kernels for you to start the operating system with. And if one does not work, you still has the hope to try another.
Nonetheless, this problem rarely existed now because it is already 2012 and the Linux kernel is very mature. If you are not a hardcore kernel hacker, most if not all times you just need the latest one -- who knows what security hole they fixed two days ago. In this case, you probably want to remove older Linux kernels to free up disk space because each one takes about 100MB when installed. Who doesn't like free disk space?
This gist can be used to remove all unused Linux kernels installed on your Ubuntu operating system. Note that it removes all kernels that are not currently in use. If you are using kernel version 3.2.0-24 and the latest kernel installed on your system is 3.2.0-27, the 3.2.0-24 one will be kept but the latest 3.2.0-27 kernel will be removed. So make sure use this gist only after you have rebooted from the newly installed kernel. In other words, after you installed a new Linux kernel, reboot to use the latest kernel before using this gist.
Gist break down:
- dpkg -l lists all packages known by your dpkg based system.
- awk '{print $2}' filters the output of the previous command so that only the second column is kept, which is the name of the packages.
- grep "linux-\(image\|headers\)-[0-9]" filters the output to only include package names like linux-image-X.. or linux-headers-X.. where X is a numerical character. The result of this is simply all kernel packages known by your Ubuntu system.
- grep -v `uname -r | sed s/-generic//`
- uname -r outputs the release of your currently active kernel. On a Ubuntu distribution it usually is in the form of "X.X.X-X-generic".
- sed s/-generic// replaces "-generic" with an empty string. The result is only in the form "X.X.X-X" as the version of the currently active kernel.
- grep -v `...` filters the list of all kernel packages so that the package names does not contain the currently active kernel version. The result of this is a list of all unused kernel packages.
- xargs sudo apt-get purge -y takes the previous result as command-line arguments. This command removes from your system the packages and configuration files in the unused kernel packages. Note that because of the -y option, it will not give you a chance to confirm (For some reason without the -y option, apt-get will simply abort). Handle with care.
For more information, please refer the man pages for these commands.
Xie Xie Zhao!!
ReplyDeleteYours was the only 'gist' which worked for me! I tried 3-4 before this one but NONE WORKED!
I like to keep my Ubuntu updated to the latest stable version. But today when I permitted Update Manager to download and install updates, it complained, "insufficient space".
Like most real-world users I run Ubuntu side-by-side Windows 7 allocating a smaller partition to Ubuntu than to Windows. Of course, my data gets the largest partition. Since I'm on a laptop, upgrading hard disk is a practical (& economical) option.
Glad this "gist" helped and thanks for letting me know!
DeleteCorrection to my last sentence:
ReplyDeleteSince I'm on a laptop, upgrading hard disk is NOT a practical (& economical) option.
Sorry for the typo!