ntp工作过程

本文通过`tcpdump`捕获NTP协议交互数据包,展示了在CentOS系统中如何进行时间同步的过程。客户端首先向多个NTP服务器发送时间校验请求,收到服务器响应后,经过多次交互,最终成功调整了系统时间,实现了与服务器时间的精确同步。
[root@centos6 logs]# tcpdump port 123 -vv
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
15:26:02.417921 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 76)
    172.18.110.101.ntp > 114.80.81.1.ntp: [bad udp cksum 8256!] NTPv4, length 48                            -->客户端发起时间校验,udp校验和错误,但是不影响使用
        Client, Leap indicator: clock unsynchronized (192), Stratum 0 (unspecified), poll 6s, precision -19
        Root Delay: 0.000000, Root dispersion: 0.002975, Reference-ID: (unspec)
          Reference Timestamp:  0.000000000
          Originator Timestamp: 3547953085.375634335 (2012/06/06 14:31:25)
          Receive Timestamp:    3546660296.572161377 (2012/05/22 15:24:56)                                   -->客户端的当前时间
          Transmit Timestamp:   3546660362.417906163 (2012/05/22 15:26:02)
            Originator - Receive Timestamp:  -1292788.803472957                                                            -->与服务器的时间差
            Originator - Transmit Timestamp: -1292722.957728171
15:26:02.573882 IP (tos 0x0, ttl 46, id 44778, offset 0, flags [none], proto UDP (17), length 76)
    114.80.81.1.ntp > 172.18.110.101.ntp: [udp sum ok] NTPv4, length 48
        Server, Leap indicator:  (0), Stratum 2 (secondary reference), poll 6s, precision -19
        Root Delay: 0.141281, Root dispersion: 0.013046, Reference-ID: clock.isc.org
          Reference Timestamp:  3547953071.150161125 (2012/06/06 14:31:11)                                 -->服务器返回的时间戳
          Originator Timestamp: 3546660362.417906163 (2012/05/22 15:26:02)
          Receive Timestamp:    3547953151.380694855 (2012/06/06 14:32:31)
          Transmit Timestamp:   3547953151.380723113 (2012/06/06 14:32:31)
            Originator - Receive Timestamp:  +1292788.962788691                                                           -->服务器比客户端时间快的秒数
            Originator - Transmit Timestamp: +1292788.962816949
15:27:03.451400 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 76)                  -->向另外一台服务器发起对时请求
    172.18.110.101.ntp > 216.45.57.38.ntp: [bad udp cksum 814c!] NTPv4, length 48
        Client, Leap indicator: clock unsynchronized (192), Stratum 0 (unspecified), poll 6s, precision -19
        Root Delay: 0.000000, Root dispersion: 0.003890, Reference-ID: (unspec)
          Reference Timestamp:  0.000000000
          Originator Timestamp: 3547953147.470567139 (2012/06/06 14:32:27)
          Receive Timestamp:    3546660358.699125226 (2012/05/22 15:25:58)
          Transmit Timestamp:   3546660423.451383206 (2012/05/22 15:27:03)
            Originator - Receive Timestamp:  -1292788.771441912
            Originator - Transmit Timestamp: -1292724.019183933
15:27:03.451433 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 76)                     -->向第三台发起对时请求
    172.18.110.101.ntp > mail.melancthon.net.ntp: [bad udp cksum 2bb7!] NTPv4, length 48
        Client, Leap indicator: clock unsynchronized (192), Stratum 0 (unspecified), poll 6s, precision -19
        Root Delay: 0.000000, Root dispersion: 0.003890, Reference-ID: (unspec)
          Reference Timestamp:  0.000000000
          Originator Timestamp: 3547953147.604954961 (2012/06/06 14:32:27)
          Receive Timestamp:    3546660358.823149831 (2012/05/22 15:25:58)
          Transmit Timestamp:   3546660423.451426872 (2012/05/22 15:27:03)
            Originator - Receive Timestamp:  -1292788.781805130
            Originator - Transmit Timestamp: -1292724.153528089
15:27:03.843280 IP (tos 0x0, ttl 45, id 0, offset 0, flags [DF], proto UDP (17), length 76)
    mail.melancthon.net.ntp > 172.18.110.101.ntp: [udp sum ok] NTPv4, length 48
        Server, Leap indicator:  (0), Stratum 2 (secondary reference), poll 6s, precision -20
        Root Delay: 0.001541, Root dispersion: 0.044952, Reference-ID: clock.nyc.he.net
          Reference Timestamp:  3547951252.414146931 (2012/06/06 14:00:52)
          Originator Timestamp: 3546660423.451426872 (2012/05/22 15:27:03)
          Receive Timestamp:    3547953212.622200572 (2012/06/06 14:33:32)
          Transmit Timestamp:   3547953212.622218973 (2012/06/06 14:33:32)
            Originator - Receive Timestamp:  +1292789.170773700
            Originator - Transmit Timestamp: +1292789.170792100
