qq412806417
作者qq412806417·2012-10-09 17:55
系统运维工程师·北京宇信易诚

ntp时间服务器

字数 5832阅读 4145评论 0赞 2

环境描述  ntp server ip172.16.81.220
ntp
局域网:172.16.85.0  (client ip 172.16.85.50)
掩码:255.255.248.0
server
端的配置:
使用root身份配置/etc/ntp.conf,加入广播域172.16.85.0 ,注释掉外部时间服务器,请注意红色部分的修改
[root@localhost
]#vi /etc/ntp.conf
# --- OUR TIMESERVERS -----
#server 0.pool.ntp.org
#server 1.pool.ntp.org
#server 2.pool.ntp.org
# --- NTP MULTICASTCLIENT ---
#multicastclient # listen on default 224.0.1.1
# restrict 224.0.1.1 mask 255.255.255.255 nomodify notrap
restrict 172.16.85.0 mask 255.255.248.0 nomodify notrap
[root@localhost
]#chkconfig ntpd on
[root@localhost
]#service ntpd star

配置客户端:
 
使用root身份配置/etc/ntp.conf,设置本网段172.16.85.0,注释掉外部服务器,加入172.16.81.220做服务器,请注意红旗部分的修改:
[root@localhost
]#vi /etc/ntp.conf
# -- CLIENT NETWORK -------
# Permit systems on this network to synchronize with this
# time service. Do not permit those systems to modify the
# configuration of this service. Also, do not use those
# systems as peers for synchronization.
restrict 172.16.85.0 mask 255.255.248.0 nomodify notrap

 
# --- OUR TIMESERVERS -----
#server 0.pool.ntp.org
#server 1.pool.ntp.org
#server 2.pool.ntp.org
server 172.16.81.220
……
……
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
……
[root@localhost
]#service ntp stop
[root@localhost
]#ntpdate 172.16.81.220
欲定期同步时间,请把这个ntpdate命令写在crontab中即可。
在测试的过程中出现的如下问题,一一总结了一下,希望对大家有所帮助:

1
、客户端执行:
[root@localhost
]#ntpdate 172.16.81.220 报错如下:

28 Apr 13:40:50 ntpdate[25573]: the NTP socket is in use, exiting
原因是由于 xntpd 已经绑定到了该 Socket。运行 ntpdate 时,它会首先进行广播,然后侦听端口 123。如果 xntpd 正在运行,而有一个进程已经在侦听该端口了,则会使 ntpdate 无法在上面运行。运行下列命令,即可找出 xntpd PID
ps -ef|grep xntpd
清除 (Kill) 该进程,然后尝试再次与 ntp 服务器进行同步。此时您不应该收到此错误消息
或是执行service ntpd stop(停掉服务)之后也可完成同步

2
、客户端执行同步命令时报错如下:
[root@localhost
]# ntpdate 172.16.81.220
28 Apr 14:28:47 ntpdate[26335]: no server suitable for synchronization found

1
)、加上-d的参数详细分析看如下信息:
[root@localehost
]# ntpdate -d 172.16.81.220
28 Apr 15:32:04 ntpdate[28467]: ntpdate 4.2.0a@1.1190-r Wed Aug 23 01:29:49 EDT 20                                                            06 (1)
Looking for host 172.16.81.220 and service ntp
host found : 172.16.81.220
transmit(172.16.81.220)
receive(172.16.81.220)
transmit(172.16.81.220)
receive(172.16.81.220)
transmit(172.16.81.220)
receive(172.16.81.220)
transmit(172.16.81.220)
receive(172.16.81.220)
transmit(172.16.81.220)
172.16.81.220: Server dropped: strata too high
server 172.16.81.220, port 123
stratum 16, precision -19, leap 11, trust 000
refid [172.16.81.220], delay 0.02582, dispersion 0.00000
transmitted 4, in filter 4
reference time:    00000000.00000000  Thu, Feb  7 2036 14:28:16.000
originate timestamp: cda12f74.50246009  Tue, Apr 28 2009 15:32:04.313
transmit timestamp:  cda12f75.08c5a815  Tue, Apr 28 2009 15:32:05.034
filter delay:  0.02599  0.02588  0.02583  0.02582
         0.00000  0.00000  0.00000  0.00000
filter offset: -0.72136 -0.72136 -0.72134 -0.72134
         0.000000 0.000000 0.000000 0.000000
delay 0.02582, dispersion 0.00000
offset -0.721345
28 Apr 15:32:05 ntpdate[28467]: no server suitable for synchronization found
因为NTP server还没有和其自身或者它的server同步上。
以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。
server 127.127.1.0
fudge 127.127.1.0 stratum 10
可以通过下面的方式确认ntp server的同步过程
watch ntpq -p
Every 2.0s: ntpq -p                                           Wed Apr 28 16:49:20 2009

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 172.16.81.220   LOCAL(0)         8 u   22   64    1    2.113 179133.   0.001
 LOCAL(0)        LOCAL(0)        10 l   21   64    1    0.000   0.000  0.001
注意LOCAL的这个就是与自身同步的ntp server
注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0175次的变更,每一次是poll的值的秒数,是64*5=320秒的时间。
即配置好之后客户段等待5分多钟
也可以在server上执行hwclock -w  server本身和硬件时间同步,
然后客户段执行ntpdate server ip即可同步时间

2
)、另外:no server suitable for synchronization found经过-d 分析还有下面的结果
 172.16.81.220: Server dropped: no data

原因可能有3
a
、可能是server段停掉了ntp服务  server端重新启动ntp服务即可。
b
、检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误。
使用以下命令检查ntp的版本:ntpd -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去掉。
上面的实验中不会出现此问题,应为添加的时候没有添加notrust
C
、检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。
可以用命令service iptables stop

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

2

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

X社区推广