NTP时钟同步配置

Centos7 NTP时钟同步配置
01、计算机时钟偏差分析

通用PC机自带两类时钟源:硬件时钟和软件时钟(或称为系统时钟)。不论是硬件时钟还是软件时钟,都是由石英晶体振荡器驱动的,通过累计石英晶体振荡器输出脉冲数,换算出时间。所以计算机时钟的准确度取决于晶振频率准确度。受温度变化、电压、芯片老化等因素影响,晶振频率会发生小幅度波动,其中温度对晶振频影响最大。

由于工艺和材料的原因,同一生产线上标称频率相同的石英晶体,其实际频率是不同的,实际频率与标称频率偏差率从10-4量级到10-9量级不等。以10-4量级为例,时钟每天至少误差8.64 s。

02、基于NTP减少计算机时钟频率偏差

时钟频率偏差是时钟长期计时累积误差的主要原因,要提高时钟长期计时精度,必须补偿时钟频率偏差。联网的计算机可采用NTP方式,可非常方便地校准时钟频率偏差。以NTP服务器时钟为标准时间,在某一时刻设置NTP客户机时间为NTP服务器当前时间T0,经过一段时间后,NTP服务器时间为T0+tsn,NTP客户端时间为T0+tcn。因为存在时钟频率偏差,tsn与tcn并不相等。NTP客户端时间tcn需乘以时钟频率偏差系数k才等于tsn,即tsn=k×tcn,所以k=tsn/tcn。

任何晶振实际工作频率都是不稳定的,只是程度不同而已。即使温度补偿的晶振,在常温范围内(摄氏10℃~35℃)也有大约5×10-7~2×10-6的误差。晶振实际频率是受外界多种因素(温度、电压、老化等)影响而改变的。因此,时钟频率偏差系数k并非恒定不变的。每隔一定时间,NTP客户机要对时钟频率偏差系数k进行校正,才能保证计时精度。

NTP在Linux
下有两种时钟同步方式:直接同步(也称跳跃同步)和平滑同步(也称微调同步)。
直接同步
使用ntpdate命令进行同步,直接进行时间变更。
如果服务器上存在一个12点运行的任务,当前服务器时间是13点,但标准时间时11点,使用此命令可能会造成任务重复执行。因此使用ntpdate同步可能会引发风险,因此该命令也多用于配置时钟同步服务时第一次同步时间时使用。
注意:如果NTP Client和NTP Server时间偏差太大,可能导致Client的ntpd进程退出。

平滑同步

使用ntpd进行时钟同步,可以保证一个时间不经历两次,它每次同步时间的偏移量不会太陡,是慢慢来的,这正因为这样,ntpd平滑同步可能耗费的时间比较长。刚开始可能时钟不同步,多等待时间后慢慢就同步了。

如果配置成平滑同步 ,vi /etc/sysconfig/ntpd,在OPTIONS选项中增加"-x"参数:
SYNC_HWCLOCK=yes
OPTIONS="-g -x"

查看网络中的NTP服务器

[root@localhost ~]# ntpq  -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+ntp1.flashdance 192.36.143.153   2 u  383  256  376  319.818   18.600   9.687
*tock.ntp.infoma .GPS.            1 u  191  256  377  242.887  -38.469  10.506
+ntp.xtom.nl     193.79.237.14    2 u  139  256  277  143.730    8.851   4.274
-time.cloudflare 10.12.2.186      3 u  186  256  317  210.689   -1.533   7.971
 localhost       .INIT.          16 l    - 1024    0    0.000    0.000   0.000

状态说明

*表示目前使用的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来监控。

实验:

局域网内的NTP同步配置
局域网内1台服务器作为NTP Server,2台服务器作为NTP Client与服务器进行时钟同步:
IP 描述
192.168.40.111 ntpd Server,用于和外部公共ntpd同步标准时间,同时作为内网的Server
192.168.40.113 ntpd Client,用于与ntpd Server同步时间

1.在实验时NTP服务需要使用到UDP端口号123,需要打开,或者关闭防火墙
2.通过date把时间改了在进行下一步操作
3.修改/etc/ntp.conf文件

# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
 
# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1
 
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 
# 允许内网其他机器同步时间,如果不添加该约束默认允许所有IP访问本机同步服务
restrict 192.168.40.0 mask 255.255.255.0 nomodify notrap (修改)
 
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
 
# 配置和上游标准时间同步 (添加)
#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
  1. 修改完以后重启ntp服务
    systemctl restart ntpd
  2. 客户端配置
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
 
driftfile /var/lib/ntp/drift
 
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
 
# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1
 
# Hosts on local network are less restricted.
#restrict 192.168.40.0 mask 255.255.255.0 nomodify notrap
 
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
 
#配置上游时间服务器为本地的ntpd Server服务器  (添加)
server 192.168.40.111
 
# 配置允许上游时间服务器主动修改本机的时间     (添加)
restrict 192.168.40.111 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   (添加)
  1. 启动ntp服务
systemctl  start  ntpd
  1. 查看ntp状态
 # ntpq -p
  1. date查看时间是否更改需要等待5分钟
    借鉴学习文献:https://blog.csdn.net/sunny05296/article/details/65980897?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161425347516780255268536%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=161425347516780255268536&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~hot_rank-1-65980897.first_rank_v2_pc_rank_v29&utm_term=NTP%E6%97%B6%E9%92%9F%E5%90%8C%E6%AD%A5%E9%85%8D%E7%BD%AE
    https://blog.csdn.net/hhcjb/article/details/84940823?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161425347516780255268536%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=161425347516780255268536&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~hot_rank-10-84940823.first_rank_v2_pc_rank_v29&utm_term=NTP%E6%97%B6%E9%92%9F%E5%90%8C%E6%AD%A5%E9%85%8D%E7%BD%AE
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值