25

网络时间协议介绍以及服务器同步网络时间

 3 years ago
source link: https://segmentfault.com/a/1190000038170941
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

NTP介绍

网络时间协议(英语:Network Time Protocol,缩写:NTP)是在数据网络潜伏时间可变的计算机系统之间通过分组交换进行时钟同步的一个网络协议,位于OSI模型的应用层。自1985年以来,NTP是目前仍在使用的最古老的互联网协议之一。NTP由特拉华大学的David L. Mills设计。

NTP意图将所有参与计算机的协调世界时(UTC)时间同步到几毫秒的误差内。它使用Marzullo算法的修改版来选择准确的时间服务器,其设计旨在减轻可变网络延迟造成的影响。NTP通常可以在公共互联网保持几十毫秒的误差,并且在理想的局域网环境中可以实现超过1毫秒的精度。不对称路由和拥塞控制可能导致100毫秒(或更高)的错误。

该协议通常描述为一种主从式架构,但它也可以用在点对点网络中,对等体双方可将另一端认定为潜在的时间源。发送和接收时间戳采用用户数据报协议(UDP)的端口123实现。这也可以使用广播或多播,其中的客户端在最初的往返校准交换后被动地监听时间更新。NTP提供一个即将到来闰秒调整的警告,但不会传输有关本地时区或夏时制的信息。

当前协议为版本4(NTPv4),这是一个RFC 5905文档中的建议标准。它向下兼容指定于RFC 1305的版本3。

ntp组件是Linux平台ntp协议的软件实现,部分Linux操作系统默认安装了ntp组件,如果没有先安装ntp,才能使用该协议完成时间同步。

服务器可访问互联网时的时间同步方法

已配置DNS

服务器能访问外网并且配置了DNS,直接

ntpdate 0.cn.pool.ntp.org 或 nptdate ntp1.aliyun.com

就可以同步外网时间。

未配置DNS

部分服务器出于安全考虑,虽然通外网,但没有配置DNS,这时需要直接从时间服务器的ip地址同步时间。可以使用阿里云(ntp1.aliyun.com)的时间服务器ip直接同步,

ntpdate 120.24.81.91  或者 清华的时间服务器ntpdate 84.16.73.33

服务器不能访问互联网的时间同步方法

假设局域网有一台服务器(ip地址是IP_TIME)时间是正确的,将这台服务器配置为局域网ntp时间服务器,其它服务器执行

ntpdate IP_TIME

这样,至少所有服务器就和这台的时间同步了,这台是否需要和网络时间同步要看功能要求。确实需要的话,这台必须可以访问互联网,按上面描述同步到网络时间,其余可保持不能访问互联网的状态。

举个例子

假设A B两台Linux服务器,要将两台都同步到网络时间。

A是开通了外网的,虽然没配置DNS服务器,但能ping通120.24.81.91(阿里云时间服务器)。可以直接同步时间。

B没开通外网,ping 120.24.81.91会报connect: Network is unreachable,这种肯定无法从外网服务器同步时间。执行ntpdate 120.24.81.91,会报no server suitable for synchronization found,或者no servers can be used, exiting。

U7rIFnn.png!mobile 解决办法统共分三步,A和B的防火墙都关闭,整个过程用root账号操作。

  1. A从外网同步时间,
  2. 把A配置成ntp服务器(NTP server),
  3. B从A同步时间。

A同步网络时间

服务器第一次同步时间显示如下

[root@localhost ~]# ntpdate 120.24.81.91
10 Aug 09:46:07 ntpdate[15071]: step time server 120.24.81.91 offset 1.423469 sec

第2-n次显示如下

[root@localhost ~]# ntpdate 120.24.81.91
10 Aug 14:16:14 ntpdate[12150]: adjust time server 120.24.81.91 offset -0.030012 sec

每执行一次,offset后面的误差都会变化,趋势是越来越准确。

把A配置为NTP server

配置A为NTP server,首先修改配置文件/etc/ntp.conf。怎么修改往下看。

修改完成执行如下cat指令,除了192.168.2.0改成你自己局域网的网段,其余要求必须和下面一样,有比下面多出的语句就注释掉,没有的语句就加上。如果恰好和下面一样,192位置也是你的局域网网段,那就什么都不用改。

#cat /etc/ntp.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}'  

restrict default ignore                                //#设置默认策略为允许任何主机进行时间同步
restrict 127.0.0.1                                         //给于本机所有权限
restrict 192.168.2.0 mask 255.255.255.0 nomodify notrap     //给于局域网机的机器有同步时间的权限
server 0.127.127.1.0               //设置时间服务器为本机,可以设为120.24.81.91外网服务器
server  127.127.1.0  # local clock
fudge  127.127.1.0 stratum 10
driftfile /var/lib/ntp/drift
broadcastdelay  0.008
keys    /etc/ntp/keys

然后执行

# /etc/init.d/ntpd start 或 #service ntpd start

会显示OK,表示成功,本机已经是NTP server了。

如果配置文件反复修改,执行

# /etc/init.d/ntpd restart

重新加载配置文件。

B和A同步时间

A启动NTP server服务后,要等待5分钟,才能在B上执行同步时间指令,这个时间是给NTP server同步本机时间用的。

5分钟后在B上执行

[root@DB1 ~]# ntpdate AIP (AIP是A的内网IP地址)
10 Aug 13:35:59 ntpdate[10737]: adjust time server AIP offset 0.004937 sec

,就可以同步A的系统时间到B,也相当于同步了外网时间。

如果A启动NTP server后,不等5分钟,马上在B上同步时间会怎么样?

由于NTP server本身时间还没和操作系统同步好,会返回错误

no server suitable for synchronization found

可以使用ntpdate –d AIP指令查看。

[root@DB1 ~]# ntpdate -d AIP
10 Aug 13:28:07 ntpdate[10719]: ntpdate [email protected] Thu Oct  5 04:11:32 EDT 2006 (1)
Looking for host 192.168.2.10 and service ntp
host found : 192.168.2.10
transmit(192.168.2.10)
receive(192.168.2.10)
省略
192.168.2.10: Server dropped: strata too high
server 192.168.2.10, port 123
stratum 16, precision -20, leap 11, trust 000
refid [192.168.2.10], delay 0.02573, dispersion 0.00000
省略

有“Server dropped: strata too high”的提示,并且“stratum 16”。

stratum的正常范围是“0~15”。

什么都不用做,等一段时间再执行指令试试,就会变成stratum 11, precision -20, leap 00, trust 000。stratum 11是正常范围,此时执行ntpdate AIP就成功同步时间了。

B服务器要始终和A保持一致怎么办?

B需要频繁校正时间的话,crontab配置ntpdate指令可以达到目的。

crontab -e  
9 7 * * * /usr/sbin/ntpdate  AIP

操作系统时间同步到硬件时间也都可以根据需要设定crontab。硬件时间保持和操作系统时间一致的好处是可以避免重启服务器后硬件时间覆盖系统时间导致误差。

A可以一边从外网同步时间,一边作为局域网的NTP server吗?

经测试,ntpd服务运行时,无法执行ntpdate指令。要频繁同步网络时间,还要作为局域网的NTP server给其它服务器同步时间,A上的crontab定时任务要按照如下顺序执行

1. ntpdate 外网时间服务器ip
2. service ntpd start
3. 间隔一段可接受的时间,间隔约长,和网络时间的误差越大。
   此时B可同步到A的时间
4. service ntpd stop
下面循环回第一步
1. ntpdate 外网时间服务器ip

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK