LLI Wiki — My Digital Knowledge Base

NTP time synchronisation on Linux and Windows

Commands to check, test and force clock synchronisation with ntpd, chrony, systemd-timesyncd and w32time.

Clock drift breaks Kerberos, certificates, logs and replication. This article gathers the commands to check and force time synchronisation on Linux (ntpd, systemd-timesyncd, chrony) and Windows (w32time).

Linux

Check the current time and test an NTP server

# Current time (RFC 2822 format)
date -R

# Query a server without changing the clock
ntpdate -q 192.0.2.10
ntpdate -q ntp.example.com

Force a synchronisation with ntpd

ntpdate -u ntp.example.com

systemctl restart ntp
systemctl enable ntp
systemctl start ntpd.service
systemctl enable ntpd.service

service ntpd status
ntpq -p

The configuration file is /etc/ntp.conf. See this guide for CentOS.

timedatectl

timedatectl
Local time: Tue 2016-02-02 09:47:54 MST
  Universal time: Tue 2016-02-02 16:47:54 UTC
        RTC time: Tue 2016-02-02 16:47:54
       Time zone: America/Denver (MST, -0700)
 Network time on: yes
NTP synchronized: no
 RTC in local TZ: no

Switch the hardware clock interpretation:

# RTC in UTC
sudo timedatectl set-local-rtc 0
# RTC in local time
sudo timedatectl set-local-rtc 1

On some distributions the same setting is UTC=yes or UTC=no in /etc/default/rcS.

Other useful commands:

timedatectl set-time 2014-11-04
timedatectl set-time 00:50:00
timedatectl list-timezones | grep Asia
timedatectl set-timezone Asia/Kuala_Lumpur

hwclock

# Hardware clock stores local time
hwclock --localtime
# Hardware clock stores UTC
hwclock --utc

systemd-timesyncd

The polling interval is set in /etc/systemd/timesyncd.conf.

systemctl status systemd-timesyncd.service
journalctl -u systemd-timesyncd

# Last successful update
stat /var/lib/systemd/timesync/clock

# Force a synchronisation
systemctl restart systemd-timesyncd

chrony (Red Hat family)

sudo systemctl restart chronyd
sudo systemctl status chronyd

chronyc tracking
chronyc sources
chronyc sourcestats

# One-shot query/step from a given server
chronyd -q 'server pool.ntp.org iburst'

# Step the clock immediately
sudo chronyc -a makestep

The configuration file is /etc/chrony.conf. Reference: Red Hat documentation.

CentOS 6: set the date and time zone by hand

date MMDDhhmmYYYY
ls /usr/share/zoneinfo/America/
cp /usr/share/zoneinfo/America/Toronto /etc/localtime
hwclock

Virtual machines

On a VMware guest, a conflict between VMware Tools time sync and NTP is a common cause of drift:

vmware-toolbox-cmd timesync status

Log the offset over time

#!/bin/bash
# Infinite loop: query the server every 5 seconds
while true; do
    ntpdate -q servername
    sleep 5
done

Run it in the background and keep the output: nohup ./stat_clock.sh >> clock.txt &

Windows

In a domain

net time /domain
net time /domain /set

Inspect the configuration

w32tm /query /source
w32tm /query /configuration
w32tm /query /status

Resync and restart the service

w32tm /resync
w32tm /config /update

net stop w32time
net start w32time

Test an NTP server

w32tm /stripchart /computer:ntp.example.com /dataonly /samples:5

The output shows the offset against the server for each sample (for example +03.3575094s).

Outside a domain

  1. Open Settings, then Time & Language, then Date & Time.
  2. Open "Additional date, time & regional settings", then "Date and Time".
  3. In the Internet Time tab, click "Change settings".
  4. Select time.nist.gov (or another server from the list below) and click "Update now".

The synchronisation source type is stored in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\w32time\Parameters, value Type:

Value Meaning
NTP Synchronise from the servers listed in the NtpServer registry entry
NT5DS Synchronise from the domain hierarchy
AllSync Use all available synchronisation mechanisms

Script: set the clock from a web time API

Run as administrator. It looks up the time zone and time through a public API, matches the zone by city name (there is no common identifier between the API and Windows) and applies both.

$time = Invoke-RestMethod -Uri http://worldtimeapi.org/api/ip

$timezones = Get-TimeZone -ListAvailable

# there is no global standard in names of timezones nor a unique id of them
# worldtimeapi always has city name in last part of timezone field
# microsoft has city names in display name
# therefore current solution is to match those names based on city
foreach ($timezone in $timezones) {
    if ($timezone.DisplayName.ToString() -match $time.timezone.Split("/")[-1]) {
        $current_zone = $timezone
        break
    }
}

if (!$current_zone) {throw "Could'nt find time zone. exiting."}

Set-TimeZone -Name $current_zone.StandardName
Set-Date -Date (Get-Date -Date $time.datetime)

Public NTP servers

Name IP address
time-a.nist.gov 192.0.2.11
time-b.nist.gov 192.0.2.12
time-c.nist.gov 192.0.2.13

Addresses are shown as documentation placeholders; resolve the names with DNS.

Notes

Mastodon LLIMastodon SocialFosstodon