freebsd系統時間同步及ntp服務的提供

來源:互聯網
上載者:User

[轉]freebsd系統時間同步及ntp服務的提供

中國國家授時中心的時間伺服器IP地址:210.72.145.44

http://www.cnfug.org/journal/archives/000038.html

www.cnfug.org 的NTP 的原文:

tonight <dingxin@sea.plala.or.jp>

電腦的時間放置不理的話過不久就會出現誤差。
這個誤差會反映在email送信時刻的time stamp上,會反映在你的論壇文章發表時刻上。至少我遇到過這個問題。
網路上有ntp(network time protocol)伺服器,提供當前的時刻。我們可以將本地的時刻與ntp伺服器的時刻進行同步。

目的如下:
1,啟動時用ntpdate進行時刻修正。
2,啟動中,通過ntpd進行時刻校正的同時,將各個用戶端電腦的時刻也進行同期校正。
3,如果不能找到ntp伺服器,那麼就使用local time,同時同步客戶。

全世界約有100多個ntp伺服器,從下面地址找一個離你最近的

http://www.eecis.udel.edu/~mills/ntp/clock1a.html
我選擇了兩個

clock.nc.fukuoka-u.ac.jp 133.100.9.2
clock.tl.fukuoka-u.ac.jp 133.100.11.8

步驟:

FreeBSD中有兩個標準的ntp程式,其一為ntpdate。
ntpdate通過從ntp伺服器擷取時刻,調整本地時刻。

# ntpdate clock.nc.fukuoka-u.ac.jp
9 Oct 18:12:23 ntpdate: step time server 133.100.9.2 offset -19.112674
大約有19秒鐘的誤差

自動設定

# grep ntpdate /etc/defaults/rc.conf >>/etc/rc.conf
# vi /etc/rc.conf
ntpdate_enable="YES" # Run ntpdate to sync time on boot (or NO).
ntpdate_program="ntpdate" # path to ntpdate, if you want a different one.
ntpdate_flags="clock.nc.fukuoka-u.ac.jp" # Flags to ntpdate (if enabled).
重新啟動以後就運行了,當然設定沒有完,不必急著啟動

FreeBSD的另一個標準ntp程式,ntp
ntp程式使的擷取ntp時刻的同時,向其他pc提供時刻。

添加檔案ntp.conf:

用來作為標準時刻的ntp伺服器我選擇了兩個

clock.nc.fukuoka-u.ac.jp 133.100.9.2
clock.tl.fukuoka-u.ac.jp 133.100.11.8

同時指定複數個伺服器也沒有問題,系統會自動選擇一個可以信賴的。
這裡,為避免多餘的DNS資料包傳遞,我們直接指定IP地址。
server行 server 127.127.1.0 為參考本地時刻時用的地址。然後用fudge指定階層編號為5,降低其優先度。

接著用restrict對每一個IP地址指定相應的規則。
最後,指定波長校正用的drift文檔儲存地址。關於這個命令列,具體的含義不太清楚。不過如果沒有的話,時間校正起來就會比較慢

# vi /etc/ntp.conf
server 133.100.9.2 #clock.nc.fukuoka-u.ac.jp
server 133.100.11.8 #clock.tl.fukuoka-u.ac.jp
server 127.127.1.0
fudge 127.127.0.1 stratum 5
restrict default ignore
restrict 127.0.0.0 mask 255.0.0.0
restrict 192.168.1.0 mask 255.255.255.0 noquery nopeer notrust
restrict 133.100.9.2 noquery
restrict 133.100.11.8 noquery
driftfile /etc/ntpd.drift
啟動測試

# ntpd -p /var/run/ntpd.pid
# tail /var/log/messages
Oct 9 16:46:56 chiwawa ntpd[89409]: ntpd 4.1.0-a Thu Apr 3 08:26:24 GMT 2003 (1)
Oct 9 16:46:56 chiwawa ntpd[89409]: kernel time discipline status 2040
......
Oct 9 16:50:10 chiwawa ntpd[89409]: time set -0.189546 s
看到類似的結果就可以了。

運行測試
ntpd的運行用ntpq命令

# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*clock.nc.fukuok .GPS. 1 u 43 64 37 19.067 -6.884 10.339
+clock.tl.fukuok .GPS. 1 u 36 64 35 19.670 -3.259 2.341
LOCAL(0) LOCAL(0) 5 l 45 64 37 0.000 0.000 0.001
啟動後到時刻校正完成需要一點時間。*是第一候補,+是第二。
本地時刻校正完成以後就可以為其他PC提供時刻校正服務了。

自動啟動的設定
完成以上設定,確認運行無誤以後:

# grep ntpd /etc/defaults/rc.conf >>/etc/rc.conf
# vi /etc/rc.conf
xntpd_enable="YES" # Run ntpd Network Time Protocol (or NO).
xntpd_program="ntpd" # path to ntpd, if you want a different one.
xntpd_flags="-p /var/run/ntpd.pid" # Flags to ntpd (if enabled).

往rc.conf追加上面3行內容,並修改。
用戶端PC設定:
UNIX OS

# vi /etc/ntp.conf
server [local ntp server IP] prefer
driftfile /etc/ntpd.drift
或者追加下面內容到crontab,這樣,每過一個小時0分的時候就自動更新。
# vi /etc/crontab
0 * * * * root ntpdate [ntp server IP] >/dev/null 2>&1
當然,你的主機其實也是一個客戶機,這些內容也可以用上。
Windows

精工的網站上有下載軟體,不過是日語的
http://www.seiko-p.co.jp/systems/down/time.html
windows2000內建了sntp機能,請自己研究吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.