環境描述 ntp server ip:172.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做伺服器,請注意紅旗部分的修改:
# -- 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.
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 [email protected] 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)
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
可以通過下面的方式确認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的時候,從0到17是5次的變更,每一次是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
來關掉iptables服務後再嘗試從ntp用戶端的同步,如果成功,證明是防火牆的問題,需要更改iptables的設定。