SkillPilotAI

NTP and time sync

NTP (Network Time Protocol) keeps the clocks of every machine on a network in step. If clocks drift apart, log timelines, Kerberos logins and HTTPS certificate checks can all break at the same time.

265.1 Why is it used?

  • Logs: during an incident you line up logs from many servers in time order; a wrong clock gives a wrong order (db1 7 minutes behind web1).
  • Kerberos: uses timestamps to stop replay attacks; Microsoft default tolerance is 5 minutes, so a 7-minute skew gives 'clock skew too great'.
  • TLS: certificates have valid-from / valid-till; a clock that is behind sees a fresh certificate as 'not yet valid'.

265.2 The NTP stratum hierarchy

Stratum levels
  1. 1.GPS receiver / atomic clock = stratum 0 (reference clock)

  2. 2.Server directly attached to it = stratum 1

  3. 3.Office time server syncing from stratum 1 = stratum 2

  4. 4.Office PCs syncing from the office server = stratum 3

  5. 5.Each level adds 1; up to 15 is valid, 16 = unsynchronized (RFC 5905)

  • Stratum says how many hops away the reference clock is, not how accurate the clock is.
  • NTP runs on UDP port 123.

265.3 How it works: offset and delay

One NTP request and reply
  1. 1.Client sends a request to UDP 123 and notes t1 (its send time)

  2. 2.Server notes t2 when the request arrives

  3. 3.Server notes t3 when it sends the reply

  4. 4.Client notes t4 when the reply arrives

  5. 5.Delay = (t4 - t1) - (t3 - t2); Offset = ((t2 - t1) + (t3 - t4)) / 2

NTP assumes the path takes the same time in both directions

  • Example from the video: round trip 22 ms minus 2 ms server time = 20 ms delay.
  • Direction gaps 60 ms and 40 ms average to a 50 ms offset: the client is 50 ms behind the server.
  • An asymmetric path (different time each way) adds error to the offset.

265.4 Drift and correction: slew vs step

  • Every clock runs on a quartz crystal that is not perfect; running fast or slow is called drift.
  • A clock 50 ppm fast gains about 4 seconds a day and more than 2 minutes a month, so NTP measures the offset at every poll.
PointSlewStep
HowClock runs slightly faster/slower until correctClock is set to the right time in one go
Time jumps?No, smoothYes, can even go backwards
WhenNormal small offsets (chrony default)Large offsets; chrony makestep decides the threshold

265.5 Comparison: chrony vs ntpd vs w32time

ToolOSNotes
chronyLinux (modern default)chronyd daemon + chronyc command tool
ntpdLinux (classic)Older reference implementation
w32timeWindowsWindows Time service on UDP 123; slews small offsets, sets the clock directly for very large ones
w32time in a domain
  1. 1.External NTP server or hardware clock

  2. 2.PDC emulator of the forest root domain

  3. 3.Other domain controllers follow the hierarchy

  4. 4.Domain PCs sync from their DC (so Kerberos works)

A non-domain Windows PC syncs with time.windows.com by default

265.6 w32tm on Windows (read-only)

  • w32tm /? shows help; syncfromflags DOMHIER means sync from the domain hierarchy.
  • w32tm /tz shows only the time zone; /query /status and /query /peers show the real time source.
PowerShell
w32tm /?w32tm /tz
Output
PS C:\> w32tm /tzTime zone: Current:TIME_ZONE_ID_UNKNOWN Bias: -330min (UTC=LocalTime+Bias)  [Standard Name:"India Standard Time" Bias:0min Date:(unspecified)]  [Daylight Name:"India Daylight Time" Bias:-60min Date:(unspecified)]

265.7 Lab: chrony server and client

  • Closed Docker network n265-lan (192.168.65.0/24): timesrv = .10, client = .20.
  • Server config: local stratum 8 + allow (serves its own clock at stratum 8, only to the lab subnet).
  • Client config: server 192.168.65.10 iburst.
  • chronyd -x: does not touch the system clock, only tracks the offset.
Terminal
# timesrv 192.168.65.10 and client 192.168.65.20cat /etc/chrony/n265.confchronyd -x -f /etc/chrony/n265.conf# on the clientchronyc sources -vchronyc tracking
Output
[client]$ chronyc trackingReference ID    : C0A8410A (n265-timesrv.n265-lan)Stratum         : 9System time     : 0.000066274 seconds slow of NTP timeLast offset     : +0.000027609 secondsFrequency       : 3.965 ppm slowRoot delay      : 0.000082761 secondsLeap status     : Normal
  • chronyc sources: ^* = server source and current best; stratum 8; reach 17 (octal) = last four polls answered.
  • Client stratum 9 = server stratum + 1; Frequency 3.965 ppm slow is the drift of this clock.
Terminal
chronyc burst 2/2tcpdump -n -v -i eth1 -c 4 udp port 123chronyc ntpdata 192.168.65.10
Output
[client]$ chronyc ntpdata 192.168.65.10Remote address  : 192.168.65.10 (C0A8410A)Remote port     : 123Stratum         : 8Poll interval   : 6 (64 seconds)Offset          : -0.000031184 secondsPeer delay      : 0.000082761 secondsTotal TX        : 4
  • tcpdump showed 48-byte NTPv4 packets from a random client port to server port 123; the reply's Receive Timestamp = t2, Transmit Timestamp = t3 (about 172 microseconds apart).

265.8 Common mistakes

  • Thinking the right time zone means the right time: the zone is only a display offset (IST = UTC+5:30); NTP works in UTC.
  • Blocking UDP 123 on the firewall and then wondering why clocks drift.
  • Stepping a production clock backwards without thinking about logs and databases.
  • Treating a low stratum number as a guarantee of accuracy.

265.9 Interview tips

  • Delay = (t4 - t1) - (t3 - t2); offset = ((t2 - t1) + (t3 - t4)) / 2; assumes a symmetric path.
  • Client shows stratum 9 -> its server is stratum 8 (+1 per level).
  • Kerberos default skew tolerance is 5 minutes; in a domain the PDC emulator of the forest root is the top time source.
  • Slew = gradual, no jump; step = instant jump (chrony makestep).