vieyahn2017 / iBlog

44 stars 0 forks source link

4.11 linux网络时钟同步 #270

Closed vieyahn2017 closed 5 years ago

vieyahn2017 commented 5 years ago

linux网络时钟同步

vieyahn2017 commented 5 years ago

RFC 5905

Network Time Protocol Version 4: Protocol and Algorithms Specification (RFC 5905, June 2010)...

vieyahn2017 commented 5 years ago

rfc5905标准这个 参考https://linux.cn/article-4664-1.html?pr

同步周期 一般是2^6 ~ 2^10秒之间,2的指数次秒 (rfc5905建议NTPv同步周期范围在2^4 ~ 2^17秒,即16秒-30小时,然而观察发现这个值的实际大小在2^6 ~ 2^10秒之间)

vieyahn2017 commented 5 years ago

inux之NTP时间同步服务介绍

2016年06月14日 23:47:49 素衣白马客京华 阅读数:8760 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/judwenwen2009/article/details/51661239 前文已经介绍了linux中时间相关的概念,在大型服务器中,应用最广泛的其中最重要最精确的就是ntp服务了。

一、ntp定义

NTP(Network Time Protocol,网络时间协议)是由RFC 1305定义的时间同步协议,用来在分布式时间服务器和客户端之间进行时间同步。NTP基于UDP报文进行传输,使用的UDP端口号为123。

使用NTP的目的是对网络内所有具有时钟的设备进行时钟同步,使网络内所有设备的时钟保持一致,从而使设备能够提供基于统一时间的多种应用。

对于运行NTP的本地系统,既可以接收来自其他时钟源的同步,又可以作为时钟源同步其他的时钟,并且可以和其他设备互相同步。

二、ntp原理

介绍ntp原理的文章很多,网上一大把,在此就简单说下。

系统时钟同步的工作过程如下:

Device A发送一个NTP报文给Device B,该报文带有它离开Device A时的时间戳,该时间戳为10:00:00am(T1)。 当此NTP报文到达Device B时,Device B加上自己的时间戳,该时间戳为11:00:01am(T2)。 当此NTP报文离开Device B时,Device B再加上自己的时间戳,该时间戳为11:00:02am(T3)。 当Device A接收到该响应报文时,Device A的本地时间为10:00:03am(T4)。

这样,Device A已经拥有足够的信息来计算两个重要的参数:

NTP报文的往返时延Delay=(T4-T1)-(T3-T2)=2秒。 Device A相对Device B的时间差offset=((T2-T1)+(T3-T4))/2=1小时。

Device A就能够根据这些信息来设定自己的时钟,与Device B的时钟同步。

三、ntp在linux中的用法

我们主要介绍ntp client端

1、在client端,首先配置ntpd.conf文件,添加server ip(时间服务提供商的ip或者主机名),可以指定多个

启动ntpd服务:/etc/init.d/ntpd start

此时ntp服务启动,通过udp的123端口与server端通信,开始时间同步

2、client和server的时间同步有两种情况:时间跳变(time step)和渐变(time slew)

跳变是指在client和server间时间差过大时(默认128ms),瞬间调整client端的系统时间

渐变是指时间差较小时,通过改变client端的时钟频率,进而改变client端中“1秒”的“真实时间”,保持client端时间连续性。

举个例子,如果client端比server端慢10s,通过ntpd,client端的中每1秒现实时间是1.0005秒!虽然client端的时间仍然是1秒1秒增加的,通过调整每秒的实际时间,直到与serrver的时间相同。在这个例子中,10s/0.0005s=20000s,20000s/60/60=5.55555小时,即需要5个多小时才能消除10s的误差。

在linux中,很多应用软件依赖系统的时间连续性来正确工作,系统时间的跳变将导致软件出现意想不到的问题,所以时间渐变才是ntpd的主要应用场景。

3、那么怎么禁止ntpd的时间跳变,只采用时间渐变呢?

刚开始通过man ntpd,尝试在ntpd启动配置(/etc/sysconfig/ntpd)中加-x选项:

-xNormally, the time is slewed if the offset is less than the step threshold, which is 128 ms by default, and stepped if above the threshold. This option forces the time to be slewed in all cases. If the step threshold is set to zero, all offsets are stepped, regardless of value and regardless of the -x option. In general, this is not a good idea, as it bypasses the clock state machine which is designed to cope with large time and frequency errors Note: Since the slew rate is limited to 0.5 ms/s, each second of adjustment requires an amortization interval of 2000 s. Thus, an adjustment of many seconds can take hours or days to amortize. This option can be used with the -q option.

结果发现-x只是提高了时间跳变的阈值,在client与server时间差小于600秒时,时间的调整使用渐变,大于600秒,时间调整使用跳变形式。

查了很多资料,最后确定在ntp配置文件/etc/ntp.conf中添加字段:

tinker panic 600

这句话的意思是在时间差大于600秒的情况下,ntpd进程自动关闭,ntpd退出时会向/var/log/messages中写入log 在时间差过大时,应该由用户手动设置系统时间或者调用ntpdate命令,这样能避免因为时间跳变出现的问题。

四、ntpq的用法和参数解释

ntpq是ntp的辅助命令,用来查看ntp的运行状态


参数            功能                            示例
remote         时间服务器地址
refid          上一级ntp服务器                   可以是ip;或其他比如:JJY (LF Fukushima, Japan) time standard radio receiver;GPS
St             Stratum值                        0~16
t               Type,服务器通讯类型              U:unicast
When           上一次与服务器通讯时间
Poll           轮询间隔 <span style="white-space:pre">  </span>                       默认64
Reach          通讯成功次数                      8位二进制数,成功一次左移一位
Delay          本地与服务器通信时延
Offset         本地与服务器的时间偏差
Jitter 

在remote项前边,会现实client与server的同步状态,正常同步用“*”表示,备用服务器用"+"表示,未同步不现实,就像上图那样.

详细说明:

状态      含义
“ ” 1)non-communicating remote machines
      2)“LOCAL” for this local host
      3)(unutilised) high stratum servers
“x” Out of tolerance, do not use (discarded by intersection algorithm);
“-” Out of tolerance, do not use (discarded by the cluster algorithm);
“#” Good remote peer or server but not utilised (not among the first six peers sorted by synchronization distance, ready as a backup source);
“+” Good and a preferred remote peer or server (included by the combine algorithm);
“*” The remote peer or server presently used as the primary reference;
“o” PPS peer