1、概述:

名字:ntp服务

功能:实战配置NTP服务器为内网服务器同步时间。 配置NTP客户端同步时间.

原理:NTP ( Network Time Protocol ,网络时间协议)是用来使计算机时间同步化的一种协议。它可以使计算机对其服务器或时钟源做同步化,它可以提供高精准度的时间校正( LAN上与标准间差小于1毫秒,WAN,上几十毫秒) . 且可介由加密确认的方式来防止恶意的协议攻击。

模式:C/S 

时钟服务器可以通过连接上层,上层可以连接上层,最大不能超过16层

centos客户端手动设置NTP服务器地址 centos ntp服务_服务器

ntp 端口号:123

扩展:nntp  端口号:119,跟ntp无任何关系
NNTP ( Network News Transport Protocol ),中文释义:( RFC-977 )网络新闻传输协议。这是- -个主要用于阅读和发布新闻文章(俗称为“帖子”, 比较正式的名字是“新闻组邮件” )到Usenet. 上的Internet应用协议,也负责新闻在服务器间的传送。

 

2、两种模式:

    2.1 一台服务端(安装ntp),多台客户端(安装ntpdate)

          好处是:客户端执行ntpdate 可以立即更新时间,缺点:只能通过crontab定时任务来做ntpdate更新时间。

   2.2  多台服务端(安装ntp),选中其中一台作为主服务端,不需要安装ntpdate

         好处是:可以自动在一段时间内,自动更新主服务端时间,缺点:不能立即更新时间。

 

3、 模式1:一台服务端(安装ntp),多台客户端(安装ntpdate) 部署安装:

实现1:搭建一个内网NTP服务器,ntp服务器不可以访问外网ntp服务器同步时间,让内网其他服务器通过此NTP服务器进行时间同步。

NTP 服务端  hdss7-11.host.com      IP: 10.4.7.11 
NTP 客户端  hdss7-200.host.com    IP: 10.4.7.11 

软件包 ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm 是服务端
软件包 ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm 是客户端

服务端:hdss7-11.host.com :

安装:[root@hdss7-11 ~]# yum install ntp  -y
启动:[root@hdss7-11 ~]# systemctl start ntpd  ,systemctl enable ntpd
查看:[root@hdss7-11 ~]# netstat -tulpn |grep ntpd
udp        0      0 10.4.7.11:123                0.0.0.0:*                           1944/ntpd           
udp        0      0 127.0.0.1:123                0.0.0.0:*                           1944/ntpd           
udp        0      0 0.0.0.0:123                    0.0.0.0:*                           1944/ntpd           
udp6      0      0 fe80::20c:29ff:feef:123   :::*                                   1944/ntpd           
udp6      0      0 ::1:123                            :::*                                   1944/ntpd           
udp6      0      0 :::123                              :::*                                   1944/ntpd           

配置文件:
[root@hdss7-11~]# ls /etc/ntp.conf 
 /etc/ntp.conf

1、restrict
作用:对ntp做权限控制
ignore:忽略所有类型的NTP连接请求
nomodify:限制客户端不能使用命令ntpc和ntpq来修改服务器端的时间
noquery:不提供NTP网络校时服务
notrap:不接受远程登录请求
notrust:不接受没有经过认证的客户端的请求
【如果没有用任何参数,那么表示不做任何限制】
例子:restrict 10.4.7.0  mask 255.255.255.0 nomodify <<<允许10.4.7.0/24/24 网段所有主机进行时间同步
例子:restrict 133.100.11.8  #允许133.100.11.8使用这个时间服务器

2、server
作用:指定ntp服务器的地址
格式:server [ip or hostname] [perfer]。
例子:server 127.127.1.0 <<<将当前主机作为时间服务器
其中注意:按照顺序同步,同步后不会同步下一个,直至上一个同步不了
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
测试:
[root@hdss7-11 ~]# ntpdate 0.centos.pool.ntp.org
23 Sep 12:57:37 ntpdate[61385]: the NTP socket is in use, exiting    报错
解决方案:由于客户端,也安装了ntp 服务器,停止

3、fudge
作用:设置时间服务器的层级
格式:fudge ip [startnum int]
例子:fudge 127.127.1.0 stratum 10  #设置服务器层级为第10层。ntp 和127.127.1.0同步完后,就变成了11层。
ntp是层次阶级的。同步上层服务器的stratum大小不能超过或等于16。
注意:fudge必须和server一块用, 而且是在server的下一行
startnum
0~15
0:表示顶级
10:通常用于给局域网主机提供时间服务

4、

driftfile /var/lib/ntp/                  #记录当前时间服务器,与上游服务器的时间差的文件
      logfile /var/log/ntp/ntp.log        #指定日志文件位置,需要手动创建[root@hdss7-11 ~]# vi /etc/ntp.conf 
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
restrict 10.4.7.0 mask 255.255.255.0

# 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 127.127.1.0
#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
fudge 127.127.1.0 stratum 10
在主节点:ntpstat 查看上游连接,表示本ntp服务器跟谁同步时间
 [root@hdss7-11 ~]# ntpstat
 synchronised to local net (127.127.1.0) at stratum 11
 time correct to within 7948 ms
 polling server every 64 s                                #64秒主动更新一下时间NTP 客户端  hdss7-200.host.com    IP: 10.4.7.11 
