simple time synchronization of a server (laptop, desktop) using built-in systemd-timesyncd service

Author:

Here we offer you a relatively new way of keeping your server’s time (or your computer and laptop) synchronized with a reliable time service on the Internet.

systemd has a built-in feature – a small daemon (systemd-timesyncd) to periodically to contact NTP servers and keep the server’s clock synchronized with them!

Of course, you must use systemd in your Linux distribution. This article is for those Linux systems using systemd, not for upstart (sysvinit, openrc, upstart, runit and so on). Most of the modern Linux distributions use the systemd like Fedora, Ubuntu, CentOS, RedHat, Gentoo, SuSe and many more.

Once there were not many options to keep your server’s clock synced with NTP servers. Now we have simpler programs (some of which by the way could act as clients only!!!) – chrony, openntpd, systemd-timesyncd and more.
This time synchronization service is not going to open server port 123, it does not have the server capabilities of an NTP server. So you won’t need any firewall rules (like for ntpd). It is a simple client service to sync your time and keep it synchronized all the time with accuracy not more than 100ms.

Do not expect complex clock discipline like training or compensating. It just sets the time according to a selected time server from the configuration file in “/etc/systemd/timesyncd.conf”. The polling interval is automatically adjusted in minimal and maximal values from the configuration file and the daemon decides which is the actual interval based on the near-term drift it thinks. Possible back running clock if it needs to set in the past. The quality of the clock source could not be checked, so

in any case, you may not expect more than 100ms accuracy.

Of course, this service is actively developed and it has already many changes from the base client once it was!

Here is how you can enable it. Here are the steps:

STEP 1) Enable the Network synchronization for the clock with timedatectl command.

root@srv:~# timedatectl status
      Local time: Thu 2019-05-30 12:22:49 UTC
  Universal time: Thu 2019-05-30 12:22:49 UTC
        RTC time: Mon 2019-01-14 02:49:02
       Time zone: UTC (UTC, +0000)
 Network time on: no
NTP synchronized: no
 RTC in local TZ: no
root@srv:~# timedatectl set-ntp true

STEP 2) Check if the Network synchronization for the clock it is enabled properly.

root@srv:~# timedatectl status
      Local time: Thu 2019-05-30 12:22:55 UTC
  Universal time: Thu 2019-05-30 12:22:55 UTC
        RTC time: Mon 2019-01-14 02:49:08
       Time zone: UTC (UTC, +0000)
 Network time on: yes
NTP synchronized: no
 RTC in local TZ: no
root@srv:~# systemctl status systemd-timesyncd
● 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: inactive (dead)
Condition: start condition failed at Thu 2019-05-30 12:22:53 UTC; 25s ago
           ConditionFileIsExecutable=!/usr/sbin/ntpd was not met
     Docs: man:systemd-timesyncd.service(8)

May 30 12:18:56 srv systemd[1]: Stopped Network Time Synchronization.
May 30 12:22:48 srv systemd[1]: Stopped Network Time Synchronization.

STEP 3) Remove any time synchronization software like ntp, chrony, openntp and VBoxService, because systemd-timesyncd won’t start, at all it these services are installed.

In our case it was ntp server (under Ubuntu server).

root@srv:~# apt remove ntp

or you can use for Ubuntu:

apt remove ntp chrony openntp

STEP 4) Restart the daemon and ensure it is running and the timedatectl reports your clock is NTP synchronized.

root@srv:~# systemctl restart systemd-timesyncd
root@srv:~# systemctl status systemd-timesyncd
● 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 Thu 2019-05-30 12:26:50 UTC; 1s ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 5909 (systemd-timesyn)
   Status: "Synchronized to time server 91.189.89.199:123 (ntp.ubuntu.com)."
   CGroup: /system.slice/systemd-timesyncd.service
           └─5909 /lib/systemd/systemd-timesyncd