15:27:07.419467 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 76)
    172.18.110.101.ntp > 114.80.81.1.ntp: [bad udp cksum aebc!] NTPv4, length 48
        Client, Leap indicator: clock unsynchronized (192), Stratum 0 (unspecified), poll 6s, precision -19
        Root Delay: 0.000000, Root dispersion: 0.003952, Reference-ID: (unspec)
          Reference Timestamp:  0.000000000
          Originator Timestamp: 3547953151.380723113 (2012/06/06 14:32:31)
          Receive Timestamp:    3546660362.573881259 (2012/05/22 15:26:02)
          Transmit Timestamp:   3546660427.419453041 (2012/05/22 15:27:07)
            Originator - Receive Timestamp:  -1292788.806841853
            Originator - Transmit Timestamp: -1292723.961270072
15:27:07.575183 IP (tos 0x0, ttl 46, id 29686, offset 0, flags [none], proto UDP (17), length 76)
    114.80.81.1.ntp > 172.18.110.101.ntp: [udp sum ok] NTPv4, length 48
        Server, Leap indicator:  (0), Stratum 2 (secondary reference), poll 6s, precision -19
        Root Delay: 0.141281, Root dispersion: 0.014022, Reference-ID: clock.isc.org
          Reference Timestamp:  3547953071.150161125 (2012/06/06 14:31:11)
          Originator Timestamp: 3546660427.419453041 (2012/05/22 15:27:07)
          Receive Timestamp:    3547953216.384588356 (2012/06/06 14:33:36)
          Transmit Timestamp:   3547953216.384617924 (2012/06/06 14:33:36)
            Originator - Receive Timestamp:  +1292788.965135315
            Originator - Transmit Timestamp: +1292788.965164883
14:33:53.467969 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 76)
    172.18.110.101.ntp > 114.80.81.1.ntp: [bad udp cksum 2535!] NTPv4, length 48
        Client, Leap indicator: clock unsynchronized (192), Stratum 0 (unspecified), poll 6s, precision -19
        Root Delay: 0.000000, Root dispersion: 0.000244, Reference-ID: (unspec)
          Reference Timestamp:  0.000000000                                                                                                        -->客户端已经完成时间调整
          Originator Timestamp: 0.000000000
          Receive Timestamp:    0.000000000
          Transmit Timestamp:   3547953233.467953163 (2012/06/06 14:33:53)
            Originator - Receive Timestamp:  0.000000000
            Originator - Transmit Timestamp: 3547953233.467953163 (2012/06/06 14:33:53)
14:33:53.623376 IP (tos 0x0, ttl 46, id 41881, offset 0, flags [none], proto UDP (17), length 76)
    114.80.81.1.ntp > 172.18.110.101.ntp: [udp sum ok] NTPv4, length 48
        Server, Leap indicator:  (0), Stratum 2 (secondary reference), poll 6s, precision -19
        Root Delay: 0.141281, Root dispersion: 0.014282, Reference-ID: clock.isc.org
          Reference Timestamp:  3547953071.150161125 (2012/06/06 14:31:11)
          Originator Timestamp: 3547953233.467953163 (2012/06/06 14:33:53)
          Receive Timestamp:    3547953233.546174910 (2012/06/06 14:33:53)
          Transmit Timestamp:   3547953233.546201410 (2012/06/06 14:33:53)
            Originator - Receive Timestamp:  +0.078221747
            Originator - Transmit Timestamp: +0.078248247
14:34:01.462939 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 76)
    172.18.110.101.ntp > 216.45.57.38.ntp: [bad udp cksum 9328!] NTPv4, length 48
        Client, Leap indicator: clock unsynchronized (192), Stratum 0 (unspecified), poll 6s, precision -19
        Root Delay: 0.000000, Root dispersion: 0.000366, Reference-ID: (unspec)
          Reference Timestamp:  0.000000000
          Originator Timestamp: 0.000000000
          Receive Timestamp:    0.000000000
          Transmit Timestamp:   3547953241.462924544 (2012/06/06 14:34:01)
            Originator - Receive Timestamp:  0.000000000
            Originator - Transmit Timestamp: 3547953241.462924544 (2012/06/06 14:34:01)
