Debian · Linux utilities

使用 ntpd 來替換 ntpdate 以完成校時的工作

現今時間同步上, 有許多校時相關 protocol 可使用.

以精確度來分級, IEEE 1588v2 最為準確, 但也須搭配較為複雜的相關設備以及相關應用程式.

而次準確的 IRIG-B 主要使用同軸電纜或者serial port 傳輸媒介.

所以除了一些工業應用有需求外, 一般環境下還是以 NTP 透過 Ethernet 來進行時間校正.

以時間精確度來分:
  1. Precision Time Protocol (PTP) – IEEE 1588-2008
    • nanoseconds level
    • Media: Ethernet
  2. IRIG-B
    • microseconds level
    • Media: Coaxial, Serial or Fiber-optic
  3. NTP
    • milliseconds level
    • Media: Ethernet

 

以往使用 NTP 更新時間都是使用 ntpdate <server IP> 指令來同步時間, 若要定時同步可搭配 cron table.

2016-07-19_174558.png

但 ntp.org 在 2012 年時開始, 表示將讓 ntpd & sntp 來取代 ntpdate [1],  主要原因如下:

  • ntpd & sntp 可以 cover ntpdate 的功能
  • ntpd 和 ntpdate 原本共用相同的程式碼, 後來 ntpd 優化了裡面的演算法, 但 ntpdate 並沒有更新

ntpdate & ntpd 差異點如下:

  • ntpdate 機制是 one-shot 同步, 若要定時同步需搭配 cron
  • ntpd 是 daemon, 可自動定期將系統時間和 NTP server 更新

若要使用 ntpd 達到 ntpdate 的功能, 可使用 ntpd -q <server IP> 指令

2016-07-19_174703.png

若執行 ntpd daemon function, 可使用 ntpq -p 來確認目前時間同步的狀況, 其中包含 delay, offset, jitter 的相關資訊.

2016-07-19_185019.png

 

由於看到官方描述 ntpd 和 ntpdate 使用的演算法有所差異, 所以我在 x86 機器上量測了一下 ntpdate & ntpd 的時間同步速度.

下圖可以看到 ntpdate 花的時間比 ntpd 硬生生多了 5 秒!!  難怪官方會強力推薦使用 ntpd

未命名.png

其他 ntpd, ntpq  相關功能可以在 [2], [3] 查詢到.

最後, 比較有趣的是:  Debian 上還是可以使用 ntpdate 套件 [6].

之前 Debian bug system 中有人提出要將 ntpdate 套件下架 [4], 但討論結論是跟著 upstream 走 –  若 upstream 移除 ntpdate, ntpdate package 才會被移除.

目前 ntp upstream  尚有保留 ntpdate 的程式, 最近也有 commit log, 只是比起其他程式, 明顯看的出來比較沒有在維護. [5]

除此之外, ntpd 也可以搭配 gpsd 來進行校時. 這篇文章有詳細說明.

 

ref:

[1] http://support.ntp.org/bin/view/Dev/DeprecatingNtpdate

[2] https://wiki.debian.org/NTP

[3] https://help.ubuntu.com/lts/serverguide/NTP.html

[4] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=514318

[5] https://github.com/ntp-project/ntp/tree/stable/ntpdate

[6] https://packages.debian.org/search?keywords=ntpdate

使用 ntpd 來替換 ntpdate 以完成校時的工作 有 “ 1 則迴響 ”

發表迴響