Home > Linux > Automatic Updates : Ubuntu (all versions)

Automatic Updates : Ubuntu (all versions)

Below is a quick hack to have your Ubuntu machine automagically update for you at a pre-specified time. This includes the “update”, “upgrade”, “dist-upgrade” and “autoclean” commands. The scheduling is taken care of by cron, our trusty always-on-time friend.

sudo crontab -e

It is important that you use sudo at this point. The command will need super-user priveleges so the cron listing needs to be setup as the root user.

0 0 * * * aptitude -y update && aptitude -y upgrade && aptitude -y dist-upgrade && aptitude -y autoclean

The above line represents the listing put into the crontab file. Here is a quick explanation of the five fields you can specify for the date and time. (The above executes daily at midnight)

minute - hour - day of month - month - day of week

After you have specified the appropriate time and date for your system simply save the file and your system will now automagically upgrade at the time specified. These upgrades include regular updates, kernel updates, security updates, etc. Also, the “autoclean” command removes old packages from your system after they are no longer needed.

Of course, if you prefer not to have cron take care of everything (if you’d still like to be somewhat hands-on) you could simply run the command in a single line:

sudo aptitude -y update && sudo aptitude -y upgrade && sudo aptitude -y dist-upgrade && sudo aptitude autoclean

technorati tags:, , , , , , , ,

Categories: Linux Tags:
  1. March 13th, 2007 at 14:32 | #1

    This may sound stupid, but I did this and it failed.

    I realised at the time it might fail, but I took a punt.

    The weak point is here: you do not say where to save the newly created crontab file. So mine ended up in tmp I think. It’s probably still there, and I still have a bunch of updates to do.

    Where should it be saved?

  2. April 17th, 2007 at 07:04 | #2

    That’s very dangerous if you have custom .conf files. eg for ssh, ftp-d clients and so on… because -y overwrites old config files.

  3. Alberto
    March 10th, 2008 at 17:35 | #3

    In regards to overwritten conf files, just back them up so you can restore them if necesary.

  4. August 12th, 2008 at 05:20 | #4

    Wouldn’t it be better to use anacron instead of cron?

    What if the machine is off power when the cron job is programmed to be run?

  5. Eric
    January 16th, 2009 at 10:11 | #5

    fix this posting. this is as clear as mud!!!

  6. trlkly
    March 29th, 2009 at 12:42 | #6

    RE:anacron

    By default on Ubuntu systems, anacron is set up to automatically run cron jobs that don’t get executed on time.

    At least, that’s what I think the settings in /etc/anacrontab mean. Check them yourself.

  7. trlkly
    March 29th, 2009 at 12:51 | #7

    Post this only if I’m right:

    If you are running GUI, you might want to disable the Automatic Updates under System -> Administration -> Software Sources -> Updates, so you aren’t doing the same job twice.

  8. April 20th, 2009 at 15:35 | #8

    Here is a very similar procedure on the official Ubuntu website:

    https://help.ubuntu.com/8.10/serverguide/C/automa

  9. Levi
    May 6th, 2009 at 16:03 | #9

    To compensate aptitude overwriting config files sometimes I made a script that backs up all my custom configuration files, runs the aptitude command, then copies the config files back. I then set that script to run as a cron job. It's pretty simple, and ensures I don't lose any configuration files while allowing me to make changes to confs without then explicitly backing them up.

  10. August 26th, 2009 at 02:17 | #10

    Thanks a lot for this article. Really help me.

  11. January 12th, 2010 at 03:59 | #11

    Isn't there something built into apt-get or aptitude to run as a service?

  12. January 12th, 2010 at 03:59 | #12

    Isn't there something built into apt-get or aptitude to run as a service?

  13. Bimbo
    August 17th, 2010 at 08:46 | #13

    From https://help.ubuntu.com/9.04/serverguide/C/automatic-updates.html

    sudo apt-get install unattended-upgrades

    Look the page for basic info

  1. January 4th, 2007 at 03:48 | #1
  2. May 15th, 2007 at 21:28 | #2