May 30 12:26:50 srv systemd[1]: Stopped Network Time Synchronization.
May 30 12:26:50 srv systemd[1]: Starting Network Time Synchronization...
May 30 12:26:50 srv systemd[1]: Started Network Time Synchronization.
May 30 12:26:50 srv systemd-timesyncd[5909]: Synchronized to time server 91.189.89.199:123 (ntp.ubuntu.com).
root@srv:~# timedatectl status
      Local time: Thu 2019-05-30 12:27:04 UTC
  Universal time: Thu 2019-05-30 12:27:04 UTC
        RTC time: Mon 2019-01-14 02:53:17
       Time zone: UTC (UTC, +0000)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

disabled with time daemon

As you can see even we had enabled the clock synchronization it did not begin to synchronize because of ntpd file existence, you must remove ntp, chrony, openntp and VBoxService to be able to use the systemd-timesyncd daemon. Uninstall the programs with apt (as we did for Ubuntu) or yum or the package manager of your Linux distributions.
Here is the file, which limits the systemd-timesyncd daemon:

root@myuser-computer:~# cat /lib/systemd/system/systemd-timesyncd.service.d/disable-with-time-daemon.conf
[Unit]
# don't run timesyncd if we have another NTP daemon installed
ConditionFileIsExecutable=!/usr/sbin/ntpd
ConditionFileIsExecutable=!/usr/sbin/openntpd
ConditionFileIsExecutable=!/usr/sbin/chronyd
ConditionFileIsExecutable=!/usr/sbin/VBoxService

Show last successful synchronization

There is a special file “/var/lib/systemd/clock”, which is touched each time a successful synchronization occurs.

root@myuser-computer:~# date
Thu May 30 12:46:35 UTC 2019
root@myuser-computer:~# stat /var/lib/systemd/clock 
  File: '/var/lib/systemd/clock'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d      Inode: 11010661    Links: 1
Access: (0644/-rw-r--r--)  Uid: (  100/systemd-timesync)   Gid: (  102/systemd-timesync)
Access: 2019-05-30 12:46:26.170471071 +0000
Modify: 2019-05-30 12:42:51.353534047 +0000
Change: 2019-05-30 12:42:51.353534047 +0000
 Birth: -

This file could be on different place in different Linux distributions, but generally it exits and you should find it under /var/lib/systemd/ like in /var/lib/systemd/timesync/clock.

Newer versions of systemd

The new version of systemd has additional commands to timedatectl “show-timesync”, which will show you the NTP server and its parameters like Root distance, Precision, Stratum, Offset and more. So this feature systemd time synchronization is constantly evolving and the features it offers are advancing!

root@myuser-computer:~# timedatectl timesync-status

       Server: 91.189.89.199:123 (ntp.ubuntu.com)
Poll interval: 7min 8s (min: 32s; max 34min 8s)
         Leap: normal
      Version: 4
      Stratum: 2
    Reference: B124A31F
    Precision: 2us (-31)
Root distance: 191.128ms (max: 5s)
       Offset: -18.317ms
        Delay: 29.823ms
       Jitter: 6.412ms
 Packet count: 3
    Frequency: +201.123ppm

Example minimal configuration

The configuration file is in “/etc/systemd/timesyncd.conf”:

[Time]
NTP=0.ubuntu.pool.ntp.org 1.ubuntu.pool.ntp.org 2.ubuntu.pool.ntp.org 3.ubuntu.pool.ntp.org
FallbackNTP=ntp.ubuntu.com

or for CentOS 7 you can use the following servers:

[Time]
NTP=0.centos.pool.ntp.org 1.centos.pool.ntp.org 2.centos.pool.ntp.org 3.centos.pool.ntp.org
FallbackNTP=ntp.ubuntu.com

One thought on “simple time synchronization of a server (laptop, desktop) using built-in systemd-timesyncd service”

Leave a Reply

Your email address will not be published. Required fields are marked *