14:34:01.722198 IP (tos 0x0, ttl 49, id 18438, offset 0, flags [none], proto UDP (17), length 76)
    216.45.57.38.ntp > 172.18.110.101.ntp: [udp sum ok] NTPv4, length 48
        Server, Leap indicator:  (0), Stratum 2 (secondary reference), poll 6s, precision -19
        Root Delay: 0.009826, Root dispersion: 0.025421, Reference-ID: clock.sjc.he.net
          Reference Timestamp:  3547952884.102668383 (2012/06/06 14:28:04)
          Originator Timestamp: 3547953241.462924544 (2012/06/06 14:34:01)
          Receive Timestamp:    3547953241.612733928 (2012/06/06 14:34:01)
          Transmit Timestamp:   3547953241.612783601 (2012/06/06 14:34:01)
            Originator - Receive Timestamp:  +0.149809383
            Originator - Transmit Timestamp: +0.149859056
以下命令在抓包的同时在另一个终端上运行
[root@centos6 logs]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 mail.melancthon 209.51.161.238   2 u    3   64    1  374.339  1292788   0.002
 216.45.57.38    216.218.254.202  2 u    2   64    1  277.786  1292788   0.002
 114.80.81.1     204.152.184.72   2 u    1   64    1  155.659  1292788   0.002
[root@centos6 logs]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 mail.melancthon 209.51.161.238   2 u   10   64    1  374.339  1292788   0.002
 216.45.57.38    216.218.254.202  2 u    9   64    1  277.786  1292788   0.002
 114.80.81.1     204.152.184.72   2 u    8   64    1  155.659  1292788   0.002
[root@centos6 logs]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 mail.melancthon 209.51.161.238   2 u   24   64    1  374.339  1292788   0.002
 216.45.57.38    216.218.254.202  2 u   23   64    1  277.786  1292788   0.002
 114.80.81.1     204.152.184.72   2 u   22   64    1  155.659  1292788   0.002
[root@centos6 logs]# date
2012年 05月 22日 星期二 15:23:15 CST
[root@centos6 logs]# date
2012年 05月 22日 星期二 15:25:00 CST
[root@centos6 ~]# date
2012年 05月 22日 星期二 15:26:58 CST
[root@centos6 ~]# date                                                               -->现在还是错误的时间,运行完这条命令后时间就已经自动修改正过来了
2012年 05月 22日 星期二 15:27:04 CST
[root@centos6 ~]# ntpq -p                                                          -->与服务器已经没有时间差了
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 mail.melancthon .STEP.          16 u 1975   64    0    0.000    0.000   0.002
 216.45.57.38    .STEP.          16 u  343   64    0    0.000    0.000   0.002
 114.80.81.1     .STEP.          16 u  156   64    0    0.000    0.000   0.002
[root@centos6 ~]# date
2012年 06月 06日 星期三 14:33:59 CST                                  -->再看时间已经是正确的了
[root@centos6 ~]# date
2012年 06月 06日 星期三 14:34:01 CST
### NTP 和 PTP 的工作原理 #### NTP (Network Time Protocol) NTP 是一种用于同步计算机时间的网络协议,能够将所有参与设备的时间同步至协调世界时(UTC)。该协议采用分层架构,由多个层次组成,每一级都有自己的时间服务器。最顶层通常是原子钟或其他高精度计时器作为参考源。 当客户端请求时间更新时,会向最近的一台上级服务器发送UDP报文;收到回复后计算往返延迟并调整本地时钟以匹配所获取的标准时刻[^3]。整个过程涉及复杂的算法来估计传播延时和漂移率,进而提高最终结果的准确性。 ```python import ntplib from time import ctime def query_ntp_server(): client = ntplib.NTPClient() response = client.request('pool.ntp.org') print(f"NTP Server Response: {ctime(response.tx_time)}") query_ntp_server() ``` #### PTP (Precision Time Protocol) 相比之下,PTP 提供更高的时间同步精度,特别适合那些对于时间敏感的应用场景,比如工业自动化控制系统或者金融市场交易系统。基于 IEEE 1588 标准定义的消息交换流程,使得即使在网络环境中也能达到亚微秒级别的精准度[^4]。 具体来说,PTP 主要依靠边界时钟(Boundary Clocks, BC) 或者透明时钟(Transparent Clocks, TC),前者负责过滤掉不必要的消息转发给下游节点,后者则记录下经过路径上的累积延迟以便后续补偿修正。此外,还存在普通时钟(Ordinary Clocks) 参与到多播组里定期广播Sync、Follow_Up等类型的事件消息完成周期性的对齐操作。 ```c++ #include <iostream> // Assume necessary includes and definitions here... int main() { // Example of initializing a PTP clock instance. ptp_clock* clk; int ret; ret = ptp_init(&clk); if (!ret) { std::cout << "PTP Clock Initialized Successfully." << std::endl; } else { std::cerr << "Failed to initialize PTP clock." << std::endl; } return ret; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值