How to remove old Kernel on Ubuntu 16.04

There was a case when we have multiple, obsolete Linux Kernels exist on our Ubuntu system. In longer time, this can cause a problem when the /boot directory gets full. This tutorial will show you how to remove any old kernels using several methods.

To see the Linux Kernel on your system use this command

Output:

abi-4.4.0-22-generic config-4.4.0-36-generic initrd.img-4.4.0-42-generic System.map-4.4.0-72-generic
abi-4.4.0-31-generic config-4.4.0-38-generic initrd.img-4.4.0-47-generic System.map-4.4.0-75-generic
abi-4.4.0-34-generic config-4.4.0-42-generic initrd.img-4.4.0-51-generic vmlinuz-4.4.0-22-generic
abi-4.4.0-36-generic config-4.4.0-47-generic initrd.img-4.4.0-72-generic vmlinuz-4.4.0-31-generic
abi-4.4.0-38-generic config-4.4.0-51-generic lost+found vmlinuz-4.4.0-34-generic
abi-4.4.0-42-generic config-4.4.0-72-generic System.map-4.4.0-22-generic vmlinuz-4.4.0-36-generic
abi-4.4.0-47-generic config-4.4.0-75-generic System.map-4.4.0-31-generic vmlinuz-4.4.0-38-generic
abi-4.4.0-51-generic grub System.map-4.4.0-34-generic vmlinuz-4.4.0-42-generic
abi-4.4.0-72-generic initrd.img-4.4.0-22-generic System.map-4.4.0-36-generic vmlinuz-4.4.0-47-generic
abi-4.4.0-75-generic initrd.img-4.4.0-31-generic System.map-4.4.0-38-generic vmlinuz-4.4.0-51-generic
config-4.4.0-22-generic initrd.img-4.4.0-34-generic System.map-4.4.0-42-generic vmlinuz-4.4.0-72-generic
config-4.4.0-31-generic initrd.img-4.4.0-36-generic System.map-4.4.0-47-generic vmlinuz-4.4.0-75-generic
config-4.4.0-34-generic initrd.img-4.4.0-38-generic System.map-4.4.0-51-generic

A. Automatic mode

The easiest way to clean up kernel is using automatic mode. The following command can be used to remove kernels which were installed during system update.

Output:

dhani@vmtest01:/$ sudo apt autoremove –purge
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following packages will be REMOVED:
bridge-utils* galera-3* iproute* libaio1* libjemalloc1* linux-headers-4.4.0-21* linux-headers-4.4.0-21-generic*
linux-headers-4.4.0-22* linux-headers-4.4.0-22-generic* linux-headers-4.4.0-31* linux-headers-4.4.0-31-generic*
linux-headers-4.4.0-34* linux-headers-4.4.0-34-generic* linux-headers-4.4.0-36* linux-headers-4.4.0-36-generic*
linux-headers-4.4.0-38* linux-headers-4.4.0-38-generic* linux-headers-4.4.0-42* linux-headers-4.4.0-42-generic*
linux-headers-4.4.0-47* linux-headers-4.4.0-47-generic* linux-headers-4.4.0-51* linux-headers-4.4.0-51-generic*
linux-image-4.4.0-22-generic* linux-image-4.4.0-31-generic* linux-image-4.4.0-34-generic*
linux-image-4.4.0-36-generic* linux-image-4.4.0-38-generic* linux-image-4.4.0-42-generic*
linux-image-4.4.0-47-generic* linux-image-4.4.0-51-generic* linux-image-extra-4.4.0-22-generic*
linux-image-extra-4.4.0-31-generic* linux-image-extra-4.4.0-34-generic* linux-image-extra-4.4.0-36-generic*
linux-image-extra-4.4.0-38-generic* linux-image-extra-4.4.0-42-generic* linux-image-extra-4.4.0-47-generic*
linux-image-extra-4.4.0-51-generic* mariadb-server-core-10.1* socat* ubuntu-fan*
0 upgraded, 0 newly installed, 42 to remove and 149 not upgraded.
6 not fully installed or removed.
After this operation, 2,503 MB disk space will be freed.
Do you want to continue? [Y/n]

B. Manually remove old Kernels

The second way to remove old kernels is using special script maintained by byobu. Its also pretty easy to use

usage:

Output:

dhani@vmtest01:~$ sudo purge-old-kernels
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following packages were automatically installed and are no longer required:
bridge-utils cockpit-docker galera-3 iproute libaio1 libjemalloc1 linux-headers-4.4.0-21
linux-headers-4.4.0-21-generic linux-headers-4.4.0-22 linux-headers-4.4.0-31 linux-headers-4.4.0-34
linux-headers-4.4.0-36 linux-headers-4.4.0-38 linux-headers-4.4.0-42 linux-headers-4.4.0-47 linux-headers-4.4.0-51
mariadb-server-core-10.1 socat ubuntu-fan
Use ‘sudo apt autoremove’ to remove them.
The following packages will be REMOVED:
linux-headers-4.4.0-22-generic* linux-headers-4.4.0-31-generic* linux-headers-4.4.0-34-generic*
linux-headers-4.4.0-36-generic* linux-headers-4.4.0-38-generic* linux-headers-4.4.0-42-generic*
linux-headers-4.4.0-47-generic* linux-headers-4.4.0-51-generic* linux-image-4.4.0-22-generic*
linux-image-4.4.0-31-generic* linux-image-4.4.0-34-generic* linux-image-4.4.0-36-generic*
linux-image-4.4.0-38-generic* linux-image-4.4.0-42-generic* linux-image-4.4.0-47-generic*
linux-image-4.4.0-51-generic* linux-image-extra-4.4.0-22-generic* linux-image-extra-4.4.0-31-generic*
linux-image-extra-4.4.0-34-generic* linux-image-extra-4.4.0-36-generic* linux-image-extra-4.4.0-38-generic*
linux-image-extra-4.4.0-42-generic* linux-image-extra-4.4.0-47-generic* linux-image-extra-4.4.0-51-generic*
0 upgraded, 0 newly installed, 24 to remove and 153 not upgraded.
6 not fully installed or removed.
After this operation, 1,802 MB disk space will be freed.
Do you want to continue? [Y/n]

C. Remove using DPKG

This third way is mainly useful when our /boot partition is full. When this happen, you will get an error saying that your /boot is full and you cannot remove package because of broken dependencies etc.

If your /boot is full, do the following:

Check which kernel is being used using this command:

uname -r

Output:

dhani@vmtest01:/$ uname -r
4.4.0-72-generic

Now list all kernels including the currently used

Output:

ii linux-image-4.4.0-22-generic 4.4.0-22.40 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-4.4.0-31-generic 4.4.0-31.50 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-4.4.0-34-generic 4.4.0-34.53 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-4.4.0-36-generic 4.4.0-36.55 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-4.4.0-38-generic 4.4.0-38.57 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-4.4.0-42-generic 4.4.0-42.62 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-4.4.0-47-generic 4.4.0-47.68 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-4.4.0-51-generic 4.4.0-51.72 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
iF linux-image-4.4.0-75-generic 4.4.0-75.96 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP

As you can see there are a lot of old kernels in my /boot directory. Now we can remove kernels with “ii” marked. Do not remove the ones with “iU” mark.

Use this command to remove the kernel

sudo dpkg –purge linux-image-4.4.0-22-generic

 

Ariel

Leave a Reply

Your email address will not be published. Required fields are marked *