004.NTP多层级架设

一 环境需求

1.1 需求

User-client:局域网所有节点主机;
IN-NTP Server:隐藏于局域网内部的NTP服务器;
Border-NTP:边界NTP服务器,用于同步外部时钟,同时对内部IN-NTP服务器提供校对;
亚洲授时中心:公网NTP授时中心。
内部局域网所有节点client需要和IN-NTP Server同步,IN-NTP Server对外隐藏,同时通过 Border-NTP进行同步, Border-NTP边界NTP服务器实时同步公网授时中心。

1.2 架构

08
主机名
IP
备注
border_ntp
内网IP:172.24.8.71
边界NTP服务器
in_ntp01
172.24.8.72
内部NTP服务器01
in_ntp02
172.24.8.73
内部NTP服务器02
client
172.24.8.74
模拟局域网内部客户端节点
注意:
  • Border-NTP部署在边界上,作为和互联网(亚洲授时中心)同步,同时为局域网内部IN-NTP提供同步服务。
  • User-client,包括所有局域网内客户端等,从两台(一主一备)IN-NTP进行同步。

二 Border-NTP配置

2.1 服务包安装

  1 [root@border_ntp ~]# rpm -qa | grep ntp
  2 [root@border_ntp ~]# yum -y install ntp			#安装ntp服务
 

2.2 ntp配置

  1 [root@border_ntp ~]# vi /etc/ntp.conf
  2 driftfile /var/lib/ntp/drift
  3 #定义与上级时间服务器联系时所花费的时间,记录在driftfile参数后面的文件内。
  4 driftfile   /var/lib/ntp/drift
  5 pidfile     /var/run/ntpd.pid				#进程pid文件
  6 logfile     /var/log/ntp.log				#开启日志记录
  7 #定义与上级时间服务器联系时所花费的时间,记录在driftfile参数后面的文件内。
  8 
  9 restrict default nomodify notrap nopeer noquery	        #默认拒绝所有NTP连接
 10 restrict 127.0.0.1
 11 restrict ::1 						#开启本地授权
 12 
 13 restrict 172.24.8.72 mask 255.255.255.255 nomodify notrap	#添加IN-NTP Server允许校时
 14 restrict 172.24.8.73 mask 255.255.255.255 nomodify notrap	#添加IN-NTP Server允许校时
 15 
 16 #server 0.cn.pool.ntp.org
 17 #server 1.asia.pool.ntp.org
 18 #restrict 0.cn.pool.ntp.org nomodify notrap noquery
 19 #restrict 1.asia.pool.ntp.org nomodify notrap noquery
 20 #添加Border-NTP的上一级时间中心,即亚洲授时中心服务器,此处建议注释,转而采用国内阿里云时钟,相对网络更稳定,如下。
 21 server ntp1.aliyun.com iburst minpoll 4 maxpoll 10
 22 restrict ntp1.aliyun.com nomodify notrap nopeer noquery
 23 server ntp2.aliyun.com iburst minpoll 4 maxpoll 10
 24 restrict ntp2.aliyun.com nomodify notrap nopeer noquery
 25 #允许阿里授时中心修改本地时间。
 26 server 127.127.1.0
 27 fudge 127.127.1.0 stratum 4
 28 #当授时不可用,采用本地时间,stratum级别为4。
 

2.3 其他配置

  1 [root@border_ntp ~]# chown ntp:ntp /var/log/ntp.log
  2 [root@border_ntp ~]# chcon -t ntpd_log_t /var/log/ntp.log
  3 [root@border_ntp ~]# firewall-cmd --permanent --add-service=ntp
  4 [root@border_ntp ~]# firewall-cmd --reload
  5 [root@border_ntp ~]# setenforce 0
 
提示:若开启日志功能,需要ntp进程具备写入日志权限。同时防火墙开放ntp服务,SELinux写入上下文授权。
建议:若没有特殊必要,建议关闭SELinux和防火墙。

2.4 开启服务

  1 [root@border_ntp ~]# systemctl start ntpd.service
  2 [root@border_ntp ~]# systemctl enable ntpd.service
 
提示:开启ntp server后,需要等待一定时间(通常为5分钟以内),才能实现和亚洲授时中心的同步。

2.5 验证

  1 [root@border_ntp ~]# ntpq -np
09
提示:ntpq相关含义见附录一。

三 IN-NTP Server配置

3.1 服务包安装

  1 [root@in_ntp01 ~]# rpm -qa | grep ntp			#查看是否安装ntp
  2 [root@in_ntp01 ~]# yum -y install ntp			#安装ntp服务
 