[root@hdss7-200 ~]#  yum install ntpdate  -y
用法:netdate IP或者ntp 服务器域名
 [root@hdss7-200 ~]# ntpdate 10.4.7.11  
 23 Sep 16:00:18 ntpdate[109053]: adjust time server 10.4.7.11 offset -0.043108 sec定时任务:
查看ntpdate命令的实际路径
 [root@localhost ~]# which ntpdate    
 /usr/sbin/ntpdate

使用定时任务,可以设置每天凌晨定时同步一次Ntp服务器

[root@localhost ~]# crontab -e  下加入
 00 00 * * *  /usr/sbin/ntpdate -u 192.168.19.103

实现2:搭建一个内网NTP服务器,ntp服务器可以访问外网ntp服务器同步时间,让内网其他服务器通过此NTP服务器进行时间同步。

[root@hdss7-11 ~]# vi /etc/ntp.conf 
# the administrative functions.
restrict 127.0.0.1       
restrict ::1
restrict 10.4.7.0 mask 255.255.255.0

# 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 127.127.1.0
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
fudge 127.127.1.0 stratum 10
[root@hdss7-11 ~]# ntpq -p         指令“ntpq -p”可以列出目前我们的NTP与相关的上层NTP的状态
  remote                  refid                  st   t   when poll    reach   delay   offset  jitter
 ==============================================================================
  LOCAL(0)            .LOCL.               9   l   11        64       1       0.000    0.000   0.000
 *ntp8.flashdance 192.36.143.150   2   u    -        64       1       328.001  45855.0   5.719
 +ntp.xtom.nl        194.80.204.184   2   u    1        64       1      198.469  45865.7   0.331
  electrode.felix     56.1.129.236      3   u    4        64       1       244.533  45892.1   0.753
  time.cloudflare    10.12.3.34          3   u    3         64       1      227.966  45844.7   5.656


+ :代表优先   *次级
remote :本机连接上层ntp 的IP或者主机名
refid      :本机连接上层ntp 的IP
st   :层数
t :未知其含义。
when:  上一次成功请求之后到现在的秒数。
poll :下次更新在多少秒之后
reach: 这是一个八进制值,用来测试能否和服务器连接.每成功连接一次它的值就会增加
delay: 从本地机发送同步要求到ntp服务器的round trip time
offset:  时间补偿误差
jitter: 这是一个用来做统计的值. 它统计了在特定个连续的连接数里offset的分布情况. 简单地说这个数值的绝对值越小,主机的时间就越精确

排错:

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

 

4、 模式2: 多台服务端(安装ntp), 选中其中一台作为主服务端,部署安装:

服务端:hdss7-11.host.com     IP: 10.4.7.11

安装:[root@hdss7-11 ~]# yum install ntp  -y
启动:[root@hdss7-11 ~]# systemctl start ntpd  ,systemctl enable ntpd
硬件时间、软件时间:[[root@hdss7-11 ~]# hwclock -w ,hwclock -r

[root@hdss7-11 ~]# vi /etc/ntp.conf 
# 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 10.4.7.11 nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1

# Hosts on local network are less restricted.
restrict 10.4.7.2 mask 255.255.255.0 nomodify notrap
restrict 10.4.7.200

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 127.127.1.0
Fudge 127.127.1.0 stratum 10

#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client

重启动:

[root@hdss7-11 ~]# systemctl stop ntpd  ,systemctl start ntpd
[root@hdss7-11 ~]# ntpstat                  查看ntp服务器有无和上层ntp连通【命令】ntpstat
 synchronised to local net (127.127.1.0) at stratum 6
    time correct to within 11 ms
    polling server every 64 s[root@hdss7-11 ~]# ntpq -p
      remote           refid      st t when poll reach   delay   offset  jitter
 ==============================================================================
 *LOCAL(0)        .LOCL.           5 l   52   64  377    0.000    0.000   0.000



服务端 hdss7-200.host.com    IP: 10.4.7.200

 

安装:[root@hdss7-11 ~]# yum install ntp  -y
启动:[root@hdss7-11 ~]# systemctl start ntpd  ,systemctl enable ntpd
硬件时间、软件时间:[[root@hdss7-11 ~]# hwclock -w ,hwclock -r

[root@hdss7-11 ~]# vi /etc/ntp.conf 
# 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 10.4.7.200 nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1

# Hosts on local network are less restricted.
restrict 10.4.7.2 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 10.4.7.11
fudge 10.4.7.11 stratum 10

#broadcast 192.168.1.255 autokey        # broadcast server

重启动:[root@hdss7-11 ~]# systemctl stop ntpd  ,systemctl start ntpd

[root@hdss7-200 ~]#  ntpstat     查看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命令查看状态,就会变成如下正常结果:

[root@hdss7-200 ~]#  ntpstat   
 synchronised to NTP server (10.4.7.11) at stratum 7
    time correct to within 20 ms
    polling server every 128 s[root@hdss7-200 ~]# ntpq -p
      remote           refid      st t when poll reach   delay   offset  jitter
 ==============================================================================
 *10.4.7.11       LOCAL(0)         6 u   60  128  377    0.360   -0.034   0.111


测试:

服务端:hdss7-11.host.com     IP: 10.4.7.11  修改date
服务端: hdss7-200.host.com    IP: 10.4.7.200  等待不止64s,10min