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的設置。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM