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
- Step 1
1.GPS receiver / atomic clock = stratum 0 (reference clock)
- Step 2
2.Server directly attached to it = stratum 1
- Step 3
3.Office time server syncing from stratum 1 = stratum 2
- Step 4
4.Office PCs syncing from the office server = stratum 3
- Step 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
- Step 1
1.Client sends a request to UDP 123 and notes t1 (its send time)
- Step 2
2.Server notes t2 when the request arrives
- Step 3
3.Server notes t3 when it sends the reply
- Step 4
4.Client notes t4 when the reply arrives
- Step 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.
| Point | Slew | Step |
|---|---|---|
| How | Clock runs slightly faster/slower until correct | Clock is set to the right time in one go |
| Time jumps? | No, smooth | Yes, can even go backwards |
| When | Normal small offsets (chrony default) | Large offsets; chrony makestep decides the threshold |
265.5 Comparison: chrony vs ntpd vs w32time
| Tool | OS | Notes |
|---|---|---|
| chrony | Linux (modern default) | chronyd daemon + chronyc command tool |
| ntpd | Linux (classic) | Older reference implementation |
| w32time | Windows | Windows Time service on UDP 123; slews small offsets, sets the clock directly for very large ones |
- Step 1
1.External NTP server or hardware clock
- Step 2
2.PDC emulator of the forest root domain
- Step 3
3.Other domain controllers follow the hierarchy
- Step 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.
w32tm /?w32tm /tzPS 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.
# 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[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.
chronyc burst 2/2tcpdump -n -v -i eth1 -c 4 udp port 123chronyc ntpdata 192.168.65.10[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).