NTP时间同步架构

一、架构

二、NTP服务端安装和配置(192.168.31.223)

yum install ntp
vim /etc/ntp.conf

1.打开配置文件,改动下面几个地方:

# 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).

# @1系统时间和BOIS时间的偏差记录
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.
# @2控制相关权限
restrict 127.0.0.1
restrict -6 ::1

# @3新增-权限配置
restrict 127.127.1.0
restrict 192.168.31.0 mask 255.255.255.0 nomodify notrap


# 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).
# @3改动-注释掉上级时间服务器地址
#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

# @4新增-上级时间服务器
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

解释:

@1:

2.启动时间服务端

[root@localhost script]# service ntpd start
正在启动 ntpd:                                            [确定]

3.查看时间服务状态

[root@localhost script]# service ntpd status
ntpd (pid  24577) 正在运行...

4.设置开机启动

chkconfig ntpd on

5.查看NTP是否正常运行

[root@localhost script]# netstat -tlunp | grep ntp
udp        0      0 10.22.9.58:123              0.0.0.0:*                               24361/ntpd          
udp        0      0 10.10.11.247:123            0.0.0.0:*                               24361/ntpd          
udp        0      0 127.0.0.1:123               0.0.0.0:*                               24361/ntpd          
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               24361/ntpd          
udp        0      0 fe80::20c:29ff:fea3:a3d3:123 :::*                                    24361/ntpd          
udp        0      0 ::1:123                     :::*                                    24361/ntpd          
udp        0      0 fe80::20c:29ff:fea3:a3c9:123 :::*                                    24361/ntpd          
udp        0      0 :::123                      :::*                                    24361/ntpd 

如果看到123端口,说明ntp服务成功启动。

6.查看ntp服务器有无和上层ntp连通,根据需求和设计这台服务器的上层只是本地系统时间

[root@localhost script]# ntpstat
synchronised to local net at stratum 11 
   time correct to within 12 ms
   polling server every 64 s

这句话的意思是在449毫秒内已经从本地系统时间(因为我们配置的上层是系统时间而不是网上的时间服务器)进行了一次同步,并且每隔64秒会同步一次。

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

[root@localhost script]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.          10 l   38   64  377    0.000    0.000   0.000

remote   - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
refid    - 参考上一层ntp主机地址
st       - stratum阶层
when     - 多少秒前曾经同步过时间
poll     - 下次更新在多少秒后
reach    - 已经向上层ntp服务器要求更新的次数
delay    - 网络延迟
offset   - 时间补偿
jitter   - 系统时间与bios时间差

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

watch "ntpq -p"
Every 2.0s: ntpq -p                                                                                                                                                                       Mon Jul 10 10:38:40 2017

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

9.永远不要忘了,防火墙配置

[root@localhost script]# /etc/init.d/iptables status
表格: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    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:20880 
6    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:7002 
7    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:8080 
8    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  

添加123端口

/sbin/iptables -I INPUT -p udp --dport 123 -j ACCEPT

到此时间服务器的服务端已经配置完成了,接下来配置客户端。

 

三、NTP客户端配置(192.168.31.217,192.168.31.216...)

1.同样的安装NTP包,然后打开配置文件

# 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

# @1新增-权限配置
restrict 192.168.31.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

# @2新增-自己的时间服务器地址
server 192.168.31.223 prefer

#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

2.手动同步一次时间

/usr/sbin/ntpdate 192.168.31.223

3.启动服务

[root@localhost script]# service ntpd start
正在启动 ntpd:                                            [确定]

4.观察时间同步状态

watch ntpq -p
[root@localhost hct]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*132.168.31.223    LOCAL(0)        11 u   64  128  377    0.202   73.980 412.834

5.等待时间同步完成

watch ntpstat
Every 2.0s: ntpstat                                                                                                                                                                       Tue Jul 11 16:55:57 2017

synchronised to NTP server (10.10.11.247) at stratum 12
   time correct to within 605 ms
   polling server every 128 s

synchronised 看到这个表示已经同步成功了,如果是unsynchronised则再等等看,一般同步是需要时间的。

 

四、注意:

1. 手动同步时间的时候必须要先停掉NTP

2. 如果主从服务时间超过1000秒则不再进行同步了,这时候要手动同步,即:/usr/sbin/ntpdate命令,如果怕服务器时差会经常变动比较大可以再Linux中添加计划任务,例如:

10 5 * * * root /usr/sbin/ntpdate 192.168.31.223 && /sbin/hwclock -w

/sbin/hwclock -w  的意思是同步BOIS时间

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

SYNC_HWCLOCK=yes

 

五、其他

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)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。

 

六、异常

1. the NTP socket is in use, exiting

在执行手动同步时间的时候要先关闭时间服务,参考

2. no server suitable for synchronization found

这是因为NTP server还没有和其自身或者它的server同步上,参考

 

参考资料

http://www.cnblogs.com/kerrycode/archive/2015/08/20/4744804.html

https://my.oschina.net/ahanflw/blog/286210

http://blog.csdn.net/yelang_110/article/details/38292895

http://blog.csdn.net/suyishuai/article/details/30221855

http://acooly.iteye.com/blog/1993484

https://my.oschina.net/lwhmdj0823/blog/753076

 

此技能大家是否get到呢?

转载于:https://my.oschina.net/dolphinboy/blog/1217723

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值