Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save dwilkie/41ae0c7acc48186e6058 to your computer and use it in GitHub Desktop.
Save dwilkie/41ae0c7acc48186e6058 to your computer and use it in GitHub Desktop.
Setup unattended upgrades on Ubuntu with Gmail

Install the unattended-upgrades package

$ sudo apt-get install unattended-upgrades 

Edit the periodic configuration

$ sudo nano /etc/apt/apt.conf.d/10periodic

Set the following:

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "1";
APT::Periodic::AutocleanInterval "7";

Where the number is the frequency (in days)

Edit the unattended upgrades configuration

$ sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

Configure the default packages you want to automatically upgrade:

Unattended-Upgrade::Allowed-Origins {
        "${distro_id}:${distro_codename}-security";
        "${distro_id}:${distro_codename}-updates";
//      "${distro_id}:${distro_codename}-proposed";
//      "${distro_id}:${distro_codename}-backports";
};

Optionally configure additional packages you want to upgrade

In order to automatically upgrade custom packages do the following:

  1. Look in /var/lib/apt/lists/ to find the custom package that you want to update. It should end with Release e.g. /var/lib/apt/lists/files.freeswitch.org_repo_deb_debian_dists_wheezy_InRelease
  2. Open up the file $ nano /var/lib/apt/lists/files.freeswitch.org_repo_deb_debian_dists_wheezy_InRelease
  3. Look for the Origin and Suite entries. e.g. Origin: freeswitch Suite: stable and note these values.
  4. Edit the unattended upgrades configuration again. $ sudo nano /etc/apt/apt.conf.d/50unattended-upgrades
  5. Add an entry for the origin and suite in the configuration

E.g.

Unattended-Upgrade::Allowed-Origins {
        "${distro_id}:${distro_codename}-security";
        "${distro_id}:${distro_codename}-updates";
        "freeswitch:stable";
//      "${distro_id}:${distro_codename}-proposed";
//      "${distro_id}:${distro_codename}-backports";
};

Setup automatic reboot (optional)

This will reboot the server if required automatically.

Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "19:00"; // Optional

Setup Mail

Unattended-Upgrade::Mail "someone@gmail.com";

Send notifications via Gmail

Install mailx

$ heirloom-mailx

Set mail defaults

$ sudo su
$ cd ~
$ nano .mailrc

Add the following to .mailrc in root's home directory.

set smtp-use-starttls
set ssl-verify=ignore
set smtp=smtp://smtp.gmail.com:587
set smtp-auth=login
set smtp-auth-user=someone@gmail.com
set smtp-auth-password=secret
set from="someone@gmail.com

Change the permissions of .mailrc

chmod 400 .mailrc

Test it out

$ sudo unattended-upgrade -v -d --dry-run

Trigger it now

$ sudo unattended-upgrade -v -d
@jarnos
Copy link

jarnos commented Jun 2, 2017

You do not need to install mailx to be able to send notifications via Gmail. See here.

@cmanzur
Copy link

cmanzur commented Apr 23, 2018

It works perfect in Ubuntu 14.04 but it doesn't work in Ubuntu 16.04 or 18.04... I think it is something relative to systemd + environments.
The final solution that works for me was install sendmail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment