How Network Time Protocol (NTP) works

What is NTP?
Network Time Protocol (NTP) is a time synchronization protocol defined by RFC 1305. It is used to synchronize time between distributed time servers and clients. NTP is based on UDP insulation and uses UDP port number 123.
The purpose of using NTP is to synchronize clocks of all devices with clocks on the network, to make the clocks of all devices on the network consistent so that the devices can provide multiple applications based on a unified time.
For a local system running NTP, it can accept synchronization from other clock sources, and it can also be used as a clock source to synchronize other clocks, and other devices can synchronize with each other.
How NTP works
The figure shows the basic working principle of NTP. Device A and Device B are connected through a network. They both have their own independent system clocks, and they need to implement automatic synchronization of their system clocks through NTP. To facilitate understanding, the following assumptions are made:
  1. Device A sends an NTP message to device B. The message carries a timestamp when it leaves device A. The timestamp is 10:00:00 am (T1).
  2. When this NTP message arrives at device B, device B adds its own timestamp. The timestamp is 11:00:01 am (T2).
  3. When this NTP message leaves device B, device B adds its own timestamp and changes the timestamp to 11:00:02 (T3).
  4. When device A receives the response message, the local time of device A is 10:00:03 am (T4).
  5. At this point, device A has enough information to calculate two important parameters:
    1. The round-trip delay of NTP packets is Delay = (T4-T1)-(T3-T2) = 2 seconds.
    2. The time difference between device A and device B is offset = ((T2-T1) + (T3-T4))/ 2 = 1 hour.
In this way, device A can set its own clock based on this information and synchronize it with the clock of device B. The above content is only a rough description of the working principle of NTP. For more detailed information, please refer to RFC 1305.
NTP working mode
  • In a client-server basis;
  • In a peer to peer mode;
  • Sending the time using broadcast/multicast.

Users can choose the appropriate working mode according to their needs. When the IP address of the server or peer cannot be determined, and there are many devices that need to be synchronized on the network, the clock synchronization can be achieved through broadcast or multicast mode; in the server and peer mode, the device starts from the specified server or peer.