NTP时钟同步配置

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"
 

快速同步/平滑同步的设置
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偏差后,才进行策略的调整。


标准的时钟同步服务器

http://www.pool.ntp.org/zone/cn网站包含全球的标准时间同步服务,也包括对中国时间的同步,对应的URL为cn.pool.ntp.org,推荐的ntp配置文件中的格式:
server 3.cn.pool.ntp.org
server 1.asia.pool.ntp.org
server 3.asia.pool.ntp.org
 
NTP相关常用命令:
重启ntpd服务
# systemctl restart ntpd
 

直接进行时间同步变更

ntpdate命令用来设置调整本地日期和时间。它从指定的每个服务器获得了一些样本,并应用标准NTP时钟过滤器和选择算法来选择最好的样本。

* 如果它确定时钟偏差超过0.5秒,它通过调用settimeofday子例程设置时钟时间。在引导时间,这是一个首选的方法。
* 如果它确定时钟偏差小于0.5秒,它通过调用adjtime子例程和偏移量来调整时钟时间。此方法倾向于用牺牲一些稳定性来保持漂移时钟更加准确。当不是通过运行一个守护程序而是从cron命令有规则的运行ntpdate命令时,每一小时或两小时执行一次可以保证足够的走时精度,从而避免调整时钟。
 
使用多个NTP Server可以大幅度改善ntpdate命令的可靠性与精度。尽管允许使用单一NTP Server,但还是建议您通过配置至少3~4个NTP Server以获得更好的性能。
 
如果一个类似 xntpd 守护程序的 NTP 服务器守护程序正在同一主机上运行,命令将拒绝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
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 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来查询详细错误信息,再做判断。
 
ntpdate -d排查错误信息:
1)Server dropped: no data
  检查ntp的版本(ntpq -c version),如果ntp版本>=4.2,在restrict的定义中使用了notrust的话,会导致以上错误。需要删除notrust。
2)检查NTP Server的防火墙,是否屏蔽了UDP 123端口。
 
查看同步状态
如果是内网,一般ntpstat很快就可以同步上
ntpstat 
ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。

[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 ~]# 

 
如果ntp客户端和服务端同步有问题,可使用下面命令查看详细信息:
ntpdate –d serverIP 
-d参数只是用于调试,显示效果而已,不会真实的改变系统的时间
restrict控制相关权限
语法为: restrict IP地址 mask 子网掩码 参数
 
其中IP地址也可以是default ,default就是指所有的IP
 
参数有以下几个:
ignore  :关闭所有的 NTP 联机服务
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器
notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
kod : 访问违规时发送 KoD 包。
restrict -6 表示IPV6地址的权限设置。
 
局域网内的NTP同步配置
局域网内1台服务器作为NTP Server,2台服务器作为NTP Client与服务器进行时钟同步:
IP                     描述
192.168.111.254        ntpd Server,用于和外部公共ntpd同步标准时间,同时作为内网的Server
192.168.111.129        ntpd Client,用于与ntpd Server同步时间
192.168.111.130        ntpd Client,用于与ntpd Server同步时间
 
1.检查ntp包是否已经安装
# rpm -q ntp
ntp-4.2.6p5-19.el7.centos.x86_64
如果没有安装,则需要先安装并设置开机自动启动ntpd服务
# yum -y install ntp
# systemctl enable ntpd
# systemctl start ntpd
 
2.防火墙配置
由于NTP服务需要使用到UDP端口号123,所以当系统的防火墙(Iptables)启动的情况下,必须开放UDP端口号123。
 
3.配置内网ntpd Server:192.168.111.254
1)配置前先使用命令同步时间,本机与外部时间服务器时间差距太大,让ntpd不能正常同步:
ntpdate -u cn.pool.ntp.org
2)修改/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.111.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 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 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
 
#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
 
修改完成后重启ntpd服务:
systemctl restart ntpd
 
查看网络中的NTP服务器,同时显示客户端和每个服务器的关系:
# ntpq -p 
 
查看时间同步状态
# ntpstat
synchronised to local net at stratum 11 
   time correct to within 7948 ms
   polling server every 64 s
这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。
刚启动的时候,执行ntpstat,会显示unsynchronised:
# ntpstat 
unsynchronised
  time server re-starting
   polling server every 64 s
同步成功以后,会显示:
# ntpstat 
synchronised to NTP server (202.112.10.36) at stratum 3
   time correct to within 275 ms
   polling server every 256 s
 
配置内网ntpd Client:192.168.111.129、130
1)检查ntp是否安装,以及是否设置了自启动,参考ntpd Server的ntp安装检查。
2)修改/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
 
# 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.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

 
#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
 
先和本地ntpd Server同步一下
# ntpdate -u 192.168.111.254
 
重启ntpd服务
# systemctl restart ntpd
 
查看状态
# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 *192.168.111.254      LOCAL(0)        11 u   24   64    1    1.626  5182468   0.000
 
 

猜你喜欢

转载自www.cnblogs.com/30go/p/11231110.html