ntp时间同步~浅学

定义

NTP是用来使计算机时间同步化的一种协议,全称是Network Time Protocol。它可以使计算机对其服务器或时钟源做同步化,可以提供高精准度的时间校正。它可以在大规模的设备范围内同步矫正时间到几ms级别的精度,在网络稳定的局域网内,精度甚至可以达到微秒级别。

以什么为基准去同步?

UTC(协调世界时)是基于标准的GMT(格林尼治标准时间,Greenwich Mean Time)提供的准确时间。现在一般都是使用UTC,也就是原子钟统一同步全世界的时间。{{ 时间戳 是1970年(数字世界内)至今。}}

架构

image.png

对于一些服务依赖于时间的集群(例如Hadoop集群,Zookeeper集群),就需要有一部主机作为ntp服务器,其他客户端主机从这部主机进行时间同步,另外ntp服务主机从更高一层的服务器获得时间信息。

NTP同步方式在linux下一般两种:使用ntpdate命令直接同步使用NTPD服务平滑同步

使用ntpdate命令直接同步的方式存在风险,比如一些定时任务在已有时间内执行过,直接同步导致时间变回任务执行前的时间段,定时任务会重复执行。

使用NTPD服务平滑同步的方式不会让一个时间点在一天内经历两次,而是平滑同步时间,它每次同步时间的偏移量不会太陡,是慢慢来的。

安装配置

实验环境

centos7.9 虚拟机两台

NTP服务器:192.168.182.50

NTP客户端:192.168.229.50

关闭防火墙
        systemctl stop firewalld
        setenforce 0
手动随便设定个时间:
        date -s '2010-10-10 10:10:10'
yum install -y ntp
vi /etc/ntp.conf
server ntp1.aliyun.com
server 127.0.0.1
server 192.168.182.50
systemctl start ntpd
systemctl enable ntpd --now

查看是否安装NTP

一般CentOS系统自带了该服务,可以通过下面的命令检查是否安装。

rpm -qa | grep ntp

如果有输出ntp和ntpdate版本信息,即已安装。

ntp :作为时间服务器

ntpdate :时间同步某台服务器

安装ntpd

yum install -y ntp

NTP命令

# 查看服务状态
service ntpd status

# 启动ntpd服务
systemctl start ntpd.service

# 停止ntpd服务
systemctl stop ntpd.service

# 设置开机自启动
systemctl enable ntpd.service

# 停止开机自启动
systemctl disable ntpd.service

# 查看服务当前状态
systemctl status ntpd.service

# 重新启动服务
systemctl restart ntpd.service

# 查看所有已启动的服务
systemctl list-units --type=service

搭建ntp服务器,给局域网内的其它机器提供授时服务,以同步全局域网内的时间,保持一致,提供集群部署的环境。

修改服务端配置文件

vim /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
restrict 192.168.182.50 nomodify notrap nopeer noquery ##当前节点IP地址

> restrict: 限制访问权限,只有本地用户和指定的用户才能修改chronyd的配置。
> default: 设置默认的访问权限,如果没有指定其他用户,那么只有root用户可以修改chronyd的配置。
> nomodify: 禁止修改chronyd的配置。
> notrap: 禁止发送通知给其他用户或系统管理员。
> nopeer: 禁止从NTP服务器获取时间信息。
> 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 ntp.sjtu.edu.cn perfer # 放行 ntp.sjtu.edu.cn 进入本 NTP 的服务器
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
###### 授权192.168.182.0网段上所有机器可以从这台机器上查询和时间同步
restrict 192.168.182.0 mask 255.255.255.0 nomodify notrap ### 集群所在网段、掩码、权限
###### 权限的设定主要以restrict这个参数来设定,主要的语法为:
  restrict  IP地址  mask 子网掩码 参数
其中IP可以是IP地址也可以是default,default是指所有的IP,默认是default,如果为了安全可以将default替换为网段,192.168.182.0/24

restrict选项格式
“客户端IP” 和 “IP掩码” 指定了对网络中哪些范围的计算机进行控制,如果使用default关键字,则表示对所有的计算机进行控制,参数指定了具体的限制内容,常见的参数如下:
◆ ignore:拒绝连接到NTP服务器 即关闭所有的 NTP 联机服务
◆ nomodify: 客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
◆ noquery: 不提供客户端的时间查询
◆ notrap: 不提供trap远程登录功能,trap服务是一种远程时间日志服务。
◆ notrust: 客户端除非通过认证,否则该客户端来源将被视为不信任子网 。
◆ nopeer: 提供时间服务,但不作为对等体。
◆ kod: 向不安全的访问者发送Kiss-Of-Death报文。

# 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 0-n
server ntp.sjtu.edu.cn prefer # prefer
server 127.127.1.0 				#指定了Chrony服务器的IP地址为本地回环地址(127.127.1.0),这意味着Chrony将使用本机作为时间服务器。
fudge 127.127.1.0 stratum 10	#当与服务器通信失败时,Chrony会尝试从本地时间偏移10秒进行修正。
server选项格式
server host [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]
其中host是上层NTP服务器的IP地址或域名,随后所跟的参数解释如下所示:
◆ key: 表示所有发往服务器的报文包含有秘钥加密的认证信息,n是32位的整数,表示秘钥号。
◆ version: 表示发往上层服务器的报文使用的版本号,n默认是3,可以是1或者2。
◆ prefer: 如果有多个server选项,具有该参数的服务器优先使用。
◆ mode: 指定数据报文mode字段的值。
◆ minpoll: 指定与查询该服务器的最小时间间隔为2的n次方秒,n默认为6,范围为4-14。
◆ maxpoll: 指定与查询该服务器的最大时间间隔为2的n次方秒,n默认为10,范围为4-14。
◆ iburst: 当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2秒。

#broadcast 192.168.1.255 autokey	# broadcast server
#broadcastclient			# broadcast client
#broadcast 224.0.1.1 autokey		# multicast server
#multicastclient 224.0.1.1		# multicast client
#manycastserver 239.255.254.254		# manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
keys /etc/ntp/keys

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats

# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor

引用其他博客的/etc/ntp.conf 配置内容
1. 先处理权限方面的问题,包括放行上层服务器以及开放局域网用户来源:
 
restrict default kod nomodify notrap nopeer noquery <==拒绝 IPv4 的用户
 
restrict -6 default kod nomodify notrap nopeer noquery  <==拒绝 IPv6 的用户
 
restrict 220.130.158.71   <==放行 tock.stdtime.gov.tw 进入本 NTP 的服务器
 
restrict 59.124.196.83<==放行 tick.stdtime.gov.tw 进入本 NTP 的服务器
 
restrict 59.124.196.84<==放行 time.stdtime.gov.tw 进入本 NTP 的服务器
 
restrict 127.0.0.1<==底下两个是默认值,放行本机来源
 
restrict -6 ::1
 
restrict 192.168.100.0 mask 255.255.255.0 nomodify <==放行局域网用户来源,或者列出单独IP
 
 
2. 设定主机来源,请先将原本的 [0|1|2].centos.pool.ntp.org 的设定批注掉:
  
server 220.130.158.71 prefer  <==以这部主机为最优先的server
 
server 59.124.196.83
 
server 59.124.196.84
 
 
3.默认的一个内部时钟数据,用在没有外部 NTP 服务器时,使用它为局域网用户提供服务:
 
server127.127.1.0 # local clock
 
fudge 127.127.1.0 stratum 10
 
 
4.预设时间差异分析档案与暂不用到的 keys 等,不需要更动它:
 
driftfile /var/lib/ntp/drift
 
keys  /etc/ntp/keys

启动服务,设置开机自启

