Total Pageviews

Sunday 15 July 2018

网络时钟同步协议-- NTP, PTP

这篇文章介绍一下两个时钟同步的网络协议:NTP和PTP。
这里不涉及协议的原理和具体实现(想了解的可自行Google),重点是如何搭建起这两个服务。

1. NTP及PTP简介


NTP(Network Time Protocol)是用于不同计算机之间同步时钟的网络协议。
它的设计目标是使所有的互连的机器之间的时钟与UTC时间只相差若干毫秒
目前NTP协议已经是有第4版了,如果不需要了解NTP太多细节的话,看看这个wiki页面应该就足够了。需要注意的就是它有clock strata的概念。
PTP(Precision Time Protocol)看名字就知道是一个比NTP更精确的时钟同步协议了,PTP的设计目标是使机器之间的时钟偏差在sub-microsecond范围—这是wiki页面上提到的,有其他的地方说的是偏差若干微秒,本文搭建的环境中测量到的偏差也在微秒级别,没有到sub-microsecond级别。在使用PTP协议时,需要了解的主要概念点就是它的master/slave机制。
接下来我们就介绍我搭建NTP和PTP环境的过程,所用到的操作系统是CentOS6.5,内核版本是3.10。其他软件的版本会在用到时提及。

2. 搭建NTP服务


配置环境:两台服务器,一台做NTP服务器,一台做NTP的客户端。同时这两台机器都未联网。
NTP服务器地址:192.168.0.11
NTP客户端地址:192.168.0.22
A. 在192.168.0.11中启动NTP服务:
$ service iptables stop  // 首先把防火墙关了 
$ yum list |grep ntp     // 看下yum源中是否有ntp软件   
$ yum install -y ntp   
$ ntpd --version 
    ntpd 4.2.6p5 
$ vim /etc/ntp.conf      // 修改配置文件 
    restrict 192.168.0.11 mask 255.255.255.0 nomodify nostrap  // 限制作为局域网NTP服务器
    // 下面两句很关键。含义是如果这台NTP服务器的server地址无法访问, 
    // 则将本地时间作为NTP服务时间,这个IP地址也是固定的,不要修改 
    server 127.127.1.0 
    fudge  127.127.1.0 stratum 10  
$ ntpd -p /var/run/ntpd.pid  // 启动ntpd 
$ service ntpd start          // 第二种启动ntpd服务的方法 
// 等待5分钟 
$ ntpstat // 从这条命令应该能看到NTP时钟同步好了,正常的显示结果应该与下面类似 
    synchronised to local net at stratum 11
        time correct to within 11 ms 
        polling server every 64 s


B. 在192.168.0.22上启动NTP服务,选择192.168.0.11为NTP服务器:
$ service iptables stop
$ yum install -y ntp
$ vim /etc/ntp.conf  // 添加下面这个server地址,把其他的都注释掉 
    server 192.168.0.11 
$ service ntpd start
$ netstat  // 等待若干时间应该就能够显示同步成功了

我的这两台实验机器是在同一个Rack的,结果显示差不多同步的偏差在30ms左右。
每个版本的ntpd配置文件可能有少许的差别,不过好在注释都做的不错,所以别的细节就不啰嗦了。

3. 搭建PTP服务


List of PTP implementations可以看到PTP的实现有很多很多种,可以是硬件实现的,可以是软件实现的也可以是软硬件结合实现的。本文中搭建的PTP服务是基于软件PTPd。如果没有特殊的硬件的话,这算是一种非常方便的方法了。
$ service iptables stop // 关掉防火墙 
$ yum list |grep ptp    // 检查yum源 
$ yum install -y ptpd
$ ptpd2 --version 
    ptpd2 version 2.3.0 
// 弄一个管理脚本,从serverfault找来的 :)   
// http://serverfault.com/questions/329127/ptp-time-synchronization-on-centos6-rhel
$ vim ptpd.sh  // 将PTPADRGS 改为 PTPD_EXTRA_OPTIONS 
$ chmod +x ptpd.sh
$ vim /etc/ptpd2.conf  // 修改配置文件 
    ptpengine:preset=masterslave    // 对于master主机,不要选masteronly,具体原因请查看help 
    ptpengine:preset=slaveonly      // 对于slave主机 
    // 其他选项也可以根据需要进行调整,比如log是否开启,是否绑定CPU。这些看配置文件的注释就好了 
$ vim /etc/sysconfig/ptpd2  // 修改启动命令,主要就是指定PTPD的配置文件 
    PTPD_EXTRA_OPTIONS="-c /etc/ptpd2.conf" 
// 现在就可以使用下面三个命令来启动,查看和关闭ptpd服务了 
$ ./ptpd.sh start 
$ ./ptpd.sh status 
$ ./ptpd.sh stop

如果遇到任何问题,首先一定要看看help,使用-H选项的话还能看到非常详细的配置(虽然大多我可看不懂,不过不能不看,理解的越多,遇到的问题就会越少)。
如果log里面的信息看不懂,可以把代码下下来,一个grep搞定。
经测试,在我的机器上使用PTPD软件搭建的服务,时钟偏移的平均值能够达到5us左右。这个粒度基本能满足我们的需求了。

参考链接


----------------------------


ntpd时钟同步服务


CentOS配置时间同步NTP
为什么要使用ntpd而不是ntpdate?
原因很简单,ntpd是步进式的逐渐调整时间,而ntpdate是断点更新,比如现在服务器时间是9.18分,而标准时间是9.28分,ntpd会在一段时间内逐渐的把时间校准到与标准时间相同,而ntpdate会立刻把时间调整到9.28分,如果你往数据库内写入内容或在其他对时间有严格要求的生产环境下,产生的后果会是很严重的。(注:当本地时间与标准时间相差30分钟以上是ntpd会停止工作)
NTP通信协议原理
1.首先主机启动NTP。
2.客户端会向NTP服务器发送调整时间的message。
3.然后NTP server会送出当前的标准时间给client
4.client接受来自server的时间后,会根据这个信息来调整自己的时间。这样就实现了网络对时。
NTP这个deamon采用了UDP 123端口。当我们要利用Tim server来进行实践的同步更新时,就需要使用NTP软件提供的ntpdate来连接端口123
相关的命令和配置文件
/etc/ntp.conf: linux各版本虽然目录不同,但文件名相同。可以用which ntp.conf 或者locate ntp.conf来查找。这是NTP唯一的一个设置文件。
/usr/share/zoneinfo/: 这个里面规定了这个主要时区的时间设置文件。
/etc/sysconfig/clock: 这个文件是linux的主要时区设置文件,每次开机后linux会自动读取这个文件来设置系统所默认的显示时间,可以看看它里面到底设置了什么:
1
2
3
4
5
6
cat /etc/sysconfig/clock
# The ZONE parameter is only evaluated by system-config-date.
# The timezone of the system is defined by the contents of /etc/localtime.
ZONE="Asia/Shanghai"
UTC=true
ARC=false
/etc/localtime: 本地端时间配置文件。
/bin/date: 这个是时间的修改命令,除了输出时间,还可以修改时间。
/sbin/hwclock: 因为linux系统上面BIOS时间与linux系统时间是分开的,所以使用date这个指令调整了时间之后,还需要使用hwclock才能将修改过的时间写入BIOS中。
/usr/sbin/ntpd: 这是NTP的daemon文件,需要启动它才能提供NTP服务,这个命令会读取/etc/ntp.conf里面的设置。
/usr/sbin/ntpdate: 这是client用来连接NTP Server的主要执行文件,如果您不想启用NTP,只想启用NTP Client功能的话,可以只应用此命令。
/usr/sbin/ntptrace: 可以用来追踪某台时间服务器的时间对应关系。
安装与配置
设置时区:cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
安装ntpd服务:yum -y install ntp
配置ntpd
/etc/ntp.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
restrict default kod nomodify notrap nopeer noquery
# restrict -6 default kod nomodify notrap nopeer noquery  #针对ipv6设置
# 允许本地所有操作
restrict 127.0.0.1
#restrict -6 ::1
# 允许的局域网络段或单独ip
restrict 10.0.0.0 mask 255.0.0.0 nomodify motrap
restrict 192.168.0.0 mask 255.255.255.0 nomodify motrap
restrict 192.168.1.123 mask 255.255.255.255 nomodify motrap
# 使用上层的internet ntp服务器
restrict cn.pool.ntp.org
restrict 1.cn.poo.ntp.org
restrict 0.asia.pool.ntp.org
restrict 3.asia.pool.ntp.org
server cn.pool.ntp.org prefer
server 1.cn.poo.ntp.org
server 0.asia.pool.ntp.org
server 3.asia.pool.ntp.org
# 如果无法与上层ntp server通信以本地时间为标准时间
server   127.127.1.0    # local clock
fudge    127.127.1.0 stratum 10
# 计算本ntp server 与上层ntpserver的频率误差
driftfile /var/lib/ntp/drift
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
#日志文件
logfile /var/log/ntp.log
如果只是想在没有外网连接的内网里面做时钟server,那么在 ntp.conf中加入下面配置:
1
2
3
4
5
6
tos orphan
server 127.127.0.1
fudge 127.127.0.1 stratum 1
# 把其它所有外网的 server 配置全部删除掉。
修改/etc/sysconfig/ntpd:
1
2
3
4
5
6
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid"
# Set to 'yes' to sync hw clock after successful ntpdate
SYNC_HWCLOCK=yes #make no into yes; BIOS的时间也会跟着修改
# Additional options for ntpdate
NTPDATE_OPTIONS=""
验证与状态检查
运行ntp
1
2
3
4
5
6
7
8
9
10
11
12
service ntpd start/stop/restart
# 查看ntp的端口,应该看到123端口
netstat -unlnp
# 查看ntp服务器有无和上层连通
ntpstat
synchronised to NTP server () at stratum 2
time correct to within 74 ms
polling server every 128 s
# 注意:此命令出现上述synchronised结果比较慢,我的用了大概5分钟。
查看ntp服务器与上层间的联系:ntptrace
1
2
3
4
ntptrace -n 127.0.0.1
127.0.0.1: stratum 3, offset -0.001095, synch distance 0.532610
116.193.83.174: timed out, nothing received
查看ntp服务器与上层ntp服务器的状态:ntpq
1
2
3
4
5
6
7
8
9
10
11
12
ntpq -p
# 其中:
# remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
# refid  - 参考上一层ntp主机地址
# st     - stratum阶层
# when   - 多少秒前曾经同步过时间
# poll   - 下次更新在多少秒后
# reach  - 已经向上层ntp服务器要求更新的次数
# delay  - 网络延迟
# offset - 时间补偿
# jitter - 系统时间与bios时间差
如果所有远程服务器的jitter值是4000并且delay和reach的值是0,那么说明时间同步是有问题的。
可能的原因是防火墙阻断了与server之间的通讯,即123端口是否正常开放;
此外每次重启NTP服务器之后大约要3-5分钟客户端才能与server建立正常的通讯连接,否则你在客户端执行“ntpdate 服务器ip”的时候将返回:
27 Jun 10:20:17 ntpdate[21920]: no server suitable for synchronization found
启动NTPD
我采用了一个很笨的办法来手动启动ntpd,而不是把ntpd加入服务,写一个简单的脚本
1
2
3
4
vi ntpstart.sh
ntpdate cn.pool.ntp.org
ntpdate cn.pool.ntp.org
service ntpd start
然后加入/etc/rc.local:
1
/shpath/ntpstart.sh
这是因为我有一台服务器启动后的时间总是与标准时间差别很大,每次启动后ntpd要花很多时间才能把时间校准,所以我是先在系统启动后ntpdate更新两次,然后再启动ntpd服务,在freebsd里好像有修改配置文件,让时间服务器在系统启动之前启动的,centos还没仔细琢磨。
客户端配置
方法1.使用ntpdate与上面配置的时间服务器定时同步,不推荐此方法
方法2.安装ntpd,指定时间server为上面配置的服务器地址,推荐
更详细的介绍参见台湾 鸟哥的Linux私房菜: http://linux.vbird.org/linux_server/0440ntp.php
附录
1
2
3
4
5
6
7
8
9
10
11
中国国家授时中心(陕西西安) 210.72.145.44
上海: 61.129.66.79 (t2.hshh.org) 61.129.42.44 (ntp.fudan.edu.cn) 202.120.2.101 (ntp.sjtu.edu.cn)
浙江 218.75.4.130 (t1.hshh.org)
内蒙古 218.21.130.42 (t1.hshh.org)
香港: 137.189.11.66 (clock.cuhk.edu.hk ) 137.189.11.128 (ntp.cuhk.edu.hk )
台湾: 220.130.158.52(time.stdtime.gov.tw) 220.130.158.72(Clock.stdtime.gov.tw)
220.130.158.51(tick.stdtime.gov.tw) 220.130.158.54(watch.stdtime.gov.tw)
asia.pool.ntp.org, 更多亚洲服务器请参考 http://www.pool.ntp.org/zone/asia
cn.pool.ntp.org, 更多中国服务器请参考 http://www.pool.ntp.org/zone/cn
tw.pool.ntp.org, 更多中国台湾服务器请参考 http://www.pool.ntp.org/zone/tw
hk.pool.ntp.org, 更多中国香港服务器请参考 http://www.pool.ntp.org/zone/hk
取消ntpd自动启动,在系统启动时,指定ntpdate远程标准时间服务器两次,然后service ntpd start
好像也可以修改rc.conf或者加上一堆什么参数来实现,我偷懒用这个笨办法来保证时间的准确了
解决ntp的错误
解决ntp的错误 no server suitable for synchronization found
当用ntpdate -d 来查询时会发现导致 no server suitable for synchronization found 的错误的信息有以下2个:
错误1.Server dropped: Strata too high
在ntp客户端运行ntpdate serverIP,出现no server suitable for synchronization found的错误。
在ntp客户端用ntpdate –d serverIP查看,发现有Server dropped: strata too high的错误,并且显示stratum 16。而正常情况下stratum这个值得范围是0~15。
这是因为NTP server还没有和其自身或者它的server同步上。
以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。
server 127.127.1.0
fudge 127.127.1.0 stratum 8
在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。
那么如何知道何时ntp server完成了和自身同步的过程呢? 在ntp server上使用命令:
watch ntpq -p
出现画面:
Every 2.0s: ntpq -p Thu Jul 10 02:28:32 2008
remote refid st t when poll reach delay offset jitter
==============================================================================
192.168.30.22 LOCAL(0) 8 u 22 64 1 2.113 179133. 0.001
LOCAL(0) LOCAL(0) 10 l 21 64 1 0.000 0.000 0.001
注意LOCAL的这个就是与自身同步的ntp server。
注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。
如果之后从ntp客户端同步ntp server还失败的话,用ntpdate –d来查询详细错误信息,再做判断。
错误2.Server dropped: no data
从客户端执行netdate –d时有错误信息如下:
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
192.168.30.22: Server dropped: no data
server 192.168.30.22, port 123
…..
28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found
出现这个问题的原因可能有两个:
1.检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误。使用以下命令检查ntp的版本:ntpq -c version。下面是来自ntp官方网站的说明:
The behavior of notrust changed between versions 4.1 and 4.2.
In 4.1 (and earlier) notrust meant “Don’t trust this host/subnet for time”.
In 4.2 (and later) notrust means “Ignore all NTP packets that are not cryptographically authenticated.” This forces remote time servers to authenticate themselves to your (client) ntpd
解决:把notrust去掉。
2.检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。 可以用命令service iptables stop来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。
NTP工作原理
NTP的基本工作原理如 图 所示。Device A和Device B通过网络相连,它们都有自己独立的系统时钟,需要通过NTP实现各自系统时钟的自动同步。为便于理解,作如下假设:
在Device A和Device B的系统时钟同步之前,Device A的时钟设定为10:00:00am,Device B的时钟设定为11:00:00am。
Device B作为NTP时间服务器,即Device A将使自己的时钟与Device B的时钟同步。
NTP报文在Device A和Device B之间单向传输所需要的时间为1秒。


系统时钟同步的工作过程如下:
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的报文格式
NTP有两种不同类型的报文,一种是时钟同步报文,另一种是控制报文。控制报文仅用于需要网络管理的场合,它对于时钟同步功能来说并不是必需的,这里不做介绍。

No comments:

Post a Comment