Ubuntu 16.04: Unattended-upgrades runs at random times
After debugging this I found the solution.
The root cause of this issue resides in the fact that under Ubuntu 16.04 and newer, unattended-upgrades uses systemd - not cron - to schedule the updates with a huge randomized delay:
/lib/systemd/system/apt-daily.timer
is configured with
OnCalendar=*-*-* 6,18:00
RandomizedDelaySec=12h
This means that it will run twice a day, at 6:00 and 18:00, with a random delay of up to 12 hours. As this is not always acceptable for production environments, I had to override these settings.
In order to keep the package config files untouched, I defined my override in /etc/systemd/system/apt-daily.timer.d/override.conf
(Update: please read the edit at the bottom of this answer for further information on filename and location, as it seems to be slightly subject to change).
There I set
[Timer]
OnCalendar=
OnCalendar=06:00
RandomizedDelaySec=1h
to have unattended-upgrades run at 6:00 plus a random delay of up to an hour.
Then I simply restarted the timer with systemctl restart apt-daily.timer
(eventually need to reload the daemon).
Unattended-updates now runs at predictable times again!
Edit:
It would seem as if for Ubuntu 18.04 things changed a bit. The override should now be stored in /etc/systemd/system/apt-daily-upgrade.timer.d/override.conf
and look like this:
[Timer]
OnCalendar=*-*-* 6:00
RandomizedDelaySec=1h
@PerlDuck has mentioned a way of creating an override file with the right name and location in a comment below. Instead of manually creating a file, please consider running sudo systemctl edit apt-daily.timer
The official debian documentation on https://wiki.debian.org/UnattendedUpgrades currently has a mistake in it that is misleading a lot of people. It claims that you can override the upgrade time by creating a file called
/etc/systemd/system/apt-daily-upgrade.d/override.conf
However the correct path is
/etc/systemd/system/apt-daily-upgrade.timer.d/override.conf
I tried Daniel's solution but the upgrade still ran at incorrect times. Turned out there are two systemd overrides needed:
Used for downloads
/lib/systemd/system/apt-daily.timer
...that is overridden with:
/etc/systemd/system/apt-daily.timer.d/override.conf
Used for upgrading
/lib/systemd/system/apt-daily-upgrade.timer
...that is overridden with:
/etc/systemd/system/apt-daily-upgrade.timer.d/override.conf