systemctl start ntpd
systemctl enable ntpd
ntpq -p #查看当前服务器与上层 ntp 的状态


remote - 本机和上层ntp的ip或主机名,“+”有连线可做候选,“*”正在使用的
refid - 更上一层的ntp地址
st - stratum的 级别
when - 多少秒前曾经同步过时间
poll - 下次更新在多少秒后
reach - 已经向上层ntp服务器要求更新的次数
delay - 网络延迟
offset - 时间补偿
jitter - 系统时间与bios时间差

查看服务器ntp服务状态

[root@localhost ~]# ntpstat
unsynchronised
time server re-starting
polling server every 8 s
表示同步上级时间服务器成功。
立即查看时间没有同步,不用担心,等待5到10分钟后再次查看
再次查看时间 date

修改客户端配置文件

vim /etc/ntp.conf

restrict 192.168.229.50 nomodify notrap nopeer noquery 		#当前节点IP地址
restrict 192.168.229.0 mask 255.255.255.0 nomodify notrap 	#集群所在网段的网段,子网掩码
server 192.168.182.50 iburst #NTP服务器IP地址,可以注释掉别的server或者在需要配置的ntp服务器后添加perfer--优先,提高优先级,如:server 192.168.182.50 perfer iburst
fudge 192.168.182.50 stratum 10

查看ntp服务状态

#  查看ntp服务状态
systemctl status ntpd

# 查看ntp链接状态
ntpq -p

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.          10 l   10   64  377    0.000    0.000   0.000

> remote 	详细说明了我们连接到的NTP服务器。 # 本机和上层ntp的ip或主机名,“+”有连线可做候选,“*”正在使用的
> refid 	表示远程服务器连接的NTP服务器。	# 上一层服务器
> st 		指的是服务器的层级,指的是服务器离我们有多“近”(数字越低,越“近”,通常越好)> t 		表示类型,具体来说是服务器使用单播、广播、多播还是多播。
> when 	指的是距离上次轮询服务器的时间有多长。
> poll 	表明服务器将被轮询的频率,对于示例屏幕截图中的大多数条目,轮询时间为64秒。
> reach 	包含最近8次NTP更新的结果。如果所有8个都成功,这个字段将读取377。#这个数字是八进制的,正常情况下值为[0, 1, 3, 7, 17, 37, 77, 177, 377],对应的二进制为[0, 1, 11, 111, 1111, 11111, 111111, 1111111, 11111111],所以8个成功的八进制数将被表示为377。当您第一次在服务器上启动ntp守护进程时,这个数字可能需要一段时间才能达到377。
> delay 表示到达服务器的延迟时间,单位为毫秒。
> offset 指的是本地时钟和服务器时钟之间的差异。
> jitter 指的是remote和refid服务器之间的网络延迟。


######## 查看端口

netstat -anup | grep 123
udp        0      0 192.168.182.50:123      0.0.0.0:*                           2604/ntpd           
udp        0      0 127.0.0.1:123           0.0.0.0:*                           2604/ntpd           
udp6       0      0 ::1:123                 :::*                                2604/ntpd 

错误

no server suitable for synchronization found

原因: 在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。

那么如何知道何时ntp server完成了和自身同步的过程呢?

在ntp server上使用命令:

watch ntpq -p

ntp服务启动后,reach就以poll值为周期与ntp server通信,为了方便理解,我们可以简单的认为每次ping一下上层ntp server,如果成功,那reach就向左移一位,右边补1,如果失败,则右边补0,所以如果reach不是上面给出的枚举值,那就是在通信过程中出错了。
当reach 达到17时(对应1111,即最近的四次通信都成功了),那才开始同步时间,这时,remote项对应的域名或IP列表有,其中一个前面会有*号,表示该IP就是NTP server。而在开始同步时间之前,当客户端访问weblbserver-1这个NTP server时,都会出现stratum 16,no server suitable fornchronization found这样的错误。也就是说如果你在NTP server主机上重启了ntp服务,那要等4poll秒(在前四次通信都是成功的前提下),该NTP server才与上层NTP server开始同步时间,而且只有当开始同步时,该NTP server才能为其它客户端提供NTP服务。
因此,你在/etc/ntp.conf中设置的同步周期minpoll maxpoll不能太大,因为每次ntp服务重启后,要等4倍长的时间才能开始同步。

