How to automatically upgrade ubuntu kernel and delete oldest one?

I am very, very new to Linux.  My son  set it up, and is not available now.  He had the kernals automatically updating, which eventually totally filled up /boot/.  With the help of Experts I was able to delete older kernels and have working room in /boot/.  I have a separate question posted to increase /boot/ to 500MB.  

What I need is to arrange for new kernel updates to be installed, then the oldest image to be deleted.  Right now I seem to have three images under /boot/, so if I automatically deleted the oldest I would always have three, which would be fine.  This is a pure ubuntu 14.04 machine, no Windows, no dual boot.

I am very new, so please don't quote processes and procedures; I need exact command lines I can type verbatim.
LVL 1
Mike CaldwellConsultant to IP industryAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

asavenerCommented:
From http://askubuntu.com/questions/2793/how-do-i-remove-or-hide-old-kernel-versions-to-clean-up-the-boot-menu



My one-liner to remove old kernels (this also frees up disk space)

dpkg --list | grep linux-image | awk '{ print $2 }' | sort | sed -n '/'`uname -r`'/q;p' | xargs sudo apt-get -y purge

Explanation (remember, | uses the output of the previous command as the input to the next)

    dpkg --list lists all installed packages
    grep link-image looks for the installed linux images
    awk '{ print $2 }' just outputs the 2nd column (which is the package name)
    sort puts the items in ascending order
    sed -n '/'`uname -r`'/q;p' prints the lines before the current kernel
    xargs sudo apt-get -y purge purges the found kernels

Unwinding the sed invocation:

    -n tells sed to be quiet
    `uname -r` outputs the current installed kernel release - we include it in backticks so that the output is includes as part of the command (you might also see this as $(uname -r)
    /something/q says stop when you match 'something' (in this case, something is output of uname -r) - the / surround a regular expression
    p is print
    the ; is the command separtor, so /something/q;p says quit when you match something, else print

altogether, sed -n '/'`uname -r`'/q;p' is print the lines until it matches with the current kernel name.

If you're paranoid (like me), you can make the last part xargs echo sudo apt-get -y purge so that the command to purge the old kernels is printed, then you can check that nothing unexpected is included before you run it.

Modified version to remove headers:

dpkg --list | grep 'linux-image' | awk '{ print $2 }' | sort -n | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p' | xargs sudo apt-get -y purge
dpkg --list | grep 'linux-headers' | awk '{ print $2 }' | sort -n | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p' | xargs sudo apt-get -y purge

Note: the sed invocation is modified. "$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")" extracts only the version (e.g. "3.2.0-44") , without "-generic" or similar from uname -r

All-in-one version to remove images and headers (combines the two versions above):

echo $(dpkg --list | grep linux-image | awk '{ print $2 }' | sort | sed -n '/'`uname -r`'/q;p') $(dpkg --list | grep linux-headers | awk '{ print $2 }' | sort -n | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p') | xargs sudo apt-get -y purge
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike CaldwellConsultant to IP industryAuthor Commented:
Wow asavener: way, way over my head.  I'll probably have my son take a peek at it, but your description matches what I am trying to do.  One quick question:  rather than deleting all of the previous kernel images, it is possible to install the new one, then delete any older than the previous two?
0
Mike CaldwellConsultant to IP industryAuthor Commented:
I forgot to ask: does this also install the new kernel?  If not, since my understanding is that it's an automatic thing, then perhaps I could set this to just run, say, once a month.

Also, given your expertise in this area, perhaps you could take a look at my other posted question

"How to increase ubuntu 14.04 boot partition size?"
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Mike CaldwellConsultant to IP industryAuthor Commented:
asavener: can't really test it now, but expect your recommendation is just fine.  So I will save it for a few days when I can give it a go.  But meanwhile if I could ask for one extension to this:  assuming I use the last listed version (the one that does everything) can you tell me how to save that as some sort of macro / utility that I can execute by clicking on something or a macro name from the terminal window?  That would be very helpful.
0
asavenerCommented:
It does not update the kernel.

To create a script that does this:

Create a new file in your favorite text editor.  

#!/bin/bash

echo $(dpkg --list | grep linux-image | awk '{ print $2 }' | sort | sed -n '/'`uname -r`'/q;p') $(dpkg --list | grep linux-headers | awk '{ print $2 }' | sort -n | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p') | xargs sudo apt-get -y purge

Save it with a .sh extension (remove_old_kernel_versions.sh or similar)

Then just execute the script from a terminal window.
0
Mike CaldwellConsultant to IP industryAuthor Commented:
Is it possible to put some sort of counter in there so that rather than wiping out all of the older images it leaves the most recent two behind?  Thanks for the script help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.