NTP服务器搭建

当搭建集群的时候,最好就要服务器之间的时间是同步的,于是就动手试了一下。

时间服务器作用:

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

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

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

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

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

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

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

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

选用了2台虚拟机,一个当服务端(192.168.180.137),一个当客户端(192.168.180.105)

[root@localhost ~]# rpm -qa |grep ntp
python-ntplib-0.3.2-1.el7.noarch
ntpdate-4.2.6p5-29.el7.centos.x86_64
fontpackages-filesystem-1.44-8.el7.noarch
ntp-4.2.6p5-29.el7.centos.x86_64
[root@localhost ~]# yum -y remove ntp-4.2.6p5-29.el7.centos.x86_64
已加载插件:fastestmirror, langpacks
正在解决依赖关系
--> 正在检查事务
[root@localhost ~]# yum remove -y ntpdate-4.2.6p5-29.el7.centos.x86_64
已加载插件:fastestmirror, langpacks
正在解决依赖关系
--> 正在检查事务
---> 软件包 ntpdate.x86_64.0.4.2.6p5-29.el7.centos 将被 删除
--> 解决依赖关系完成

依赖关系解决

首先把已经安装的ntp服务给卸载掉,然后开始重新安装ntp

[root@localhost ~]# yum -y install ntp
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 ntp.x86_64.0.4.2.6p5-29.el7.centos 将被 安装
--> 正在处理依赖关系 ntpdate = 4.2.6p5-29.el7.centos,它被软件包 ntp-4.2.6p5-29.el7.centos.x86_64 需要
--> 正在检查事务
---> 软件包 ntpdate.x86_64.0.4.2.6p5-29.el7.centos 将被 安装
--> 解决依赖关系完成

依赖关系解决

=====================================================================================================================================================================
 Package                              架构                                版本                                               源                                 大小
=====================================================================================================================================================================
正在安装:
 ntp                                  x86_64                              4.2.6p5-29.el7.centos                              base                              548 k
为依赖而安装:
 ntpdate                              x86_64                              4.2.6p5-29.el7.centos                              base                               86 k

事务概要
=====================================================================================================================================================================
安装  1 软件包 (+1 依赖软件包)

总下载量:635 k
安装大小:1.5 M
Downloading packages:
(1/2): ntpdate-4.2.6p5-29.el7.centos.x86_64.rpm                                                                                               |  86 kB  00:00:00
(2/2): ntp-4.2.6p5-29.el7.centos.x86_64.rpm                                                                                                   | 548 kB  00:00:00
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                 1.5 MB/s | 635 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : ntpdate-4.2.6p5-29.el7.centos.x86_64                                                                                                             1/2
  正在安装    : ntp-4.2.6p5-29.el7.centos.x86_64                                                                                                                 2/2
  验证中      : ntp-4.2.6p5-29.el7.centos.x86_64                                                                                                                 1/2
  验证中      : ntpdate-4.2.6p5-29.el7.centos.x86_64                                                                                                             2/2

已安装:
  ntp.x86_64 0:4.2.6p5-29.el7.centos

作为依赖被安装:
  ntpdate.x86_64 0:4.2.6p5-29.el7.centos

完毕!

注:服务端和客户端都进行一样的安装

 服务端:

[root@localhost ~]# vim  /etc/ntp.conf

修改

restrict 192.168.180.137 nomodify notrap nopeer noquery
restrict 192.168.180.2 mask 255.255.255.0 nomodify notrap

 

 

 

 

# 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 127.127.1.0
Fudge 127.127.1.0 stratum 10

 

注:把之前的server都注释掉

客户端:

restrict 192.168.180.105 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.180.2 mask 255.255.255.0 nomodify notrap

 

 

server 192.168.180.137
Fudge 192.168.180.137 stratum 10

 

启动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:时间补偿

(四)设置开机启动

【命令】chkconfig ntpd on

测试:

将客户的时间改错,然后启动ntp

[root@topcheer ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.180.137 LOCAL(0)         6 u    2   64   17    2.057  2369747   0.378
[root@topcheer ~]# date
2020年 01月 30日 星期四 13:16:08 CST
[root@topcheer ~]# ntpstat
unsynchronised
  time server re-starting
   polling server every 8 s
[root@topcheer ~]# ntpstat
unsynchronised
   polling server every 64 s
[root@topcheer ~]# date
2020年 01月 30日 星期四 19:56:31 CST
[root@topcheer ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.180.137 LOCAL(0)         6 u    2   64   37    2.017    0.543   0.585

 

服务端的时间:

[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.           5 l    1   64   17    0.000    0.000   0.000
[root@localhost ~]# ntpstat
synchronised to local net (127.127.1.0) at stratum 6
   time correct to within 449 ms
   polling server every 64 s

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

===/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掩码 ]  [参数]

restrict 控制相关权限

ignore  :关闭所有的 NTP 联机服务

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

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

noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器

notrap :,不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。

nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟

kod : 访问违规时发送 KoD 包。

restrict -6 表示IPV6地址的权限设置。

restrict default kod nomodify notrap nopeer noquery  #首先我们对于默认的client拒绝所有的操作

restrict 127.0.0.1    #允许本机地址的一切操作

restrict 192.168.180.0 mask 255.255.255.0 nomodify notrap  #在该子网里面的客户端可以进行网络校时,但是不能修改NTP时间服务器的时间参数,不提供trap远端登陆

server 127.127.1.0    #新增上级时间服务器为本机
fudge 127.127.1.0 stratum 10  #设置时间服务器的层次,

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

 

posted @ 2020-01-30 20:18  天宇轩-王  阅读(701)  评论(0编辑  收藏  举报