让linux运行ntpdate更新时间时,linux不能开启NTP服务,否则会提示端口被占用:
如下

[root@ntp_client ~]# ntpdate ntp_server
30 Nov 09:50:59 ntpdate[4394]: the NTP socket is in use, exiting

同步硬件时钟

ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,
在/etc/sysconfig/ntpd文件中 添加【SYNC_HWCLOCK=yes】,就可以让硬件时间与系统时间一起同步。
允许BIOS与系统时间同步,也可以通过 hwclock -w 命令。

国内常用NTP服务器地址及IP

210.72.145.44 (国家授时中心服务器IP地址)  
133.100.11.8 日本 福冈大学  
time-a.nist.gov 129.6.15.28 NIST, Gaithersburg, Maryland   
time-b.nist.gov 129.6.15.29 NIST, Gaithersburg, Maryland   
time-a.timefreq.bldrdoc.gov 132.163.4.101 NIST, Boulder, Colorado   
time-b.timefreq.bldrdoc.gov 132.163.4.102 NIST, Boulder, Colorado   
time-c.timefreq.bldrdoc.gov 132.163.4.103 NIST, Boulder, Colorado   
utcnist.colorado.edu 128.138.140.44 University of Colorado, Boulder   
time.nist.gov 192.43.244.18 NCAR, Boulder, Colorado   
time-nw.nist.gov 131.107.1.10 Microsoft, Redmond, Washington   
nist1.symmetricom.com 69.25.96.13 Symmetricom, San Jose, California   
nist1-dc.glassey.com 216.200.93.8 Abovenet, Virginia   
nist1-ny.glassey.com 208.184.49.9 Abovenet, New York City   
nist1-sj.glassey.com 207.126.98.204 Abovenet, San Jose, California   
nist1.aol-ca.truetime.com 207.200.81.113 TrueTime, AOL facility, Sunnyvale, California   
nist1.aol-va.truetime.com 64.236.96.53 TrueTime, AOL facility, Virginia  
————————————————————————————————————  
ntp.sjtu.edu.cn 202.120.2.101 (上海交通大学网络中心NTP服务器地址)  
s1a.time.edu.cn 北京邮电大学  
s1b.time.edu.cn 清华大学  
s1c.time.edu.cn 北京大学  
s1d.time.edu.cn 东南大学  
s1e.time.edu.cn 清华大学  
s2a.time.edu.cn 清华大学  
s2b.time.edu.cn 清华大学  
s2c.time.edu.cn 北京邮电大学  
s2d.time.edu.cn 西南地区网络中心  
s2e.time.edu.cn 西北地区网络中心  
s2f.time.edu.cn 东北地区网络中心  
s2g.time.edu.cn 华东南地区网络中心  
s2h.time.edu.cn 四川大学网络管理中心  
s2j.time.edu.cn 大连理工大学网络中心  
s2k.time.edu.cn CERNET桂林主节点  
s2m.time.edu.cn 北京大学
ntp.aliyun.com  阿里云

时钟的跃变问题

什么是时钟跃变?
前面提到过,ntpdate是直接同步,更新时间是线性的更新,这就造成有可能两个时间点(更新前是一个时间点,更新后是一个时间点)时间倒流(比如更新前时间是0点0分5秒,更新后时间是0点0分0秒),或者两个时间点不变,这就造成了时间静止,例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的。

问题:
第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,***者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。
第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。
第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。

BUT:
NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的自然漂移(drift)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。

  • 46
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值