How to Compare Local Server Time with NTP Server Time Using ntpd


1 views

When running ntpd on your server, you can check the time difference between your local system and its configured NTP servers using the ntpq utility:

ntpq -p

This will display a table showing the time offset (in milliseconds) between your server and each NTP peer in the offset column.

For a one-time comparison with a specific NTP server like ntp.ubuntu.com:

ntpdate -q ntp.ubuntu.com

Sample output:

server 91.189.94.4, stratum 2, offset 0.003647, delay 0.04618
25 Apr 16:23:44 ntpdate[1234]: adjust time server 91.189.94.4 offset 0.003647 sec

To see your server's current time drift and synchronization status:

ntpstat

For more detailed synchronization information:

ntptime

If using chrony instead of ntpd:

chronyc tracking
chronyc sources

Here's a bash script to log time differences periodically:

#!/bin/bash
while true; do
    offset=$(ntpdate -q ntp.ubuntu.com | grep offset | awk '{print $10}')
    echo "$(date) - Time offset: $offset seconds" >> /var/log/ntp_offset.log
    sleep 3600
done

For significant time differences (> 5 minutes), you may need to force a time update:

service ntpd stop
ntpdate ntp.ubuntu.com
service ntpd start

When you need to verify the time synchronization between your local server and an NTP server like ntp.ubuntu.com, the ntpq utility (included with ntpd) provides the most accurate comparison:

ntpq -pn

This will display output similar to:

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*time.cloudflare 10.11.12.13     3 u   12   64  377    1.234   -0.567   0.891
 ntp.ubuntu.com  .POOL.          16 u    - 1024    0    0.000    0.000   0.000

Focus on these columns in the output:

  • offset: The time difference in milliseconds between your server and the NTP server
  • delay: Network round-trip time to the server
  • jitter: Variation in network latency

If you need a quick one-time comparison without relying on the running ntpd daemon:

ntpdate -q ntp.ubuntu.com

Sample output:

server 91.189.89.198, stratum 2, offset 0.002345, delay 0.14567
28 Apr 15:20:21 ntpdate[1234]: adjust time server 91.189.89.198 offset 0.002345 sec

For regular monitoring, you can create a simple bash script:

#!/bin/bash
OFFSET=$(ntpq -pn | awk '/^*/ {print $9}')
THRESHOLD=100 # milliseconds
if (( $(echo "$OFFSET > $THRESHOLD" | bc -l) )); then
  echo "WARNING: Time offset too large - $OFFSET ms"
  exit 1
else
  echo "Time sync OK - Offset: $OFFSET ms"
  exit 0
fi

Remember that:

  • Network conditions affect measurement accuracy
  • Large offsets may indicate ntpd hasn't fully synchronized yet
  • Firewalls can interfere with NTP traffic (UDP port 123)

When using ntp.ubuntu.com (part of the NTP Pool Project):

  • The actual server IP may change due to DNS rotation
  • Multiple servers are used for redundancy
  • Consider using specific pool zone servers (e.g., 0.ubuntu.pool.ntp.org) for better consistency