多台linux服务器时间同步

多台linux服务器时间同步

 

ntp安装:https://www.cnblogs.com/quchunhui/p/7658853.html

https://www.cnblogs.com/liushui-sky/p/9203657.html

https://www.cnblogs.com/30go/p/11231110.html

一、设置A服务器为时间服务器(加入A服务器IP为172.18.102.2)

1 修改 /etc/ntp.conf,如下所示:

 命令:vi /etc/ntp.conf

 内容:

  # Undisciplined Local Clock. This is a fake driver intended for backup
  # and when no outside source of synchronized time is available.
  server 127.127.1.0 # local clock
  #fudge 127.127.1.0 stratum 10

2 启动时间服务器,命令如下:

 service ntpd status

 service ntpd start

3 查看启动结果,如下所示:

 命令:ntpq -p

 结果:

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

4 设置自启动,命令如下:

 chkconfig ntpd on

二、设置需要同步的服务器

1 修改 /etc/ntp.conf,如下:

 命令:vi /etc/ntp.conf

 内容:

  # Undisciplined Local Clock. This is a fake driver intended for backup
  # and when no outside source of synchronized time is available.
  server 172.18.102.2 # local clock 与服务器A机时间同步
  fudge 172.18.102.2 stratum 10

2 启动时间服务器,命令如下:

 service ntpd status

 service ntpd start

3 查看启动结果,如下所示:

 命令:ntpq -p

 结果:

  remote refid st t when poll reach delay offset jitter
  ==============================================================================
  172.18.102.2 LOCAL(0) 6 u 5 64 1 1.377 -7077.8 0.000

4 设置自启动,命令如下:

 chkconfig ntpd on

5 立即同步

 ntpdate -d 172.18.102.2

 
第一种:

Linux配置ntp时间服务器(全)

 

时间服务器作用:

大数据产生与处理系统是各种计算设备集群的,计算设备将统一、同步的标准时间用于记录各种事件发生时序,

如E-MAIL信息、文件创建和访问时间、数据库处理时间等。

大数据系统内不同计算设备之间控制、计算、处理、应用等数据或操作都具有时序性,

若计算机时间不同步,这些应用或操作或将无法正常进行。

大数据系统是对时间敏感的计算处理系统,时间同步是大数据能够得到正确处理的基础保障,是大数据得以发挥作用的技术支撑。

大数据时代,整个处理计算系统内的大数据通信都是通过网络进行。

时间同步也是如此,利用大数据的互联网络传送标准时间信息,实现大数据系统内时间同步。

网络时间同步协议(NTP)是时间同步的技术基础。

 

(一)确认ntp的安装

1)确认是否已安装ntp

【命令】rpm –qa | grep ntp

若只有ntpdate而未见ntp,则需删除原有ntpdate。如:

ntpdate-4.2.6p5-22.el7_0.x86_64

fontpackages-filesystem-1.44-8.el7.noarch

python-ntplib-0.3.2-1.el7.noarch

 

2)删除已安装ntp

【命令】yum –y remove ntpdate-4.2.6p5-22.el7.x86_64

 

3)重新安装ntp

【命令】yum –y install ntp

 

(二)配置ntp服务

1)修改所有节点的/etc/ntp.conf

【命令】vi /etc/ntp.conf

【内容】

restrict 192.168.6.3 nomodify notrap nopeer noquery          //当前节点IP地址

restrict 192.168.6.2 mask 255.255.255.0 nomodify notrap  //集群所在网段的网关(Gateway),子网掩码(Genmask)

 

2)选择一个主节点,修改其/etc/ntp.conf

【命令】vi /etc/ntp.conf

【内容】在server部分添加一下部分,并注释掉server 0 ~ n

server 127.127.1.0

Fudge 127.127.1.0 stratum 10

 

3)主节点以外,继续修改/etc/ntp.conf

【命令】vi /etc/ntp.conf

【内容】在server部分添加如下语句,将server指向主节点。

server 192.168.6.3

Fudge 192.168.6.3 stratum 10

 

===修改前===

 

===修改后===

节点1(192.168.6.3):

 

节点2(192.168.6.4):

 

节点3(192.168.6.5):

 

 (三)启动ntp服务、查看状态

1)启动ntp服务

【命令】service ntpd start

 

2)查看ntp服务器有无和上层ntp连通

【命令】ntpstat

查看ntp状态时,可能会出现如下所示情况

① unsynchronised time server re-starting polling server every 8 s

② unsynchronised polling server every 8 s

这种情况属于正常,ntp服务器配置完毕后,需要等待5-10分钟才能与/etc/ntp.conf中配置的标准时间进行同步。

等一段时间之后,再次使用ntpstat命令查看状态,就会变成如下正常结果:

 

3)查看ntp服务器与上层ntp的状态

【命令】ntpq -p

remote:本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先

refid:参考上一层ntp主机地址

st:stratum阶层

when:多少秒前曾经同步过时间

poll:下次更新在多少秒后

reach:已经向上层ntp服务器要求更新的次数

delay:网络延迟

offset:时间补偿

jitter:系统时间与bios时间差

 

4)查看ntpd进程的状态

【命令】watch "ntpq -p"

【终止】按 Ctrl+C 停止查看进程。

第一列中的字符指示源的质量。星号 ( * ) 表示该源是当前引用。

remote:列出源的 IP 地址或主机名。

when:指出从轮询源开始已过去的时间(秒)。

poll:指出轮询间隔时间。该值会根据本地时钟的精度相应增加。

reach:是一个八进制数字,指出源的可存取性。值 377 表示源已应答了前八个连续轮询。

offset:是源时钟与本地时钟的时间差(毫秒)。

 

(四)设置开机启动

【命令】chkconfig ntpd on

 

(五)从其他博客的一些参考摘录

===/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 服务器时,使用它为局域网用户提供服务:
# server    127.127.1.0     # local clock
# fudge     127.127.1.0 stratum 10

# 4.预设时间差异分析档案与暂不用到的 keys 等,不需要更动它:
driftfile /var/lib/ntp/drift
keys      /etc/ntp/keys
 
复制代码

 

===restrict选项格式===

restrict [ 客户端IP ]  mask  [ IP掩码 ]  [参数]

“客户端IP” 和 “IP掩码” 指定了对网络中哪些范围的计算机进行控制,如果使用default关键字,则表示对所有的计算机进行控制,参数指定了具体的限制内容,常见的参数如下:

◆ ignore:拒绝连接到NTP服务器

◆ nomodiy: 客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。

◆ noquery: 不提供客户端的时间查询

◆ notrap: 不提供trap远程登录功能,trap服务是一种远程时间日志服务。

◆ notrust: 客户端除非通过认证,否则该客户端来源将被视为不信任子网 。

◆ nopeer: 提供时间服务,但不作为对等体。

◆ kod: 向不安全的访问者发送Kiss-Of-Death报文。

  

===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秒。

 

===查看网关方法===

【命令1】route -n  

【命令2】ip route show  

【命令3】netstat -r

 

===层次(stratum)===

stratum根据上层server的层次而设定(+1)。

对于提供network time service provider的主机来说,stratum的设定要尽可能准确。

而作为局域网的time service provider,通常将stratum设置为10

 

0层的服务器采用的是原子钟、GPS钟等物理设备,stratum 1与stratum 0 是直接相连的,

往后的stratum与上一层stratum通过网络相连,同一层的server也可以交互。

ntpd对下层client来说是service server,对于上层server来说它是client。

ntpd根据配置文件的参数决定是要为其他服务器提供时钟服务或者是从其他服务器同步时钟。所有的配置都在/etc/ntp.conf文件中。

 

===注意防火墙屏蔽ntp端口===

ntp服务器默认端口是123,如果防火墙是开启状态,在一些操作可能会出现错误,所以要记住关闭防火墙。

 

===同步硬件时钟===

ntp服务,默认只会同步系统时间。

如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,

在/etc/sysconfig/ntpd文件中,添加【SYNC_HWCLOCK=yes】这样,就可以让硬件时间与系统时间一起同步。

允许BIOS与系统时间同步,也可以通过hwclock -w 命令。

 

===ntpd、ntpdate的区别===

下面是网上关于ntpd与ntpdate区别的相关资料。如下所示所示:

使用之前得弄清楚一个问题,ntpd与ntpdate在更新时间时有什么区别。

ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,

并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。

时钟的跃变,对于某些程序会导致很严重的问题。

 

许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时间是线性的,

一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。

不幸的是,ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,

这有几个非常明显的问题:

【一】这样做不安全。

ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。

由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。

【二】这样做不精确。

一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。

与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。

【三】这样做不够优雅。

由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错

(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。

因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。

其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。

NTPD在和时间服务器的同步过程中,会把BIOS计时器的振荡频率偏差——或者说Local Clock的自然漂移(drift)——记录下来。

这样即使网络有问题,本机仍然能维持一个相当精确的走时。

 

===国内常用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 北京大学
复制代码

 

==2020年9月29日追记==

配置了ntp服务之后,发现时间仍然无法同步,在网上查了半天,

通过命令timedatectl查看,之后,发现ntp服务没有生效(NTP enabled: no),如下图所示

执行命令timedatectl set-ntp yes之后,问题得以解决。

 

第二种:

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

 

第三种:

linux时间同步,ntpd、ntpdate

在Windwos中,系统时间的设置很简单,界面操作,通俗易懂。而且设置后,重启,关机都没关系。系统时间会自动保存在Bios的时钟里面,启动计算机的时候,系统会自动在Bios里面取硬件时间,以保证时间的不间断。

    但在Linux下,默认情况下,系统时间和硬件时间,并不会自动同步。在Linux运行过程中,系统时间和硬件时间以异步的方式运行,互不干扰。硬件时间的运行,是靠Bios电池来维持,而系统时间,是用CPU tick来维持的。

    在系统开机的时候,会自动从Bios中取得硬件时间,设置为系统时间。

 

.Linux系统时间的设置

    在Linux中设置系统时间,可以用date命令:

//查看时间

[root@localhost ~]# date
2008年 12月 12日星期五 14:44:12 CST

//修改时间
[root@localhost ~]# date -set  "2013-12-24 00:01" <== (年/月/日 时:分【:秒】)
2009年 01月 01日星期四 00:01:00 CST

//date 有几种时间格式可接受,这样也可以设置时间:

[root@localhost ~]# date 012501012009.30  <== 月日时分年.秒
2009年 01月 25日星期日 01:01:30 CST

 

 .Linux硬件时间的设置

    硬件时间的设置,可以用hwclock或者clock命令。其中,clock和hwclock用法相近,只用一个就行,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。

//查看硬件时间可以是用hwclock,hwclock --show 或者hwclock -r

[root@localhost ~]# hwclock --show
2008年12月12日星期五 06时52分07秒  -0.376932 seconds

//设置硬件时间

[root@localhost ~]# hwclock --set --date="1/25/09 00:00" <== 月/日/年时:分:秒
[root@localhost ~]# hwclock
2009年01月25日星期日 00时00分06秒  -0.870868 seconds

[root@localhost ~]# hwclock  -w   根据系统时间设置硬件时间

 

.系统时间和硬件时间的同步

    同步系统时间和硬件时间,可以使用hwclock命令。

//以系统时间为基准,修改硬件时间

[root@localhost ~]# hwclock --systohc<== sys(系统时间)to(写到)hc(Hard Clock)
[root@localhost ~]# hwclock -w

//以硬件时间为基准,修改系统时间

[root@localhost ~]# hwclock --hctosys
[root@localhost ~]# hwclock -s

 

.不同机器之间的时间同步(重点)

    为了避免主机时间因为长期运作下所导致的时间偏差,进行时间同步(synchronize)的工作是非常必要的。Linux系统下,一般使用ntp服务器来同步不同机器的时间。一台机器,可以同时是ntp服务器和ntp客户机。在网络中,推荐使用像DNS服务器一样分层的时间服务器来同步时间。

    同步时间,可以使用ntpdate命令,也可以使用ntpd服务(debian中ntp服务器为ntp服务,需要安装apt-get install ntp,同时对上手动同步时间需要用到ntpdate,故还需安装apt-get install  ntpdate)。

    使用ntpdate比较简单。格式如下:

[root@linux ~]# ntpdate [-nv] [NTP IP/hostname]
[root@linux ~]# ntpdate 192.168.0.2
[root@linux ~]# ntpdate time.ntp.org

    但这样的同步,只是强制性的将系统时间设置为ntp服务器时间。如果cpu tick有问题,只是治标不治本。所以,一般配合cron命令,来进行定期同步设置。比如,在crontab中添加:

0 12 * * * * /usr/sbin/ntpdate 192.168.0.1

     这样,会在每天的12点整,同步一次时间。ntp服务器为192.168.0.1。

    使用ntpd服务,要好于ntpdate加cron的组合。因为,ntpdate同步时间,会造成时间的跳跃,对一些依赖时间的程序和服务会造成影响。比如sleep,timer等。而且,ntpd服务可以在修正时间的同时,修正cpu tick。理想的做法为,在开机的时候,使用ntpdate强制同步时间,在其他时候使用ntpd服务来同步时间。

    要注意的是,ntpd有一个自我保护设置: 如果本机与上源时间相差太大, ntpd不运行. 所以新设置的时间服务器一定要先ntpdate从上源取得时间初值, 然后启动ntpd服务。ntpd服务运行后, 先是每64秒与上源服务器同步一次, 根据每次同步时测得的误差值经复杂计算逐步调整自己的时间, 随着误差减小, 逐步增加同步的间隔. 每次跳动, 都会重复这个调整的过程.

 

.ntpd服务的设置

    ntpd服务的相关设置文件如下:

1./etc/ntp.conf:这个是NTP daemon的主要设文件,也是 NTP 唯一的设定文件。

2./usr /share/zoneinfo/:在这个目录下的文件其实是规定了各主要时区的时间设定文件,例如北京地区的时区设定文件在/usr/share/zoneinfo/Asia/Beijing 就是了。这个目录里面的文件与底下要谈的两个文件(clock 与localtime)是有关系的。

3./etc/sysconfig/clock:这个文件其实也不包含在NTP 的 daemon 当中,因为这个是linux的主要时区设定文件。每次开机后,Linux 会自动的读取这个文件来设定自己系统所默认要显示的时间。

4./etc /localtime:这个文件就是“本地端的时间配置文件”。刚刚那个clock 文件里面规定了使用的时间设置文件(ZONE) 为/usr/share/zoneinfo/Asia/Beijing ,所以说,这就是本地端的时间了,此时, Linux系统就会将Beijing那个文件另存为一份/etc/localtime文件,所以未来我们的时间显示就会以Beijing那个时间设定文件为准。

5. /etc/timezone:系统时区文件

 

    下面重点说说 /etc/ntp.conf文件的设置。在 NTP Server 的设定上面,其实最好不要对 Internet 无限制的开放,尽量仅提供您自己内部的 Client 端联机进行网络校时就好。此外, NTP Server 总也是需要网络上面较为准确的主机来自行更新自己的时间啊,所以在我们的 NTP Server 上面也要找一部最靠近自己的 Time Server 来进行自我校正。事实上, NTP 这个服务也是Server/Client 的一种模式。

[root@linux ~]# vi /etc/ntp.conf
# 1. 关于权限设定部分
#  权限的设定主要以 restrict 这个参数来设定,主要的语法为:
#   restrict IP mask netmask_IP parameter
#   其中 IP 可以是软件地址,也可以是 default ,default 就类似 0.0.0.0
#  至于paramter则有:
#   ignore :关闭所有的 NTP 联机服务
#   nomodify:表示 Client 端不能更改 Server 端的时间参数,不过,

#   Client 端仍然可以透过 Server 端来进行网络校时。
#   notrust:该 Client 除非通过认证,否则该 Client 来源将被视为不信任网域
#   noquery:不提供 Client 端的时间查询

#   notrap:不提供trap这个远程事件登入

#  如果paramter完全没有设定,那就表示该 IP (或网域)“没有任何限制”

restrict default nomodifynotrapnoquery # 关闭所有的 NTP 要求封包
restrict 127.0.0.1    #这是允许本级查询
restrict 192.168.0.1 mask 255.255.255.0 nomodify
#在192.168.0.1/24网段内的服务器就可以通过这台NTP Server进行时间同步了
# 2. 上层主机的设定
#  要设定上层主机主要以 server 这个参数来设定,语法为:
#  server [IP|HOST Name] [prefer]
#  Server 后面接的就是我们上层 Time Server 啰!而如果 Server 参数
#  后面加上perfer的话,那表示我们的 NTP 主机主要以该部主机来作为
#  时间校正的对应。另外,为了解决更新时间封包的传送延迟动作,
#  所以可以使用driftfile来规定我们的主机
#  在与 Time Server 沟通时所花费的时间,可以记录在driftfile 
#  后面接的文件内,例如下面的范例中,我们的 NTP server 与 
#  cn.pool.ntp.org联机时所花费的时间会记录在 /etc/ntp/drift文件内
server 0.pool.ntp.org

server 1.pool.ntp.org

server 2.pool.ntp.org

server cn.pool.ntp.org prefer

#其他设置值,以系统默认值即可

server  127.127.1.0     # local clock

fudge   127.127.1.0 stratum 10

driftfile /var/lib/ntp/drift
broadcastdelay  0.008
keys /etc/ntp/keys

总结一下,restrict用来设置访问权限,server用来设置上层时间服务器,driftfile用来设置保存漂移时间的文件。

 

.ntp服务的启动与观察

在启动NTP服务前,先对提供服务的这台主机手动的校正一次时间咯。(因为启动服务器,端口会被服务端占用,就不能手动同步时间了)

[root@linux ~] # ntpdate cn.pool.ntp.org

25 Apr 14:33:51 ntpdate[8310]: step time server 80.85.129.2 offset 6.655976 sec

然后,启动ntpd服务:

[root@linux ~] # service ntpd start

或 [root@linux ~] # /etc/init.d/ntpd start

查看端口:

[root@linux ~] # netstat -ln|grep 123

udp        0      0 192.168.228.153:123        0.0.0.0:*

udp        0      0 127.0.0.1:123               0.0.0.0:*

udp        0      0 0.0.0.0:123                  0.0.0.0:*

udp        0      0 :::123                       :::*

如何确认我们的NTP服务器已经更新了自己的时间呢?

[root@linux ~] # ntpstat

synchronized to NTP server(127.127.1.0) at stratum 11

time correct to within 950ms

polling server every 64 s

#改指令可列出NTP服务器是否与上层联机。由上述输出结果可知,时间校正约

#为950*10(-6)秒。且每隔64秒会主动更新时间。

常见的错误:

25 Apr 15:30:17 ntpdate[11520]: no server suitable for synchronization found

其实,这不是一个错误。而是由于每次重启NTP服务器之后大约要3-5分钟客户端才能与server建立正常的通讯连接。当此时用客户端连接服务端就会报这样的信息。一般等待几分钟就可以了。

[root@linux ~] # ntptrace –n 127.0.0.1

127.0.0.1:stratum 11, offset 0.000000,synch distance 0.950951

222.73.214.125:stratum 2,offset –0.000787,synch distance 0.108575

209.81.9.7:stratum 1,offset 0.000028,synch distance 0.00436,refid ‘GPS’

#这个指令可以列出目前NTP服务器(第一层)与上层NTP服务器(第二层)彼此之间的

#关系

[root@linux ~] # ntpq –p

 

指令“ntpq -p”可以列出目前我们的NTP与相关的上层NTP的状态,以上的几个字段的意义如下:

remote:即NTP主机的IP或主机名称。注意最左边的符号,如果由“+”则代表目前正在作用钟的上层NTP,如果是“*”则表示也有连上线,不过是作为次要联机的NTP主机。

refid:参考的上一层NTP主机的地址

st:即stratum阶层

when:几秒前曾做过时间同步更新的操作

poll:下次更新在几秒之后

reach:已经向上层NTP服务器要求更新的次数

delay:网络传输过程钟延迟的时间

offset:时间补偿的结果

jitter:Linux系统时间与BIOS硬件时间的差异时间

 

    最后提及一点,ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件。

在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

转自:http://blog.sina.com.cn/s/blog_636a55070101u1mg.html

 

其它:

NTP的配置

 

 

 

A: 配置/etc/ntp.conf

 

NTP Server的主要配置文件为/etc/ntp.conf ,没有修改过的ntp.conf文件内容如下所示,配置选项都有相关注释信息(Linux 版本为Red Hat Enterprise Linux Server release 6.6 )

[root@localhost ~]# more /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 kod nomodify notrap nopeer noquery
restrict -6 default kod 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 -6 ::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.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst
 
 
#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

各个选项信息:

#系统时间与BIOS事件的偏差记录

driftfile /etc/ntp/drift

 

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地址的权限设置。

1:设定NTP主机来源(其中prefer表示优先主机),192.168.7.49是本地的NTP服务器,所以优先指定从该主机同步时间。

server 192.168.7.49 prefer 
 
server 0.rhel.pool.ntp.org iburst
 
server 1.rhel.pool.ntp.org iburst
 
server 2.rhel.pool.ntp.org iburst
 
server 3.rhel.pool.ntp.org iburst

clip_image001

 

2:限制你允许的这些服务器的访问类型,在这个例子中的服务器是不容许修改运行时配置或查询您的Linux NTP服务器

 

restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap

在上例中,掩码地址扩展为255,因此从192.168.0.1-192.168.0.254的服务器都可以使用我们的NTP服务器来同步时间

 

#此时表示限制向从192.168.0.1-192.168.0.254这些IP段的服务器提供NTP服务。

restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap noquery

 

#设置默认策略为允许任何主机进行时间同步

restrict default ignore

 

3:确保localhost(这个常用的IP地址用来指Linux服务器本身)有足够权限.使用没有任何限制关键词的语法:

restrict 127.0.0.1

restrict -6 ::1

 

B:配置/etc/ntp/stpe-tickers文件

修改/etc/ntp/stpe-tickers文件,内容如下(当ntpd服务启动时,会自动与该文件中记录的上层NTP服务进行时间校对)

[root@localhost ntp]# more /etc/ntp/step-tickers 
# List of servers used for initial synchronization.
[root@localhost ntp]# vi /etc/ntp/step-tickers 
# List of servers used for initial synchronization.
server 192.168.7.49 prefer
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org 
server 2.rhel.pool.ntp.org 
server 3.rhel.pool.ntp.org 

关于ntp.conf and step-tickers区别:

step-tickers is used by ntpdate where as ntp.conf is the configuration file for the ntpd daemon. ntpdate is initially run to set the clock before ntpd to make sure time is within 1000 sec. ntp will not run if the time difference between the server and client by more then 1000 sec ( or there about). The start up script will read step-tickers for servers to be polled by ntpdate.

C:配置/etc/sysconfig/ntpd文件

ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

#允许BIOS与系统时间同步,也可以通过hwclock -w 命令

SYNC_HWCLOCK=yes

 

IPTABLES 配置

由于NTP服务需要使用到UDP端口号123,所以当系统的防火墙(Iptables)启动的情况下,必须开放UDP端口号123。

[root@localhost ~]#  /etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
5    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
 
[root@localhost ~]# /sbin/iptables -I INPUT -p udp --dport 123 -j ACCEPT
[root@localhost ~]#  /etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:123 
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
3    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
6    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
 
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
 
[root@localhost ~]# 

clip_image002

如果防火墙没有开放UDP端口号123,有可能出现下面情况。

[root@localhost ~]# /usr/sbin/ntpq -c rv | grep stratum

stratum=16, precision=-24, rootdelay=0.000, rootdisp=3.525, refid=INIT,

[root@localhost~]#

A stratum level of 16 indicates that NTP is not synchronizing correctly.If a stratum level of 16 is detected, wait 15 minutes and issue the command again. It may take this long for the NTP server to stabilize.If NTP continues to detect a stratum level of 16, verify that the NTP port (UDP Port 123) is open on all firewalls between the cluster and the remote machine you are attempting to synchronize to.

 

 

启动NTP服务

 

 

[root@localhost ~]# service ntpd status
ntpd is stopped
[root@localhost ~]# service ntpd start
Starting ntpd: [  OK  ]
[root@localhost ~]# 
 
service ntpd status      #查看ntpd服务状态
service ntpd start           #启动ntpd服务
service ntpd stop            #停止ntpd服务
service ntpd restart         #重启ntpd服务

检查ntp服务是否开机启动,将其设置为开机启动。

[root@localhost ~]# chkconfig --list ntpd

ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

[root@localhost ~]# runlevel

N 3

[root@localhost ~]# chkconfig ntpd on #在运行级别2、3、4、5上设置为自动运行

[root@localhost ~]# chkconfig --list ntpd

ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@localhost ~]#

如果要设置在运行级别上自动运行,可以使用下面命令

chkconfig --level 345 ntpd on

可以用下面命令检测NTP服务是否运行

[root@localhost ~]# pgrep ntpd 
2639
2641
[root@localhost ~]# netstat -tlunp | grep ntp   #如果看到123端口,说明ntp服务成功启动。
udp        0      0 192.168.7.224:123           0.0.0.0:*                               2639/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               2639/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               2639/ntpd           
udp        0      0 fe80::250:56ff:feb3:b5:123  :::*                                    2639/ntpd           
udp        0      0 ::1:123                     :::*                                    2639/ntpd           
udp        0      0 :::123                      :::*                                    2639/ntpd           
[root@localhost ~]# 

clip_image003

查看ntp服务器有无和上层ntp连通

 

[root@localhost ~]# ntpstat
synchronised to NTP server (192.168.7.49) at stratum 6 
   time correct to within 440 ms
   polling server every 128 s
[root@localhost ~]# 

查看ntp服务器与上层ntp的状态

[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.7.49    192.168.7.50     5 u   13   64    3    5.853  1137178   2.696
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.7.49    192.168.7.50     5 u   17   64    3    5.853  1137178   2.696
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.7.49    192.168.7.50     5 u    1   64    1    0.937   -9.570   0.000

clip_image004

remote   - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先

refid    - 参考上一层ntp主机地址

st       - stratum阶层

when     - 多少秒前曾经同步过时间

poll     - 下次更新在多少秒后

reach    - 已经向上层ntp服务器要求更新的次数

delay    - 网络延迟

offset   - 时间补偿

jitter   - 系统时间与bios时间差

要查看 ntpd 进程的状态,请运行以下命令,按 Ctrl+C 停止查看进程。

clip_image005

第一列中的字符指示源的质量。星号 ( * ) 表示该源是当前引用。

remote 列出源的 IP 地址或主机名。

when   指出从轮询源开始已过去的时间(秒)。

poll   指出轮询间隔时间。该值会根据本地时钟的精度相应增加。

reach  是一个八进制数字,指出源的可存取性。值 377 表示源已应答了前八个连续轮询。

offset 是源时钟与本地时钟的时间差(毫秒)。

 

ntpd、ntpdate的区别

下面是网上关于ntpd与ntpdate区别的相关资料。如下所示所示:

使用之前得弄清楚一个问题,ntpd与ntpdate在更新时间时有什么区别。ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。

时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题:

第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。

第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。

第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。

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

 
posted @ 2019-09-04 11:24  嘻嘻哈哈的人生  阅读(5762)  评论(0编辑  收藏  举报