NTP时间服务器配置与解析

NTP时间服务器配置与解析

 

Edit By ZhenXing_Yu

 

目 录

 

编译安装ntp server 2

修改ntp.conf配置文件 2

配置时间同步客户机 2

在服务端验证: 3

在客户端进行同步验证: 3

启动、重启启动与关闭NTP 4

第一种方式 4

第二种方式 4

查看NTP服务: 4

设置开机自启动: 5

NTP的主配置文件/etc/ntp.conf解析 5

NTP服务报错解析: 7

 

 

 

 

 

编译安装ntp server


rpm -qa | grep ntp

Yum install ntp

 

修改ntp.conf配置文件


vi /etc/ntp.conf
①、第一种配置:允许任何IP的客户机都可以进行时间同步
#restrict default kod nomodify notrap nopeer noquery(表示默认拒绝所有IP的时间同步)

restrict default modify notrap

②、第二种配置:只允许192.168.2.0网段的客户机进行时间同步
restrict 192.168.2.0 mask 255.255.255.0 nomodify notrap

配置时间同步客户机

在客户端手工执行 ntpdate <ntp server> 来同步,如:

ntpdate 172.25.25.25


或者利用crontab来执行
crontab -e
0-59/10 * * * * /usr/sbin/ntpdate 192.168.2.10 >> /root/ntpdate.log 2>&1

表示每隔10分钟同步一次时间

 

在服务端验证:

# watch ntpq -p

Every 2.0s: ntpq -p                                                    Tue Dec  7 05:52:55 2010

 

     remote           refid      st t when poll reach   delay   offset  jitter

==============================================================================

*LOCAL(0)        .LOCL.           8 l   22   64  377    0.000    0.000   0.001

服务正常运行

 

 

 

在客户端进行同步验证:

# ntpdate -d 192.168.2.10

 

注:-d表示显示详细信息

Ntpdate指令不能在服务端使用,只能在客户端使用

 

 7 Dec 05:54:42 ntpdate[3627]: ntpdate 4.2.2p1@1.1570-o Thu Nov 26 11:35:07 UTC 2009 (1)

Looking for host 192.168.2.10 and service ntp

host found : 192.168.2.10

transmit(192.168.2.10)

receive(192.168.2.10)

transmit(192.168.2.10)

receive(192.168.2.10)

transmit(192.168.2.10)

receive(192.168.2.10)

transmit(192.168.2.10)

receive(192.168.2.10)

transmit(192.168.2.10)

server 192.168.2.10, port 123

stratum 9, precision -20, leap 00, trust 000

refid [192.168.2.10], delay 0.02632, dispersion 0.00061

transmitted 4, in filter 4

reference time:    d0a890f1.b4d040ff  Tue, Dec  7 2010  5:54:41.706

originate timestamp: d0a89102.09318178  Tue, Dec  7 2010  5:54:58.035

transmit timestamp:  d0a890f2.3c8effe2  Tue, Dec  7 2010  5:54:42.236

filter delay:  0.02910  0.02632  0.02808  0.02641 

         0.00000  0.00000  0.00000  0.00000 

filter offset: 15.80026 15.79880 15.79793 15.79837

         0.000000 0.000000 0.000000 0.000000

delay 0.02632, dispersion 0.00061

offset 15.798801

 

 7 Dec 05:54:42 ntpdate[3627]: step time server 192.168.2.10 offset 15.798801 sec

显示此段表示同步成功,建议多执行几次以保持时间的更精确的同步

Offset表示延迟了15秒。

 

 

启动、重启启动与关闭NTP

 

第一种方式

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

Starting ntpd:                                             [  OK  ]

[root@YuZhenXing ~]# /etc/init.d/ntpd restart

Shutting down ntpd:                                        [  OK  ]

Starting ntpd:                                             [  OK  ]

[root@YuZhenXing ~]# /etc/init.d/ntpd stop

Shutting down ntpd:                                        [  OK  ]

 

第二种方式

[root@YuZhenXing ~]# service ntpd start

Starting ntpd:                                             [  OK  ]

[root@YuZhenXing ~]# service ntpd restart

Shutting down ntpd:                                        [  OK  ]

Starting ntpd:                                             [  OK  ]

[root@YuZhenXing ~]# service ntpd stop

Shutting down ntpd:                                        [  OK  ]

[root@YuZhenXing ~]# 

 

 

查看NTP服务:

NTP服务主要是UDP且在port123端口

[root@X /]# netstat -tlunp | grep ntp

 

设置开机自启动:

[root@YuZhenXing ~]# chkconfig ntpd on

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

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

 

 

 

 

 

 

NTP的主配置文件/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 kod nomodify notrap nopeer noquery 《=====拒绝所有IPv4的用户

restrict -6 default kod nomodify notrap nopeer noquery  《=====拒绝I所有Pv6的用户

Restrict 172.25.25.25   《=========允许这个IP进入NTP服务器

---------------------------------------------------------------------------------------------------

参数介绍:

ignore:拒绝所有类型的NTP连接

Nomodify:客户端不能使用ntpcntpq这两个程序来修改服务器的时间参数,但客户端仍可通过这部主机来进行网络校时

Notrap:不提供trap这个远程事件登陆(remote event logging)的功能

Noquery:客户端不能够使用ntpcntpq等命令来查询时间服务器,等于不提供NTP的网络校时

Notrust:拒绝没有认证的客户端

 

如果使用在没有参数的地方添加,表示该IP或者网段不受任何限制

如:

172.25.25.25 表示该IP不受任何限制

一般可以先关闭NTP的同步权限,再一个一个的添加可以同步的客户端IP或网段。

----------------------------------------------------------------------------------------

 

# 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  《===放行本机回环IPv4形式

restrict -6 ::1 《====放行本机回环IPv6形式

 

# 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 ======上层同步的服务器

server 1.rhel.pool.ntp.org ======上层同步的服务器

server 2.rhel.pool.ntp.org ======上层同步的服务器

 

#broadcast 192.168.1.255 key 42 # broadcast server

#broadcastclient # broadcast client

#broadcast 224.0.1.1 key 42 # multicast server

#multicastclient 224.0.1.1 # multicast client

#manycastserver 239.255.254.254 # manycast server

#manycastclient 239.255.254.254 key 42 # manycast client

 

# 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

 

# Drift file.  Put this in a directory which the daemon can write to.

# No symbolic links allowed, either, since the daemon updates the file

# by creating a temporary in the same directory and then rename()'ing

# it to the file.

 

driftfile /var/lib/ntp/drift

 

TIP

因为NTP默认本身的时间计算式依据BIOS的芯片振荡周期频率来计算的,但是这个数值与上层time server不见得一致,所以NTP这个daenonntpd)会自动去计算我们自己主机的频率与上层time server的频率,并且将两个频率的误差记录下来,记录下来的文件就是在driftfiile后面的完整文件名指定的文件。

  • 该文件必须指定完整的路径
  • 该文件不能是链接文件
  • 该文件需要设置成ntpd这个daemon可以写入的权限
  • 改文件所记录的数值单位是百万分之一秒(ppm

 

# 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

 

 

NTP服务报错解析:


当用ntpdate -d 来查询时会发现导致 no server suitable for synchronization found 的错误的信息有以下2个: 
错误1.Server dropped: Strata too high
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客户端。

server 127.127.1.0
fudge 127.127.1.0 stratum 8 

ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。
那么如何知道何时ntp server完成了和自身同步的过程呢?
ntp server上使用命令:

# watch ntpq -p

Every 2.0s: ntpq -p                                                    Tue Dec  7 05:52:55 2010

 

     remote           refid      st t when poll reach   delay   offset  jitter

==============================================================================

*LOCAL(0)        .LOCL.           8 l   22   64  377    0.000    0.000   0.001

 

注意LOCAL的这个就是与自身同步的ntp server
注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0175次的变更,每一次是poll的值的秒数,是64*5=320秒的时间。
如果之后从ntp客户端同步ntp server还失败的话,用ntpdate d来查询详细错误信息,再做判断。
错误2.Server dropped: no data
从客户端执行netdate d时有错误信息如下:

transmit(192.168.30.22) 
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
192.168.30.22: Server dropped: no data
server 192.168.30.22, port 123

.....
28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found
出现这个问题的原因可能有2


1。检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误。

使用以下命令检查ntp的版本:
# ntpq -c version
下面是来自ntp官方网站的说明:
The behavior of notrust changed between versions 4.1 and 4.2.
In 4.1 (and earlier) notrust meant "Don't trust this host/subnet for time".
In 4.2 (and later) notrust means "Ignore all NTP packets that are not cryptographically authenticated." This forces remote time servers to authenticate themselves to your (client) ntpd
解决:
把notrust去掉。

2。检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。
可以用命令
#service iptables stop

来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。

 

转载请说明出处 |QQ:327488733@qq.com
原文地址:https://www.cnblogs.com/zhenxing/p/3599662.html