/ linux-administration

NTP time sync with systemd-timesyncd

With systems that are using systemd[1] as init system there is no need to install ntpd daemon anymore if you only need to sync local time with your NTP server.

On Debian distro systemd-timesyncd service can be found by default from Stretch (9.0) version. So in order to make it work without using ntpd daemon or ntpdate command through cron (I've seen these also) you need a few steps:

  1. sync with specific NTP server by editing timesyncd.conf configuration file:

     
     sudo vim /etc/systemd/timesyncd.conf
     
     
    [Time]
    NTP=your.ntp.server.or.pool your.next.ntp.server.or.pool
    #FallbackNTP=
    
    • more on these options can be found in man pages of timesyncd.conf
        NTP=
          A space-separated list of NTP server host names or IP
          addresses. During runtime this list is combined with any
          per-interface NTP servers acquired from systemd-
          networkd.service(8). systemd-timesyncd will contact all
          configured system or per-interface servers in turn until
          one is found that responds. This setting defaults to an
          empty list.
          
        FallbackNTP=
          A space-separated list of NTP server host names or IP
          addresses to be used as the fallback NTP servers. Any
          per-interface NTP servers obtained from systemd-
          networkd.service(8) take precedence over this setting, as
          do any servers set via NTP= above. This setting is hence
          only used if no other NTP server information is known. If
          this option is not given, a compiled-in list of NTP
          servers is used instead.
      
  2. after editing just restart service:

      
      sudo service systemd-timesyncd restart
      
      
  3. check service status and it will show if service is running and state of NTP synchronization.

      
          sudo service systemd-timesyncd status
          ● 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 2017-09-26 12:16:19 CEST; 6min ago
         Docs: man:systemd-timesyncd.service(8)
         Main PID: 241 (systemd-timesyn)
         Status: "Synchronized to time server 185.143.194.7:123 (hr.pool.ntp.org)."
         Tasks: 2 (limit: 4915)
         CGroup: /system.slice/systemd-timesyncd.service
                 └─241 /lib/systemd/systemd-timesyncd
         Sep 26 12:16:19 test.machine systemd[1]: Starting Network Time Synchronization...
         Sep 26 12:16:19 test.machine systemd[1]: Started Network Time Synchronization.
         Sep 26 12:16:51 test.machine systemd-timesyncd[241]: Synchronized to time server 185.143.194.7:123 (hr.pool.ntp.org).
      
      
    • also if ntpd is installed it needs to be removed because timesyncd will complain and not start:

      Condition: start condition failed at Tue 2017-09-26 12:55:57 CEST; 2h 29min ago
                 └─ ConditionFileIsExecutable=!/usr/sbin/ntpd was not met
      
      • same thing will happen if you are setting this on virtual machine and there are guest tools responsible for time synchronization.
    • if there are no ntp tools or something else you can use timedatectl command:

        
        timedatectl 
             Local time: Tue 2017-09-26 15:25:48 CEST
         Universal time: Tue 2017-09-26 13:25:48 UTC
               RTC time: Tue 2017-09-26 13:25:48
              Time zone: Europe/Zagreb (CEST, +0200)
        Network time on: yes
       NTP synchronized: yes
        RTC in local TZ: no
        
        

That's about it. Using timdatectl command is useful if you need a script for checking that your servers are in sync with ntp server. So you can run check from central point and see time state on all your servers.


  1. https://www.freedesktop.org/wiki/Software/systemd/ ↩︎