Ubuntu – Get last time timedatectl synchronized its time/force synchronization now

dateUbuntu

I have an Ubuntu server that is a little over a minute out of sync with the actual time. When I run timedatectl I get:

Local time: Tue 2018-04-24 14:47:57 MDT
Universal time: Tue 2018-04-24 20:47:57 UTC
RTC time: Tue 2018-04-24 20:47:57
Timezone: America/Denver (MDT, -0600)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: yes
Last DST change: DST began at
          Sun 2018-03-11 01:59:59 MST
          Sun 2018-03-11 03:00:00 MDT
Next DST change: DST ends (the clock jumps one hour backwards) at
          Sun 2018-11-04 01:59:59 MDT
          Sun 2018-11-04 01:00:00 MST

My understanding is that it should be synchronizing using NTP. It's possible ntp isn't working quite right. If I run ntpq -p I get:

remote           refid      st t when poll reach   delay   offset  jitter
======================================================================
propjet.latt.ne .INIT.          16 u    - 1024    0    0.000    0.000   0.000
time-a-g.nist.g .INIT.          16 u    - 1024    0    0.000    0.000   0.000
linode227395.st .INIT.          16 u    - 1024    0    0.000    0.000   0.000
dev.smatwebdesi .INIT.          16 u    - 1024    0    0.000    0.000   0.000
pugot.canonical .INIT.          16 u    - 1024    0    0.000    0.000   0.000

Is there a way to tell when the last time was that it synced and from what server. Also, how do I force synchronization right now to fix the problem (I know I can manually set the date, but I won't get it quite right, and I want to actually fix the syncing).

Best Answer

Checking status

Since Ubuntu 16.04 timesyncd which is part of systemd is now responsible (out of the box) for syncing date/time. You can check its status using systemctl:

$ systemctl status systemd-timesyncd.service
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: active (running) since Sun 2018-08-05 10:14:44 EDT; 11min ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 3714 (systemd-timesyn)
   Status: "Synchronized to time server 108.59.2.24:123 (0.debian.pool.ntp.org)."
   CGroup: /system.slice/systemd-timesyncd.service
           └─3714 /lib/systemd/systemd-timesyncd

Aug 05 10:14:44 pi-hole systemd[1]: Starting Network Time Synchronization...
Aug 05 10:14:44 pi-hole systemd[1]: Started Network Time Synchronization.
Aug 05 10:14:45 pi-hole systemd-timesyncd[3714]: Synchronized to time server 108.59.2.24:123 (0.debian.pool.ntp.org).

Or through journalctl:

$ journalctl -u systemd-timesyncd
-- Logs begin at Thu 2016-11-03 13:16:42 EDT, end at Sun 2018-08-05 10:20:01 EDT. --
Aug 05 10:14:44 pi-hole systemd[1]: Starting Network Time Synchronization...
Aug 05 10:14:44 pi-hole systemd[1]: Started Network Time Synchronization.
Aug 05 10:14:45 pi-hole systemd-timesyncd[3714]: Synchronized to time server 108.59.2.24:123 (0.debian.pool.ntp.org).

You can also watch systemd/clock:

$ stat /var/lib/systemd/clock
  File: /var/lib/systemd/clock
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: b302h/45826d    Inode: 18750       Links: 1
Access: (0644/-rw-r--r--)  Uid: (  100/systemd-timesync)   Gid: (  103/systemd-timesync)
Access: 2018-08-05 10:22:13.838262405 -0400
Modify: 2018-08-05 10:22:13.838262405 -0400
Change: 2018-08-05 10:22:13.838262405 -0400
 Birth: -

The Access/Modify/Change times on this file show when systemd-timesynd last touched it.

Forcing a sync

It doesn't appear that there's any method to force a sync directly. Restarting the systemd-timesyncd service does in fact trigger a resync though:

$ systemctl restart systemd-timesyncd

You can see this in the logs:

$ journalctl -u systemd-timesyncd
-- Logs begin at Thu 2016-11-03 13:16:42 EDT, end at Sun 2018-08-05 10:44:37 EDT. --
Aug 05 10:14:44 pi-hole systemd[1]: Starting Network Time Synchronization...
Aug 05 10:14:44 pi-hole systemd[1]: Started Network Time Synchronization.
Aug 05 10:14:45 pi-hole systemd-timesyncd[3714]: Synchronized to time server 108.59.2.24:123 (0.debian.pool.ntp.org).
Aug 05 10:44:37 pi-hole systemd[1]: Stopping Network Time Synchronization...
Aug 05 10:44:37 pi-hole systemd[1]: Stopped Network Time Synchronization.
Aug 05 10:44:37 pi-hole systemd[1]: Starting Network Time Synchronization...
Aug 05 10:44:37 pi-hole systemd[1]: Started Network Time Synchronization.
Aug 05 10:44:37 pi-hole systemd-timesyncd[4157]: Synchronized to time server 208.75.88.4:123 (0.debian.pool.ntp.org).

The last line is from my restart.

Controlling interval

You can control the interval which this polls the NTP servers through these properties in the systemd-timesyncd config file /etc/systemd/timesyncd.conf:

   PollIntervalMinSec=, PollIntervalMaxSec=

   The minimum and maximum poll intervals for NTP messages. Each setting takes a time 
   value (in seconds). PollIntervalMinSec= must not be smaller than 16 seconds. 
   PollIntervalMaxSec= must be larger than PollIntervalMinSec=. PollIntervalMinSec= 
   defaults to 32 seconds, and PollIntervalMaxSec= defaults to 2048 seconds.

The above appears to have been added in late 2017 - timesync: make poll interval configurable #7268.

References

Related Question