Time sync: w32tm, chronyc, timedatectl, ntpdate, ntpq, ntpdig, sntp (2/2)
Part 2 of the time sync tools: timedatectl on systemd Linux, ntpdate and macOS sntp for quick offset checks, ntpq for reading the internals of an ntpd server, and ntpdig, the NTPsec SNTP client. The focus is checking time without touching the clock.
268.1 Why is it used?
- Quickly check how far a clock is off (ntpdate -q, ntpdig, sntp) without changing it.
- See time zone and whether NTP sync is on (timedatectl).
- Debug an ntpd server: its peers, variables, counters and which clients use it (ntpq).
268.2 How it works: mode 3/4 vs mode 6
- Step 1
1.Client 192.168.68.20 sends a mode 3 request to UDP 123 carrying T1
- Step 2
2.n268-timesrv 192.168.68.10 (NTPsec ntpd) adds T2 and T3
- Step 3
3.Mode 4 reply returns; client notes T4
- Step 4
4.Client computes offset and delay and only prints them
The clock is not changed
- ntpq sends a mode 6 control query that reads server variables (stratum, offset, peers), not the time.
- restrict lines decide who may query; the lab allows only 192.168.68.0/24.
- chrony does not serve mode 6, so ntpq needs ntpd; for chrony use chronyc.
| Tool | Where | Job |
|---|---|---|
| timedatectl | Every systemd distro, built-in | Status, time zone, NTP on/off |
| ntpdate | ntpsec-ntpdate (shell script emulating old ntpdate) | One-shot query (-q) or set |
| sntp | macOS 11+ built-in | Query, set or slew |
| ntpq | ntpsec (or classic ntp) | Mode 6: peers, variables, stats |
| ntpdig | ntpsec-ntpdig | SNTP client, JSON output |
268.3 timedatectl
- No command = status: local time, universal time, time zone, NTP sync state.
- set-time, set-timezone, set-local-rtc, set-ntp change settings; timesync-status / show-timesync are for systemd-timesyncd.
- Options: -p NAME --value (one value for scripts), -H user@host (SSH), -M container, --monitor (live).
- In a container systemd is not init, so status gives an error that the system was not booted with systemd.
Terminal
timedatectltimedatectl show -p NTPSynchronized --valuetimedatectl timesync-statusOutput
$ timedatectl Local time: Thu 2017-09-21 16:08:56 CEST Universal time: Thu 2017-09-21 14:08:56 UTC RTC time: Thu 2017-09-21 14:08:56 Time zone: Europe/Warsaw (CEST, +0200)System clock synchronized: yes NTP service: active RTC in local TZ: no268.4 Lab: a time server that never changes the clock
- tos orphan 8: ntpd serves time at stratum 8 without any upstream.
- disable ntp: clock discipline off, the server never changes its clock.
- restrict default ignore + query permission only for the lab subnet; no SYS_TIME, so ntp_adjtime is 'not permitted' (as intended).
Terminal
tos orphan 8 orphanwait 1disable ntprestrict default ignorerestrict -6 default ignorerestrict 127.0.0.1restrict ::1restrict 192.168.68.0 mask 255.255.255.0 nomodify# start: ntpd -n -c /etc/ntp.conf268.5 ntpdate and macOS sntp
| Option | ntpdate | sntp (macOS) |
|---|---|---|
| Query only | -q | sntp HOST |
| Step the clock | -b (settimeofday) | -S (offset > 50 ms) |
| Slew the clock | -B (adjtime) | -s (offset < 50 ms) |
| Timeout | -t 3 | -t SEC |
| Other | -d, -s, -u, -v, -o 1-4, -4/-6, -a, -k | -d, -g MS, -n, -r, -z |
Terminal
ntpdate -q 192.168.68.10Output
[client]$ ntpdate -q 192.168.68.102026-11-10 21:29:57.70112 (+0000) -0.000015 +/- 0.000128 192.168.68.10 s8 no-leap(exit 0)- -q needs no root; output: time, offset (15 microseconds), error bound, server, stratum 8, no-leap.
- macOS sntp is Apple's simple client from macOS 11, not the Network Time Foundation sntp; sntp -sS = slew small, step large.
268.6 ntpq: peers and associations
- -p lists peers (reverse DNS names), -n shows IPs, -c runs a command, host ... queries other hosts.
- Tally codes: * system peer, + combined, - cluster reject, x reject, # backup.
- apeers adds the association ID (17767 = sys peer on the client); associations, pstats ID for one peer.
- Interactive internal commands (host, hostnames, keyid, timeout, > FILE) change only ntpq, nothing on the server.
Terminal
ntpq -pnntpq -c apeersOutput
remote refid st t when poll reach delay offset jitter===============================================================================*192.168.68.10 127.0.0.1 8 u 6 16 7 0.0819 0.0153 0.0052 remote refid assid st t when poll reach delay offset jitter===============================================================================*n268-timesrv.n2 ... 17767 8 u - 16 17 0.1122 0.0200 0.0064268.7 ntpq: variables, statistics and MRU list
- rv (readvar) reads system variables of association 0; rv with names returns only those values (great for monitoring).
- Server rv: leap 00, stratum 8, refid 127.0.0.1 (orphan mode = its own source); client rv: leap 11 = alarm (discipline off).
- writevar, :config, reslist, ifstats need authentication; without a key reslist gives Invalid key identifier (good).
- sysstats: protocol counters (server got 12 packets, restricted and rate-limited 0).
- mrulist: which clients use the server; sort=-count puts the busiest first; limited / kod show rate-limited clients.
Terminal
ntpq -c 'rv 0 offset,stratum,refid'ntpq -c 'rv &1 srcadr,stratum,reach,offset,delay'ntpq -c reslist 192.168.68.10ntpq -c mrulist 192.168.68.10Output
stratum=9, refid=192.168.68.10, offset=0.0srcadr=192.168.68.10, stratum=8, reach=017, delay=0.112208, offset=0.020017***Invalid key identifierCtrl-C will stop MRU retrieval and display partial results. lstint avgint rstr r m v count score drop rport remote address===================================================================== 0 3.75 80 . 6 2 14 0.358 0 41210 client.lab.local268.8 ntpdig (NTPsec SNTP client)
- Without -S/-s/-M it only prints the offset (22 microseconds in the lab).
- -j gives JSON; "adjusted": false proves the clock was not changed.
- -p N takes N samples and uses the best; -t timeout (default 5); -c queries all IPs of a pool; -S/-s need root.
- Empty lab address with -t 2 gives 'no eligible servers' and exit 1.
Terminal
ntpdig 192.168.68.10ntpdig -j 192.168.68.10ntpdig -t 2 192.168.68.99Output
[client]$ ntpdig -t 2 192.168.68.99ntpdig: no eligible servers(exit 1)- Step 1
1.Only check the offset -> ntpdate -q or ntpdig (no root)
- Step 2
2.On a Mac -> sntp HOST
- Step 3
3.Zone / NTP on-off on systemd -> timedatectl
- Step 4
4.Inside an ntpd server -> ntpq (mode 6)
- Step 5
5.Inside chronyd -> chronyc
- Step 6
6.Really set the clock -> ntpdig -S / sntp -sS (root)
268.9 Common mistakes
- Using ntpq against a chrony server: chrony does not serve mode 6.
- restrict lines blocking your own query: the first lab try got 'Request timed out' on localhost until ::1 was allowed.
- Running ntpdate without -q (or ntpdig -S) when you only wanted to check.
- Expecting timedatectl to work inside a container without systemd.
268.10 Interview tips
- Offset = ((T2 - T1) + (T3 - T4)) / 2; delay = (T4 - T1) - (T3 - T2).
- Check without touching the clock: ntpdate -q or ntpdig ('adjusted': false).
- ntpq = mode 6 for ntpd; chronyc for chrony.
- ntpq mrulist answers 'who is using my NTP server?'.