3.2 ntp配置

  1 [root@in_ntp01 ~]# vi /etc/ntp.conf
  2 driftfile   /var/lib/ntp/drift
  3 pidfile     /var/run/ntpd.pid				#进程pid文件
  4 logfile     /var/log/ntp.log				#开启日志记录
  5 #定义与上级时间服务器联系时所花费的时间,记录在driftfile参数后面的文件内。
  6 tinker stepout 0
  7 restrict    default ignore				        #默认拒绝所有连接
  8 restrict -6 default ignore				        #默认拒绝所有ipv6连接
  9 restrict    127.0.0.1
 10 restrict    -6 ::1					        #开启本地授权
 11 
 12 restrict 172.24.8.0 mask 255.255.255.0 nomodify notrap	#允许局域网内网段校时
 13 
 14 restirct 172.24.8.71 nomodify notrap nopeer noquery
 15 #允许根据上一级时间修改本地时间
 16 server 172.24.8.71  iburst minpoll 3 maxpoll 3 prefer
 17 #添加上一层Border-NTP服务地址
 18 
 19 #peer 172.24.8.72 iburst minpoll 4 maxpoll 6
 20 #peer 172.24.8.73 iburst minpoll 4 maxpoll 6
 21 #服务器接收其他服务器的地址,同时也会为其他设备提供NTP服务器,即互相同步,用于在孤岛环境中,若同时具备上层时钟,可注释掉。
 22 
 23 server 127.127.1.0
 24 fudge 127.127.1.0 stratum 8			#上一级不可用,使用本地时间,stratum为8
 

3.3 其他配置

参考2.3.

3.4 开启服务

  1 [root@in_ntp01 ~]# systemctl start ntpd
  2 [root@in_ntp01 ~]# systemctl enable ntpd
 

3.5 验证

  1 [root@in_ntp01 ~]# ntpq -np
10
注意:两台IN-NTP Server都需要配置。

四 user-client配置

4.1 服务包安装

  1 [root@client ~]# rpm -qa | grep ntp			#查看是否安装ntp
  2 [root@client ~]# yum -y install ntp			#安装ntp服务
 

4.2 ntp配置

  1 [root@client ~]# vi /etc/ntp.conf
  2 driftfile   /var/lib/ntp/drift
  3 pidfile     /var/run/ntpd.pid				#进程pid文件
  4 logfile     /var/log/ntp.log				#开启日志记录
  5 #定义与上级时间服务器联系时所花费的时间,记录在driftfile参数后面的文件内。
  6 
  7 tinker panic 600
  8 #当client和server时差在600秒以内,使用渐变调整client时间,超过600秒,ntpd进程自动终止,需要手动进行调整。
  9 restrict    default ignore				        #默认拒绝所有连接
 10 restrict -6 default ignore				        #默认拒绝所有ipv6连接
 11 restrict    127.0.0.1					#开启本地授权
 12 
 13 restrict 172.24.8.72 nomodify notrap noquery		#允许根据上一级时间修改本地时间
 14 restrict 172.24.8.73 nomodify notrap noquery		#允许根据上一级时间修改本地时间
 15 
 16 server 172.24.8.72 iburst minpoll 4 maxpoll 6 prefer
 17 server 172.24.8.73 iburst minpoll 4 maxpoll 6
 

4.3 其他配置

参考2.3.

4.4 开启服务

  1 [root@client ~]# systemctl start ntpd
  2 [root@client ~]# systemctl enable ntpd
 

4.5 验证

  1 [root@client ~]# ntpq -np
11
  1 [root@client ~]# ntpstat
12

附录一

ntpq参数解释:
remote:本地服务器所连接的远程NTP服务器。
refid:NTP服务器使用的上一级ntp服务器,即给远程ntp服务器提供时间同步是服务器。。
st :remote远程服务器的级别,由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端。所以服务器从高到低级别可以设定为1-16,为了减
缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的。
when: 上一次成功请求之后到现在的秒数。
poll : 本地机和远程服务器多少时间进行一次同步(单位为秒),在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快调
整到正确的时间范围,之后poll值会逐渐增大,同步的频率也就会相应减小。
reach:这是一个八进制值,用来测试能否和服务器连接,每成功连接一次它的值就会增加
delay:从本地机发送同步要求到ntp服务器的round trip time
offset:主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒(ms)。offset越接近于0,主机和ntp服务器的时间越接近。
jitter:这是一个用来做统计的值,它统计了在特定个连续的连接数里offset的分布情况,简单地说这个数值的绝对值越小,主机的时间就越精确。
『 * 』:代表目前正在作用当中的NTP,即主NTP Server;
『 + 』:代表辅助的NTP Server和带有*号的服务器一起为我们提供同步服务, 当主NTP Server服务器不可用时备服务器接管,即作为下一个提供时间更新的候
选者。
『 - 』:远程服务器被认为是不合格的NTP Server。
『 x 』:远程服务器不可用。

附录二

ntp服务,默认只会同步系统时间。若需要ntp同时同步硬件时间,可以在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes ,则可以让硬件时间与系统时间一起同步。
SYNC_HWCLOCK=yes
注意:允许BIOS与系统时间同步,也可以通过hwclock -w 命令。

猜你喜欢

转载自www.cnblogs.com/itzgr/p/10342262.html