來源:不自量力Weithenn Study Daily 網站地圖 (http://www.weithenn.idv.tw/cgi-bin/wiki.pl?OpenNTPD-%E7%B6%B2%E8%B7%AF%E5%B0%8D%E6%99%82)

【前言】

[OpenNTPD Org]是 OpenBSD 的子計劃,目標就是提供一個安全的 NTP Service;當您的區網有許多伺服器,而每一台伺服器各自去和 stratum 1/2 public ntp servers 對時是浪費網路資源且不智的做法。較好的作法是 DMZ 裡一、二台伺服器 (屆時擔任 NTP Server 角色) 和外面 stratum 1/2 ntp servers 對時,而其它 DMZ 或 LAN 的機器 (屆時擔任 NTP Client 角色) 與之對時。

【作業環境】

FreeBSD 6.2-RELEASE-p6

openntpd-3.9p1_1,2

NTPClock.exe (Windows NTP Client UI Software)
【安裝及設定】

NTP Server 設定

Step1.安裝 openntpd

安裝過程中系統會自動幫您新增 _ntp 這個 user、group,屆時也會以這帳號來啟動 openntpd 服務。

#cd /usr/ports/net/openntpd //切換至安裝路徑
#make install clean //安裝並清除安裝過程中不必要的檔案

Step2.設定 ntpd.conf

#vi /usr/local/etc/ntpd.conf //修改 openntpd 設定檔
listen on 192.168.1.10 //指定屆時 openntpd 的 IP
server tick.stdtime.gov.tw //指定 time server
server tock.stdtime.gov.tw
server clock.stdtime.gov.tw
server watch.stdtime.gov.tw
server time.stdtime.gov.tw
server ntp.ntu.edu.tw
server 3.tw.pool.ntp.org
server 1.asia.pool.ntp.org
server 3.asia.pool.ntp.org

Step3.修改 /etc/rc.conf、syslog.conf

修改 /etc/rc.conf 以便重開機時能自動載入 openntpd 服務

#vi /etc/rc.conf
openntpd_enable="YES"

修改 /etc/syslog.conf 以便讓我們了解 NTP Server 去跟外面 Time Server 對時及調整本機時間的訊息

#vi /etc/syslog.conf
ntp.info /var/log/openntpd.log

請注意:應該要寫 daemon.info 還是 ntp.info 才會有校時資料寫入?跟你安裝的 openntpd 版本有關系,至於新版的 openntpd 為何要設定為 ntp.info 原因請看這篇 [ports/114191: (patch) net/openntpd should use syslog facility LOG_NTP]

openntpd-3.9p1,2 設定 daemon.info /var/log/openntpd.log
openntpd-3.9p1_1,2 設定 ntp.info /var/log/openntpd.log

加入上列一行後我們先建立 openntpd.log 這個空檔後在 reload syslogd 服務

#touch /var/log/openntpd.log
#/etc/rc.d/syslogd reload

Step4.啟動 openntpd 服務

#/usr/local/etc/rc.d/openntpd start //啟動 openntpd 服務
Starting openntpd.

檢查 openntpd 服務是否啟動成功

# ps ax |grep ntpd
72621 ?? Is 0:00.00 ntpd: [priv] (ntpd)
72622 ?? S 0:00.16 ntpd: ntp engine (ntpd)
73161 p3 R+ 0:00.00 grep ntpd

可發現有去跟外面的 Time Server 連接

#sockstat |grep ntpd
_ntp ntpd 72622 3 dgram -> /var/run/logpriv
_ntp ntpd 72622 4 udp4 192.168.1.10:54512 220.130.158.51:123
_ntp ntpd 72622 5 stream -> ??
_ntp ntpd 72622 6 udp4 192.168.1.10:123 *:*
_ntp ntpd 72622 7 udp4 192.168.1.10:51987 220.130.158.71:123
_ntp ntpd 72622 8 udp4 192.168.1.10:49509 220.130.158.72:123
_ntp ntpd 72622 9 udp4 192.168.1.10:62446 220.130.158.54:123
_ntp ntpd 72622 10 udp4 192.168.1.10:51543 192.115.133.35:123
_ntp ntpd 72622 11 udp4 192.168.1.10:58900 140.112.4.189:123
_ntp ntpd 72622 12 udp4 192.168.1.10:62115 61.70.206.117:123
_ntp ntpd 72622 13 udp4 192.168.1.10:52513 60.56.119.79:123
_ntp ntpd 72622 14 udp4 192.168.1.10:56684 220.130.158.52:123
root ntpd 72621 3 dgram -> /var/run/logpriv
root ntpd 72621 4 stream -> ??

使用 ntptrace 來了解與外部 time server 對時狀況

#ntptrace 220.130.158.72
220-130-158-72.HINET-IP.hinet.net: stratum 2, offset 0.002189, root distance 0.012691

或使用 ntpq 來了解與外部 time server 對時狀況

#ntpq -p 220.130.158.72
remote refid st t when poll reach delay offset jitter
==============================================================================
+220-130-158-50. .IRIG. 1 u 187 1024 231 20.874 -0.271 499.767
*220-130-158-70. .IRIG. 1 u 492 1024 67 12.691 2.189 0.957
+220-130-158-51. 220.130.158.50 2 u - 1024 167 75.498 28.432 13.156
220-130-158-71. 220.130.158.50 2 u - 1024 377 1.329 -3.495 0.151

Step5.查看 openntpd.log

我們可以查看剛才設定的 openntpd.log 來了解目前本機的時間校對情況 (大約 3 ~ 5 分鐘調整 1 秒),基本上 openntpd 是慢慢調整時間,若您想一次就調整好可以利用 -s 來一次把時間調到標準 (不知會不會出事?)

#tail /var/log/openntpd.log
Jul 31 15:09:46 mybsd ntpd[72622]: listening on 192.168.1.10
Jul 31 15:09:46 mybsd ntpd[72622]: ntp engine ready
Jul 31 15:10:03 mybsd ntpd[72622]: peer 220.130.158.72 now valid //time server 有效
Jul 31 15:10:04 mybsd ntpd[72622]: peer 61.70.206.117 now valid
Jul 31 15:10:06 mybsd ntpd[72622]: peer 220.130.158.52 now valid
Jul 31 15:11:06 mybsd ntpd[72621]: adjusting local clock by 454.400335s //本機校時狀態
Jul 31 15:15:25 mybsd ntpd[72621]: adjusting local clock by 453.485940s
Jul 31 15:18:56 mybsd ntpd[72621]: adjusting local clock by 452.473868s
Jul 31 15:22:45 mybsd ntpd[72621]: adjusting local clock by 451.561672s
Jul 31 15:26:03 mybsd ntpd[72621]: adjusting local clock by 450.369801s
Jul 31 15:29:52 mybsd ntpd[72621]: adjusting local clock by 449.532482s
Jul 31 15:33:40 mybsd ntpd[72621]: adjusting local clock by 448.585979s

FreeBSD NTP Client 設定

Step1.安裝 openntpd

安裝過程中系統會自動幫您新增 _ntp 這個 user、group,屆時也會以這帳號來啟動 openntpd 服務。

#cd /usr/ports/net/openntpd //切換至安裝路徑
#make install clean //安裝並清除安裝過程中不必要的檔案

Step2.設定 ntpd.conf

#vi /usr/local/etc/ntpd.conf //修改 openntpd 設定檔
server 192.168.1.10 //指定我們架設的 time server

Step3.修改 /etc/rc.conf、syslog.conf

修改 /etc/rc.conf 以便重開機時能自動載入 openntpd 服務

#vi /etc/rc.conf
openntpd_enable="YES"

修改 /etc/syslog.conf 以便讓我們了解 NTP Server 去跟外面 Time Server 對時及調整本機時間的訊息

#vi /etc/syslog.conf
ntp.info /var/log/openntpd.log

加入上列一行後我們先建立 openntpd.log 這個空檔後在 reload syslogd 服務

#touch /var/log/openntpd.log
#/etc/rc.d/syslogd reload

Step4.啟動 openntpd 服務

#/usr/local/etc/rc.d/openntpd.sh start //啟動 openntpd 服務
Starting openntpd.

檢查 openntpd 服務是否啟動成功

# ps ax |grep ntpd
5500 ?? Is 0:00.01 ntpd: [priv] (ntpd)
5501 ?? I 0:00.02 ntpd: ntp engine (ntpd)
5524 p0 R+ 0:00.00 grep ntpd

可發現有去跟內部的 Time Server 連接

#sockstat |grep ntpd
_ntp ntpd 5501 3 dgram -> /var/run/logpriv
_ntp ntpd 5501 4 udp4 172.18.1.12:63701 192.168.1.10:123
_ntp ntpd 5501 5 stream -> ??
root ntpd 5500 3 dgram -> /var/run/logpriv
root ntpd 5500 4 stream -> ??

Step5.查看 openntpd.log

我們可以查看剛才設定的 openntpd.log 來了解目前本機的時間校對情況 (大約 3 ~ 5 分鐘調整 1 秒),基本上 openntpd 是慢慢調整時間,若您想一次就調整好可以利用 -s 來一次把時間調到標準 (不知會不會出事?)

#tail /var/log/openntpd.log
Aug 1 17:21:08 testbsd ntpd[5443]: ntp engine exiting
Aug 1 17:21:08 testbsd ntpd[5442]: Terminating
Aug 1 17:21:08 testbsd ntpd[5501]: ntp engine ready
Aug 1 17:21:30 testbsd ntpd[5501]: peer 192.168.1.10 now valid
Aug 1 17:22:24 testbsd ntpd[5500]: adjusting local clock by 398.739917s
Aug 1 17:25:39 testbsd ntpd[5500]: adjusting local clock by 397.073363s

Solaris 9 NTP Client 設定

Step1.設定 ntp.conf

#cd /etc/inetd ; cp ntp.client ntp.conf //複製 ntp client 範例檔來修改
#vi ntp.conf //加入如下三行
server 192.168.1.10 //指定 ntp server
driftfile /var/ntp/ntp.drift
logfile /var/ntp/ntp.log

Step2.建立相關檔案 ntp.drift、ntp.log

#touch /var/ntp/ntp.drift
#touch /var/ntp/ntp.log

Step3.啟動 xnptd 服務

#/etc/init.d/xntpd start //啟動 xnptd 服務

檢查 xntpd 服務是否啟動成功

#ps -ef |grep ntpd
root 9715 1 0 15:57:23 ? 0:00 /usr/lib/inet/xntpd

使用 ntpq 來了解跟內部 time server 對時狀況

#ntpq -p
remote refid st t when poll reach delay offset disp
==============================================================================
*192.168.1.10 220.130.158.51 3 u 21 64 377 0.46 1.764 0.23

使用 ntptrace 來了解跟內部 time server 對時狀況

# ntptrace -n 127.0.0.1
127.0.0.1: stratum 4, offset 0.000028, synch distance 0.01624
192.168.1.10: stratum 3, offset 0.001898, synch distance 0.00569
220.130.158.51: stratum 2, offset 0.020941, synch distance 0.03044
220.130.158.50: stratum 1, offset 0.180524, synch distance 1.00000, refid 'IRIG'

使用 snoop 來了解跟內部 time server 對時狀況

#snoop |grep -i ntp
Using device /dev/bge0 (promiscuous mode)
192.168.1.11 -> 192.168.1.10 NTP client (Wed Aug 1 11:41:34 2007)
192.168.1.10 -> 192.168.1.11 NTP server (Wed Aug 1 11:41:34 2007)

Step4.查看 ntp.log、messages

查看 /var/ntp/ntp.log 來了解跟內部 time server 對時狀況

#tail /var/ntp/ntp.log
1 Aug 16:01:39 xntpd[9715]: synchronized to 192.168.1.10 , stratum=3
1 Aug 12:47:43 xntpd[8605]: time reset (step) 171.638805 s

查看 /var/ntp/ntp.log 來了解跟內部 time server 對時狀況

#tail -f /var/adm/messages |grep ntp
Aug 1 14:53:58 sundev ntpdate[9394]: [ID 558275 daemon.notice] adjust time server 192.168.1.10 offset 0.004170 sec
Aug 1 14:54:00 sundev xntpd[9396]: [ID 702911 daemon.notice] xntpd 3-5.93e Mon Sep 20 15:47:11 PDT 1999 (1)
Aug 1 14:54:00 sundev xntpd[9396]: [ID 301315 daemon.notice] tickadj = 5, tick = 10000, tvu_maxslew = 495, est. hz = 100
Aug 1 14:54:00 sundev xntpd[9396]: [ID 798731 daemon.notice] using kernel phase-lock loop 0041

Windows NTP Client 設定

我們可使用由[國家時間與頻率標準實驗室]出的[NTP 校時軟體]目前最新版本為 V2.1 來達成讓 Windows Server 也跟 FreeBSD NTP Server 對時即可。

1.下載 http://www.stdtime.gov.tw/chinese/EXE/NTPClock.exe,並安裝它
2.在【時間伺服器】欄位上填上 FreeBSD NTP Server IP Address --> 按下【增加】
3.勾選【信任未同步伺服器時間】
4.預設為一個小時跟指定的 NTP Server 對時若想馬上對時可按下【立即查尋主機時間】

有關於 NTPClock 軟體的操作可參考[標準時間網路校時客戶端程式 (NTPClock) 操作說明]

【參考】

[潛艦 | http://to2100.idv.tw , Network Lab@FreeBSD . » FreeBSD Openntpd]

[Setup OpenNTPD - OSWikiHK]

[OpenNTPD Org]

[When {Puffy} Meets ^RedDevil^: FreeBSD Openntpd]

[HOWTO NTP Using OpenNTPD - Gentoo Linux Wiki]

[OpenNTPD OpenBSD Config Files]

[NTPPoolServers < Servers < NTP]

[pool.ntp.org: NTP Servers in Asia, asia.pool.ntp.org]

[pool.ntp.org: NTP Servers in Taiwan, tw.pool.ntp.org]

[Configuring OpenNTPD on OpenBSD 3.7]

[ports/114191: (patch) net/openntpd should use syslog facility LOG_NTP]

[ntp]

[OpenNTPD - Wikipedia, the free encyclopedia]

[Quick HOWTO : Ch24 : The NTP Server - Linux Home Networking]

[docs.sun.com: System Administration Guide: Resource Management and Network Services]

[鳥哥的 Linux 私房菜 -- NTP 時間伺服器]

【Me FAQ】

Q1.openntpd.log內沒有任何訊息寫入?

Error Message:

當我啟動 openntpd 服務想查看 openntpd.log 時,但該 log 內容都是空的

Ans:

請確定以下事項你作了沒

* 修改完 /etc/syslog.conf 後是否有 reload syslogd
* 試著 restart openntpd

若還是沒有訊息寫入指定的 log 內就輸出 all.log 看看是否 ok,我的問題是之前修改 syslogd 是寫 daemon.info 但 log 內容都是空的,看了這篇後 [ports/114191: (patch) net/openntpd should use syslog facility LOG_NTP]改為 ntp.info 後 log 就有訊息寫入了。

【Log】

初稿-2007/07/31