ntp服务器设置(时间同步协议)
2010-01-11 11:28:39 阿炯

The Network Time Protocol (NTP) is widely used to synchronize a computer to Internet time servers or other sources, such as a radio or satellite receiver or telephone modem service. It can also be used as a server for dependent clients. It provides accuracies typically less than a millisecond on LANs and up to a few milliseconds on WANs. Typical NTP configurations utilize multiple redundant servers and diverse network paths in order to achieve high accuracy and reliability. Authentication is provided using symmetric key cryptography and the MD5 message digest algorithm included in the distribution. If the OpenSSL cryptographic library is installed, the SHA or SHA1 message digest algorithms can be used. If the OpenSSL library is installed, additional options based on public key cryptography are available.

NTP time synchronization services are widely available in the public Internet. The public NTP subnet in early 2008 includes several thousand servers in most countries and on every continent of the globe, including Antarctica. These servers support a total population estimated at over 25 million computers in the global Internet. The NTP subnet operates with a hierarchy of levels, where each level is assigned a number called the stratum. Stratum 1 (primary) servers at the lowest level are directly synchronized to national time services. Stratum 2 (secondary) servers at the next higher level are synchronize to stratum 1 servers and so on. Normally, NTP clients and servers with a relatively small number of clients do not synchronize to public primary servers. There are several hundred public secondary servers operating at higher strata and are the preferred choice.

网络时间协议(Network Time Protocol,缩写:NTP)是在数据网络潜伏时间可变的计算机系统之间通过分组交换进行时钟同步的一个网络协议,位于 OSI 模型的应用层。它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可使用加密确认的方式来防止恶毒的协议攻击。

亚洲中国的时间同步源
http://www.pool.ntp.org/zone/cn

NTP如何工作

NTP提供准确时间,首先要有准确的时间来源,这一时间应该是国际标准时间UTC。NTP获得UTC的时间来源可以是原子钟、天文台、卫星,也可以从Internet上获取。这样就有了准确而可靠的时间源。时间按NTP服务器的等级传播。按照离外部UTC源的远近将所有服务器归入不同的Stratum(层)中。Stratum-1在顶层,有外部UTC接入,而Stratum-2则从Stratum-1获取时间,Stratum-3从Stratum-2获取时间,以此类推,但Stratum层的总数限制在15以内。所有这些服务器在逻辑上形成阶梯式的架构相互连接,而Stratum-1的时间服务器是整个系统的基础。

计算机主机一般同多个时间服务器连接,利用统计学的算法过滤来自不同服务器的时间,以选择最佳的路径和来源来校正主机时间。即使主机在长时间无法与某一时间服务器相联系的情况下,NTP服务依然有效运转。为防止对时间服务器的恶意破坏,NTP使用了识别(Authentication)机制,检查来对时的信息是否是真正来自所宣称的服务器并检查资料的返回路径,以提供对抗干扰的保护机制。

NTP的发展
网络时间协议(NTP)的首次实现记载在InternetE ngineeringNote之中,其精确度为数百毫秒。稍后出现了首个时间协议的规范,即RFC-778,它被命名为DCNET互联网时间服务,而它提供这种服务还是借助于 Internet control MessageProtocol(ICMP),即互联网控制消息协议中的时间戳和时间戳应答消息。作为NTP名称的首次出现是在RFC-958之中,该版本也被称为NTPv0,其目的是为ARPA网提供时间同步。它己完全脱离ICMP,是作为独立的协议以完成更高要求的时间同步。它对于如本地时钟的误差估算和精密度等基本运算、参考时钟的特性、网络上的分组数据包及其消息格式进行了描述。但是不对任何频率误差进行补偿,也没有规定滤波和同步的算法。

美国特拉华大学(University of Delaware)的David L .Mills主持了由美国国防部高级研究计划局DARPA、美国国家科学基金NSF和美国海军水面武器中心NSWC资助的网络时间同步项目,成功的开发出了NTP协议的第1, 2, 3版。

NTP version 1 出现于1988年6月,在RFC-1059中描述了首个完整的NTP的规范和相关算法。这个版本已经采用了client/server模式以及对称操作,但是它不支持授权鉴别和NTP的控制消息。

1989年9月推出了取代RFC-958和RFC-1059的NTP v2版本即RFC-1119。

几乎同时,DEC公司也推出了一个时间同步协议,数字时间同步服务DTSS(Digital Time Synchronization Service).在 1992年3月,NTP v3版本RFC-1305问世,该版本总结和综合了NTP先前版本和DTSS,正式引入了校正原则,并改进了时钟选择和时钟滤波的算法,而且还引入了时间消息发送的广播模式,这个版本取代了NTP的先前版本。

NTPv3 发布后,一直在不断地进行改进,NTP实现的一个重要功能是对计算机操作系统的时钟调整。在NTPv3研究和推出的同时,有关在操作系统核心中改进时间保持功能的研究也在并行地进行。1994年推出了RFC-1589,名为A KernelModel for Precision Time keening,即精密时01保持的核心模式,这个实现可以把计算机操作系统的时间精确度保持在微秒数量级。几乎同时,改进建议。对本地时钟调整算法,通信模式,新的时钟驱动器,又提出了NTPv4。

适配规则等方面的改进描述了具体方向。现在NTP的第4版正在研究和测试中,网络时间同步技术也将向更高精度、更强的兼容性和多平台的适应性方向发展。网络时间协议NTP是用于互联网中时间同步的标准之一,它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.lms,在Internet上绝大多数的地方其精度可以达到1-50ms。

NTP version4 是一个很重要的NTP协议,可是还没有一个正式的RFC文本协议的描述。值得提一下的是,简单的NTP(SNTP)version4已经在RFC2030描述了。

NTP 协议设计者 David L. Mills 已于 2024 年 1 月 17 日去世。他曾参与设计早期 NSFNET 的 Fuzzball 路由,担任 INARG 工作组负责人。


自 1985 年以来,NTP 是目前仍在使用的最古老的互联网协议之一。尽管 NTP 是 David L. Mills 最为著名的工作之一,但他的贡献不仅限于此。David L. Mills 发明了基于 DEC LSI-11 的 Fuzzball 路由,用于 56 kbit/s NSFNET,这项技术启发了 ping for BSD 的作者,并实现了第一个 FTP。他还撰写了大量 RFC。

-----------------------------------------------------
服务配置:
相关配置参数说明
#restrict权限控制语法为:
#restrict IP mask netmask_IP parameter
#其中 IP 可以是软件地址,也可以是 default ,default 就类似 0.0.0.0 咯!
#至于 paramter 则有:
#ignore :关闭所有的 NTP 联机服务
#nomodify:表示 Client 端不能更改 Server 端的时间参数,不过,
#Client 端仍然可以透过 Server 端来进行网络校时。
#notrust :该 Client 除非通过认证,否则该 Client 来源将被视为不信任网域
#noquery :不提供 Client 端的时间查询
#如果 paramter 完全没有设定,那就表示该 IP (或网域) 『没有任何限制!』

#设定上层主机主要以 server这个参数来设定,语法为:
#server [IP|FQDN] [prefer]
#Server 后面接的就是我们上层 Time Server 啰!而如果 Server 参数
#后面加上 perfer 的话,那表示我们的 NTP 主机主要以该部主机来作为
#时间校正的对应。另外,为了解决更新时间封包的传送延迟动作


下述设置以时钟源服务器IP地址为192.168.0.12为例。

可以命令ntpstat和ntpq -p 查看本机NTPD服务状态。配置完成后,均需要重新启动NTPD服务 service ntpd restart

在linux下,我们可以通过自带的NTP(Network Time Protocol)协议通过网络使自己的系统保持精确的时间。可用的公共时间服务器列表可以从下面的地址获取:
http://ntp.isc.org/bin/view/Servers/NTPPoolServers

NTP是用来使系统和一个精确的时间源保持时间同步的协议。建议大家在自己管理的网络中建立至少一台时间服务器来同步本地时间,这样可以使得在不同的系统上处理和收集日志和管理更加容易,介绍一下环境:12为本地时间服务器,其他服务器和12同步。12和网上时间服务器同步。

一、配置12时间服务器
1、首先查询NTP软件版本
rpm -qa|grep ntp
ntp-4.2.2p1-9.el5.centos
如果没有可以从linux安装盘上查找,安装此ntp包

2、编辑配置文件
vim /etc/ntp.conf

首先定义服务器
server 0.centos.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org
restrict default nomodify notrap noquery
restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap #从192.168.0.1-192.168.0.254的服务器都可以使用我们的NTP服务器来同步时间。如果你的服务器所用的版本在4.2之后的话,请将'notrust'选项去掉。

注释掉以下一行
#restrict default ignore

3、启动NTP服务器
#chkconfig ntpd on
#/etc/init.d/ntpd start
#/etc/init.d/ntpd stop
#/etc/init.d/ntpd restart

默认情况下,我们配置的NTP服务器不会去时间源那里同步时间,所以必须修改/etc/ntp/step-tickers文件,加入我们的时间源,这样每次通过/etc/init.d/ntpd 来启动服务的时候就会自动更新时间了,检查服务器同步状态:

ntpq -p
ntptrace 192.168.0.12

如果输出正确,则说明时间服务器成功。每次启动服务器,会自动同步时间。

配置LINUX客户端
在linux客户端上执行ntpdate ntp_server_ip 就可以根据时间服务器统一局域网的时间了,将上面的命令放到cron里每天早上3点定期执行,crontab –e 然后输入
0 3 * * * /usr/sbin/ntpdate 192.168.0.12

-----------------------------------------------------
排错:
错误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
remote           refid     st t when poll reach   delay   offset  jitter
==============================================================================
61.129.42.44    24.88.26.231     3 u   46   64  377    5.437  1096572 442044.
Hshh.org        66.220.9.122     2 u   52   64  377    5.483  727058. 301203.
122.226.192.4   192.43.244.18    2 u   52   64  377    5.239  606225. 353469.
*LOCAL(0)        .LOCL.          10 l   48   64  377    0.000    0.000   0.002

注意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时有错误信息如下:
root@ubuserv:~# ntpdate -d ntp.freeoa.net
4 Apr 23:55:44 ntpdate[4138]: ntpdate 4.2.4p4@1.1520-o Wed May 13 21:05:58 UTC 2009 (1)
transmit(192.168.0.12)
receive(192.168.0.12)
transmit(192.168.0.12)
receive(192.168.0.12)
transmit(192.168.0.12)
receive(192.168.0.12)
transmit(192.168.0.12)
receive(192.168.0.12)
transmit(192.168.0.12)
192.168.0.12: Server dropped: strata too high
server 192.168.0.12, port 123
stratum 16, precision -19, leap 11, trust 000
refid [192.168.0.12], delay 0.02582, dispersion 0.00026
transmitted 4, in filter 4
reference time:    00000000.00000000  Thu, Feb  7 2036 14:28:16.000
originate timestamp: cf62c29a.698153be  Sun, Apr  4 2010 15:47:38.412
transmit timestamp:  cf633500.c066d373  Sun, Apr  4 2010 23:55:44.751
filter delay:  0.02600  0.02583  0.02592  0.02582
0.00000  0.00000  0.00000  0.00000
filter offset: -29286.3 -29286.3 -29286.3 -29286.3
0.000000 0.000000 0.000000 0.000000
delay 0.02582, dispersion 0.00026
offset -29286.339566

4 Apr 23:55:44 ntpdate[4138]: no server suitable for synchronization found

出现这个问题的原因可能有2:

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的设置。

修改vyatta软路由做的Ntp服务器

这个作为Ntp服务器的功能默认好像是开启的,默认是vyatta官方的(不过已经被收购了)
vyatta@freeoa-router# show system ntp server
 server 0.vyatta.pool.ntp.org {
 }
 server 1.vyatta.pool.ntp.org {
 }
 server 2.vyatta.pool.ntp.org {
 }

这些ntp server并不能很好的工作,我们需要修改就近的。

vyatta@freeoa-router# set system ntp server 0.asia.pool.ntp.org
[edit]
vyatta@freeoa-router# set system ntp server 2.cn.pool.ntp.org
[edit]
vyatta@freeoa-router# set system ntp server 3.asia.pool.ntp.org
[edit]
vyatta@freeoa-router# commit
[ system ntp ]
Stopping NTP server: ntpd.
Starting NTP server: ntpd.

[edit]
vyatta@freeoa-router# save
Saving configuration to '/config/config.boot'...
Done

保存后在其它机器上使用它来做为ntp服务器来使用时,发现会还是不能成功同步到时间。

[root@freeoa-app ~]# /usr/sbin/ntpdate 192.168.20.251
21 Sep 15:19:18 ntpdate[7563]: no server suitable for synchronization found
[root@freeoa-app ~]# /usr/sbin/ntpdate 192.168.20.251
21 Sep 15:25:20 ntpdate[8015]: no server suitable for synchronization found

原因如下:本地的ntp中继服务器会花一定的时间去测试这些给定的ntp server,可以通过指令看到这个过程:

Every 2.0s: ntpq -p
Wed Sep 21 15:20:13 2016

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*chobi.paina.jp  131.113.192.40   2 u   37   64    1   57.231   -0.414   3.371
 194.225.150.25  .STEP.          16 u    -   64    0    0.000    0.000   0.000
 202.118.1.130   .STEP.          16 u    -   64    0    0.000    0.000   0.000
 
 
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*chobi.paina.jp  131.113.192.40   2 u   15   64    1   57.456   -0.249   3.033
+194.225.150.25  194.190.168.1    2 u   42   64    1  456.902  -92.977  32.218
 202.118.1.130   .STEP.          16 u    -   64    0    0.000    0.000   0.000
 

大致在一会儿(有说是5分钟),在同步时,就正常了。
[root@freeoa-app ~]# /usr/sbin/ntpdate 192.168.20.251
21 Sep 16:21:43 ntpdate[12506]: adjust time server 192.168.20.251 offset 0.033840 sec


该文章最后由 阿炯 于 2024-01-20 10:20:18 更新,目前是第 3 版。