8

We've some Ubuntu 16.04 servers. unattended-upgrades are automatically enabled since 16.04 and the boot partition is on a separate partition. Due to the automatic security updates the boot partition is running out of space with new kernels.

We can't just reboot the systems so the machine is still using the penultimate kernel.

Which kernels should I remove? All but the currrent, the oldest and the newest?

Do you guys have some recommendations?

I have also noticed that the newest kernel has the status "Half Configured". This kernel would probably not work so should I remove this one and use the an older kernel.

Output of dpkg -l | grep linux image:

    ii  linux-image-4.4.0-21-generic --> old kernel
    ii  linux-image-4.4.0-34-generic --> current kernel
    ii  linux-image-4.4.0-36-generic --> new kernel
    ii  linux-image-4.4.0-38-generic --> new kernel
    ii  linux-image-4.4.0-42-generic --> new kernel
    ii  linux-image-4.4.0-45-generic --> new kernel
    ii  linux-image-4.4.0-47-generic --> new kernel
    ii  linux-image-4.4.0-51-generic --> new kernel
    ii  linux-image-4.4.0-53-generic --> new kernel
    iF  linux-image-4.4.0-57-generic --> new kernel
7

To purge old kernel , you can use purge-old-kernels command line tool .

This program will remove old kernel and header packages from the system, freeing disk space. It will never remove the currently running kernel. By default, it will keep at least the latest 2 kernels, but the user can override that value using the --keep parameter. Any additional parameters will be passed directly to apt-get

To install purge-old-kernels , run:

sudo apt install byobu

or

sudo apt install bikeshed

To keep the latest n kernel run:

sudo purge-old-kernels --keep n

e,g: n=2

sudo purge-old-kernels --keep 2

Update grub:

sudo update-grub
  • 2
    byobu is probably the package you want for 16.04 Xenuial: purge-old-kernels got moved from bikeshed to byobu sometime between Trusty and Xenial, though bikeshed now recommends byobu so you'll still get the program unless you have no-install-recommends. – Wolfgang Dec 28 '16 at 22:19
4

For Ubuntu, Grub2 automatically display the latest kernel and hides the older kernels. If you want to see them, you can press Shift while booting.

So, before deleting older kernels, boot with the latest available kernel (grub2 load it by default in booting). To check which kernel you are using you can use the command:

uname -r

The recommendation is to keep at least two or preferably three kernels including the latest. The reason is that you will have at least one/two other kernels to boot with if it occurs that you are unable to boot with the latest kernel.

To remove the older kernels, open terminal and check your current kernel:

uname -r 

Then to list all installed kernels on your system.

dpkg --list | grep linux-image 

Find all the kernels that lower than your current kernel. Run the commands below to remove the kernel you selected:

sudo apt-get purge linux-image-x.x.x.x-generic 

Finally, run the commands below to update grub2

sudo update-grub2 

And reboot your system.

  • 1
    From the question: "we can't just reboot the systems". Your answer requires doing so twice. – Lightness Races with Monica Dec 28 '16 at 17:56
3

apt-get autoremove, ran periodically, should be able to help you accomplish what you want. The running kernel, the previous kernel and the two latest kernels should be enough.

  • 1
    Given the list of installed kernels, and the fact that the running kernel is old, I'd say "the running kernel, the previous kernel and the two latest kernels". – Stephen Kitt Dec 28 '16 at 14:34
2

Boot one of the previous kernel.

First try apt autoremove --purge

If it not works dpkg -l 'linux-[ihs]*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\([-0-9]*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'

Control what will be deinstallt.

dpkg -l 'linux-[ihs]*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\([-0-9]*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo dpkg -P This will purges the listet Kernel from command below.

sudo apt -f install

and for sure

 sudo update-grub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.