ntpd和ntpdate


以無GUI 的Ubuntu 為例

1. ntpdate

ntpdate是Ubuntu發行版的標配。按Ubuntu14.04的官方文檔(這里),Ubuntu在啟動時,會調用ntpdate,根據Ubuntu NTP server更新系統時間,但我實際測試卻並不成功。

注:測試方法:sudo sysv-rc-conf npt off -> 修改為一個過去的時間sudo date -s hh:mm:ss ,並用date檢查是否已修改-> 同步到硬件sudo hwclock -w ->

sync;sync;sync;sudo reboot ->開機后等待10分鍾,發現date還是錯誤時間。

所以,對於客戶端,那就在crontab中增加一個任務,如下,其中weblbserver-1是ntp服務器,如果你想出錯時(比如weblbserver無法連接)發郵件,那要設置MAILTO=yourmail,

並且還要安裝postfix,安裝也非常簡單,直接用apt-get install就行,安裝完無需任何配置。

*/10  * * * *   root    (ntpdate weblbserver-1 && hwclock -w) > /dev/null

 

如果開啟了nptd服務,那就無法執行ntpdate了,如下

zhj@test:~$ sudo service ntp status
 * NTP server is running
zhj@test:~$
zhj@test:~$ sudo ntpdate pool.ntp.org
29 Dec 12:55:26 ntpdate[2964]: the NTP socket is in use, exiting
zhj@test:~$

 

2. ntpd

下面是自己的ntp server——weblbserver-1的配置文件/etc/ntp.conf,它是集群中的一台服務器,它允許集群內的其它服務器從它這里獲取時間。

該配置文件修改后,要調用sudo service ntp reload重新加載。另外,注意在ntp server上要在防火牆中放行UDP 123號端口

ntpd啟動后,要等4*2^(minpoll ~ maxpoll) 秒才能真正開始同步,所以minpoll和maxpoll不要設置的太大。

配置文件參數含義可參考 http://wenku.baidu.com/view/e3282cc62cc58bd63186bd41.html

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift


# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
# 參數minpoll和maxpoll都是2的冪,下面即設置2^9=512秒同步一次 server 0.ubuntu.pool.ntp.org minpoll 9 maxpoll 9 server 1.ubuntu.pool.ntp.org minpoll 9 maxpoll 9 server 2.ubuntu.pool.ntp.org minpoll 9 maxpoll 9 server 3.ubuntu.pool.ntp.org minpoll 9 maxpoll 9 # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for # details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions> # might also be helpful. # # Note that "restrict" applies to both servers and clients, so a configuration # that might be intended to block requests from certain clients could also end # up blocking replies from your own upstream servers. # By default, exchange time with everybody, but don't allow configuration. restrict -4 default kod notrap nomodify nopeer noquery restrict -6 default kod notrap nomodify nopeer noquery # Local users may interrogate the ntp server more closely. restrict 127.0.0.1 restrict ::1 # Clients from this (example!) subnet have unlimited access, but only if # cryptographically authenticated.
# 放行局域網內的主機,可以從本機獲取ntp時間,但不能修改本機的時間 restrict
192.168.0.0 mask 255.255.0.0 nomodify

同時,在本ntp server主機增加一個定時任務,每小時將osclock刷入hwclock,如下,如果想發郵件,要設置MAILTO,並安裝postfix

*/10  *   * * *   root    hwclock -w > /dev/null

 

我們可以用ntpq -p來查看本機與上層NTP server的時間同步情況,如下

zhj@weblbserver-1:~$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 dns1.synet.edu. 202.118.1.46     2 u  915 1024    1   43.464  -134.79   0.004
 golem.canonical 192.93.2.20      2 u  914 1024    1  433.083  -198.90   0.004
zhj@weblbserver-1:~$

remote ——亦即 NTP 主機的 IP 或主機名,注意最左邊的符號

  • 如果有『 * 』代表目前正在作用當中的上層 NTP
  • 如果是『 + 』代表也有連上線,而且可作為下一個提供時間更新的候選者
  • 如果是空格,那代表還沒有連接

refid  —— 參考的上一層 NTP 主機的地址

st      —— 就是 stratum 階層,ntp允許的層數為1~15

when —— 上次同步時間到現在的距離,單位是秒。當然,如果ntp服務是剛啟動,還沒同步過,那這個參數就是ntp服務啟動后到現在的距離

poll   —— 同步周期,單位為秒

reach —— 它是八進制數,正常情況下值為[0, 1, 3, 7, 17, 37, 77, 177, 377],對應的二進制為[0, 1, 11, 111, 1111, 11111, 111111, 1111111, 11111111],

               ntp服務啟動后,reach就以poll值為周期與ntp server通信,為了方便理解,我們可以簡單的認為每次ping一下上層ntp server,如果成功,那

               reach就向左移一位,右邊補1,如果失敗,則右邊補0,所以如果reach不是上面給出的枚舉值,那就是在通信過程中出錯了。當reach 達到17時

             (對應1111,即最近的四次通信都成功了),那才開始同步時間,這時,remote項對應的域名或IP列表有,其中一個前面會有*號,表示該IP就是

               NTP server。而在開始同步時間之前,當客戶端訪問weblbserver-1這個NTP server時,都會出現stratum 16,no server suitable for

               nchronization found這樣的錯誤。也就是說如果你在NTP server主機上重啟了ntp服務,那要等4*poll秒(在前四次通信都是成功的前提下),

               該NTP server才與上層NTP server開始同步時間,而且只有當開始同步時,該NTP server才能為其它客戶端提供NTP服務。因此,你在/etc/ntp.conf

               中設置的同步周期minpoll maxpoll不能太大,因為每次ntp服務重啟后,要等4倍長的時間才能開始同步。

offset —— NTP server與本機的時間差,單位為毫秒,即10^(-3) 秒。如果這個值比較大,那就要考慮縮短同步周期了

delay —— 網絡傳輸過程當中延遲的時間,單位為 10^(-6) 秒

jitter —— Linux 系統時間與 BIOS 硬件時間的差異時間, 單位為 10^(-6) 秒

 


免責聲明!

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



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