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
 
 

NTP时钟同步配置的更多相关文章

  1. Windows 2008 R2 NTP 时钟同步配置

    一.配置 本地组策略 a.windows+R 或  "开始菜单"  | "运行"  ,打开运行窗口. 输入gpedit.msc打开本地组策略 b.在 本地组策略 ...

  2. PTP时钟和NTP时钟同步有什么区别

    PTP时钟 理论上任何PTP时钟都能实现主时钟和从时钟的功能,但一个PTP通信子网内只能有一个主时钟.整个系统中的最优时钟为最高级时钟GMC(Grandmaster Clock),有着最好的稳定性.精 ...

  3. [原]NTP时钟同步服务设置

    服务器列表 192.168.0.2 ntp服务端 192.168.0.3 ntp客户端 192.168.0.4 ntp客户端 192.168.0.5 ntp客户端 注:以下操作均以root操作 一.N ...

  4. Linux下的ntp时钟同步问题

    前段时间,项目中有个需求,需要将linux和windows的时间进行同步,网上也有很多类似时钟同步的帖子,大致类似:不过本次的linux的机器有点特殊,没有service命令,而且要求在另一台suse ...

  5. ntp 时钟同步

    注意: 如果你无法和外部网络的时钟同步,请检查UDP端口时候被封.

  6. NTP时钟同步学习记录

    --1 要点回顾 . 1. NTP唯一配置文件:/etc/ntp.conf . 2. NTP系统日志记录:/var/log/ntp . 3. ntp.conf简要介绍 - 利用 restrict 来管 ...

  7. ntp时钟同步

    服务器时间的一致性,很关键的. 11. 基于ntp服务的形式 [root@server0 ~]# yum -y install chrony   //NTP客户端    centos7.x  cent ...

  8. Linux(Unix)时钟同步ntpd服务配置方法

    http://xu20cn.blog.51cto.com/274020/69689 假定时钟服务器IP地址为:192.168.0.1 服务器端配置: 1:置/etc/ntp.conf文件内容为: se ...

  9. buildroot ntp 网络时钟同步

    /********************************************************************** * buildroot ntp 网络时钟同步 * 说明: ...

随机推荐

  1. Java开发环境之MySql

    查看更多Java开发环境配置,请点击<Java开发环境配置大全> 叁章:MySql安装教程 1)去官网下载MySql安装包 https://www.mysql.com/downloads/ ...

  2. windows动态库和静态库VS导入

    1. 静态库和动态库 1.1 静态库(.lib) 函数和数据被编译进一个二进制文件(通常扩展名为.LIB).在使用静态库的情况下,在编译链接可执行文件时,链接器从库中复制这些函数和数据并把它们和应用程 ...

  3. 详解Linux系统中10个最危险的命令

    概述 大多数的朋友都是主要用的windows系统,基本用鼠标就可以完成所有的操作,但是在Linux系统中很多都是键盘+命令操作电脑的,Linux命令行使用很有趣,但有时候也很危险,尤其是在你不确定你自 ...

  4. 微信小程序校验文件在浏览器无法打开

    txt文件放在网站根目录后浏览器无法打开,同一目录的其他txt文件却可以打开.我试过了修改文件权限.修改所有者.修改文件编码等各种办法都没有用. 最后找到了.htaccess文件,原来在这个文件里修改 ...

  5. mongodb移除分片和添加分片(转)

    首先我们要移除的分片之后再次添加此分片时会出现添加失败的情况,需要在添加的分片上登录进行删除此分片之前数据库的历史数据比如testdb,删除分片上的数据库之后就可重新添加此分片到mongos中 1.执 ...

  6. dockerhub下载加速

    curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f5dad4ec.m.daocloud.io syste ...

  7. Please umount the filesystem and rectify the problem(s)

    1.输入命令:ls -l /dev/mapper 2.再输入:xfs_repair /dev/dm-0 3.输入命令:xfs_repair -L /dev/dm-0 4.最后进行重启:init 6 等 ...

  8. TAPD---“文档”的用途

    主要用途:文件的存放 (1)对于测试组:存放测试用例.主要针对当前的迭代,可新建对应的文件夹,上传存放相应的xmind.excel文件.方便开发查找用例文件 (2)对于项目:存放共用的文档等 这里只是 ...

  9. MySQL之自连接

    自连接就是说,在同一个表中,看做是两个表,下表表示 找每个人的领导,如果没有领导,显示无领导,eid 对应 leaderid,请看员工表 mysql> select * from emp; +- ...

  10. web自动化测试-selenium多表单切换

    一.概述 1.在web应用中会经常遇到frame/iframe表单嵌套页面的应用 2.WebDriver只能在一个页面上对元素进行识别与定位 3.对于frame/iframe表单内嵌的页面上元素无法识 ...