注意:如果NTP Client和NTP Server时间偏差太大,可能导致Client的ntpd进程退出。
平滑同步
使用ntpd进行时钟同步,可以保证一个时间不经历两次,它每次同步时间的偏移量不会太陡,是慢慢来的,这正因为这样,ntpd平滑同步可能耗费的时间比较长。
刚开始可能时钟不同步,多等待时间后慢慢就同步了。
SYNC_HWCLOCK=yes
OPTIONS=”-g -x”
快速同步/平滑同步的设置
Linux中ntpd的-x选项的说明
offset值 | 0~128ms | 128ms~600s | 600s~1000s | 1000s以上 |
有-x参数 | 微调 | 微调(0.5ms/s,600s需14天) | 跳跃 | 退出(加-g参数可忽略) |
无-x参数 | 微调 | 跳跃 | 跳跃 | 退出(加-g参数可忽略) |
Linux ntpd的访问间隔与调整时间没有联系,标准ntp服务不是step模式调整的,每次访问后经过计算滤波,最后得出offset偏差后,才进行策略的调整。
标准的时钟同步服务器
NTP相关常用命令:
systemctl restart ntpd
直接进行时间同步变更
ntpdate命令用来设置调整本地日期和时间。它从指定的每个服务器获得了一些样本,并应用标准NTP时钟过滤器和选择算法来选择最好的样本。
* 如果它确定时钟偏差超过0.5秒,它通过调用settimeofday子例程设置时钟时间。在引导时间,这是一个首选的方法。
* 如果它确定时钟偏差小于0.5秒,它通过调用adjtime子例程和偏移量来调整时钟时间。此方法倾向于用牺牲一些稳定性来保持漂移时钟更加准确。当不是通过运行一个守护程序而是从cron命令有规则的运行ntpdate命令时,每一小时或两小时执行一次可以保证足够的走时精度,从而避免调整时钟。
ntpdate [-46bBdqsuv] [-a key#] [-e delay] [-k file] [-p samples] [-o version#] [-t timeo] [-U username] server …
-a keyid: 使用keyid来认证全部数据包。
-b: 通过调用settimeofday子例程来增加时钟的时间。
-d: 指定调试方式。判断ntpdate命令会产生什么结果(不产生实际的结果)。结果再现在屏幕上。这个标志使用无特权的端口。
-e delay: 指定延迟认证处理的时间秒数。
-k keyfile: 当不使用缺省值/etc/ntp.keys文件时,为包含密钥的文件指定一个不同的名称。
-o version: 当轮询它的发出数据包时,指定使用的NTP版本实现。 Version的值可以是1,2,3。缺省值是3。
-p samples: 指定从每个服务器获取的样本的数目。 Samples的值:1~8,它的缺省值是4。
-s: 指定日志操作syslog设施的使用,而不是使用标准输出。 当运行ntpdate命令和cron命令时,它是很有用的。
-t timeout: 指定等待响应的时间。给定timeout的值四舍五入为0.2秒的倍数。缺省值是1秒。
-u: 指定使用无特权的端口发送数据包。 当在一个对特权端口的输入流量进行阻拦的防火墙后是很有益的, 并希望在防火墙之外和主机同步。防火墙是一个系统或者计算机,它控制从外网对专用网的访问。
查看网络中的NTP服务器
#
ntpq -p
[root@abc ~]# ntpq -p
==============================================================================
ntpsrv01 .XFAC. 16 u – 1024 0 0.000 0.000 0.000
*LOCAL(0) .LOCL. 10 l 46 64 377 0.000 0.000 0.000
[root@abc ~]#
配置了LOCAL作为Server。这个说明当前使用了本地时钟作为服务端同步。这样时钟可能和ntpsrv01的不一致。
[root@abc ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*ntpsrv01 101.201.72.121 4 u 32 64 17 0.161 0.119 0.093
[root@abc ~]#
未配置LOCAL作为Server。这个说明使用ntpsrv01作为服务端同步。时钟和ntpsrv01的保持一致。
*表示目前使用的NTP Server,这里选择的本机;
st:即stratum阶层,值越小表示ntp serve的精准度越高;
when:单位秒,几秒前曾做过时间同步更新的操作;
poll表示,每隔多少毫秒与ntp server同步一次;
reach:已经向上层NTP服务器要求更新的次数;
delay:网络传输过程钟延迟的时间;
offset:时间补偿的结果;
jitter:Linux系统时间与BIOS硬件时间的差异时间
注意:
1)NTP Server端重启后,Client端需要等5分钟再与其进行时间同步,否则会提示“no server suitable for synchronization found”错误。等待的时间可以通过命令 watch ntpq -p来监控。
2)注意reach这个值,在启动NTP Server服务后,这个值就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。如果之后从NTP Client同步NTP Server还失败的话,用ntpdate –d来查询详细错误信息,再做判断。
检查ntp的版本(ntpq -c version),如果ntp版本>=4.2,在restrict的定义中使用了notrust的话,会导致以上错误。需要删除notrust。
2)检查NTP Server的防火墙,是否屏蔽了UDP 123端口。
ntpstat
[root@abc ~]# ntpstat
synchronised to NTP server (192.168.111.254) at stratum 5
time correct to within 54 ms
polling server every 64 s
[root@abc ~]#
ntpdate –d serverIP
局域网内的NTP同步配置
#
rpm -q ntp
#
yum -y install ntp
#
systemctl enable ntpd
#
systemctl start ntpd
ntpdate -u cn.pool.ntp.org
# 允许内网其他机器同步时间,如果不添加该约束默认允许所有IP访问本机同步服务
restrict 192.168.111.0 mask 255.255.255.0 nomodify notrap
# 配置和上游标准时间同步
#server 101.201.72.121
# 中国国家授时中心
#server 133.100.11.8 #日本[福冈大学]
server ntp.ntsc.ac.cn #中科院
server time2.aliyun.com #阿里云NTP
server cn.ntp.org.cn #国家授时中心
server 3.cn.pool.ntp.org
server 1.asia.pool.ntp.org
server 3.asia.pool.ntp.org
# 配置允许上游时间服务器主动修改本机(内网ntp Server)的时间
#restrict
101.201.72.121
nomodify notrap noquery
#restrict 133.100.11.8 nomodify notrap noquery
restrict ntp.ntsc.ac.cn nomodify notrap noquery
restrict time2.aliyun.com nomodify notrap noquery
restrict cn.ntp.org.cn nomodify notrap noquery
restrict 3.cn.pool.ntp.org nomodify notrap noquery
restrict 1.asia.pool.ntp.org nomodify notrap noquery
restrict 3.asia.pool.ntp.org nomodify notrap noquery
# 确保localhost有足够权限,使用没有任何限制关键词的语法。
# 外部时间服务器不可用时,以本地时间作为时间服务。
# 注意:这里不能改,必须使用127.127.1.0,否则会导致无法
#在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客户端。
#下面这个配置,建议NTP Client关闭,建议NTP Server打开。因为Client如果打开,可能导致NTP自动选择合适的最近的NTP Server、也就有可能选择了LOCAL作为Server进行同步,而不与远程Server进行同步。
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
systemctl restart ntpd
#
ntpq -p
ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
+114.118.7.161 123.139.33.3 2 u 18 64 1 28.685 0.224 0.000
+203.107.6.88 10.137.38.86 2 u 17 64 1 17.570 -0.138 0.000
*120.25.115.20 10.137.53.7 2 u 16 64 1 30.275 -0.677 0.000
ntp.hkg10.hk.le 130.133.1.10 2 u 15 64 1 57.664 33.023 0.000
send.mx.cdnetwo 216.239.35.8 2 u 14 64 1 78.696 -1.135 0.000
LOCAL(0) .LOCL. 10 l – 64 0 0.000 0.000 0.000
#
ntpstat
#
ntpstat
#
ntpstat
time correct to within 7955 ms
polling server every 64 s
#配置上游时间服务器为本地的ntpd Server服务器
server 192.168.111.254
# 配置允许上游时间服务器主动修改本机的时间
restrict 192.168.111.254 nomodify notrap noquery
#下面这个配置,建议NTP Client关闭,建议NTP Server打开。因为Client如果打开,可能导致NTP自动选择合适的最近的NTP Server、也就有可能选择了LOCAL作为Server进行同步,而不与远程Server进行同步。
#server 127.127.1.0 # local clock
#fudge 127.127.1.0 stratum 10
#
ntpdate -u 192.168.111.254
#
systemctl restart ntpd
#
ntpq -p