時間同步,或者說校時,是所有 OS 都內建的基礎服務之一,以 Ubuntu 為例,它會以 ntp.ubuntu.com 的時間為標準來校準自己的時間,NTP(Network Time Protocol)是目前主流的時間協定。

然而在某些情況下,例如軍網、內網中,我們的機台可能無法用公網的 NTP 服務器,因此有必要自行搭建內網的 NTP 服務,如此方可確保內網內的所有機台都具有一致的時間基礎,才不會發生你那筆訂單在九點,我這邊出貨卻是在八點半的靈異現象。

chrony 是新一代的 NTP 服務,它既可以是 NTP client,也可以是 NTP server,這篇主要談的是以 chrony 作為 NTP server 的話題。

相較於老牌的 nptd、OpenNTPD、NTPsec,chrony 有以下優勢:

  • 現代化

是的沒錯,「新」本身就是優點,更具體的比較可以看〈Comparison of NTP implementations〉,另外 chrony 本身也已經是 RedHat Linux 的預設 NTP 服務,不是射後不理型套件,可以放心使用。

安裝

在 Debian / Ubuntu 一行安裝:

$ sudo apt install chrony

安裝 chrony 時會提示要取代 systemd-timesyncd,systemd-timesyncd 是 Ubuntu 預帶的 NTP client,本身不具備 NTP server 功能,而 chrony 同時具備 NTP client / server 功能,可以放心取代。

安裝過程會自行配置並運作 chrony 服務,裝完後確認一下:

$ systemctl status chrony 

應該會看到狀態顯示為「active (running)」。

另外也可以檢視當前的校時狀態:

$ chronyc tracking

應該會看到這樣的輸出:

Reference ID    : 01220D59
Stratum         : 3
Ref time (UTC)  : Wed Apr 26 01:59:48 2023
System time     : 0.000083251 seconds slow of NTP time
Last offset     : +0.000094192 seconds
RMS offset      : 0.000860929 seconds
Frequency       : 13.559 ppm fast
Residual freq   : +0.016 ppm
Skew            : 1.030 ppm
Root delay      : 0.012618504 seconds
Root dispersion : 0.001813692 seconds
Update interval : 64.1 seconds
Leap status     : Normal

看起來就很厲害對吧。

確認有正常運作就好,後面再介紹其他命令。

配置

配置檔在 /etc/chrony/chrony.conf,裡面有好幾個區段,其中定義上游 NTP 的區段如下:

# This will use (up to):
# - 4 sources from ntp.ubuntu.com which some are ipv6 enabled
# - 2 sources from 2.ubuntu.pool.ntp.org which is ipv6 enabled as well
# - 1 source from [01].ubuntu.pool.ntp.org each (ipv4 only atm)
# This means by default, up to 6 dual-stack and up to 2 additional IPv4-only
# sources will be used.
# At the same time it retains some protection against one of the entries being
# down (compare to just using one of the lines). See (LP: #1754358) for the
# discussion.
#
# About using servers from the NTP Pool Project in general see (LP: #104525).
# Approved by Ubuntu Technical Board on 2011-02-08.
# See http://www.pool.ntp.org/join.html for more information.
pool ntp.ubuntu.com        iburst maxsources 4
pool 0.ubuntu.pool.ntp.org iburst maxsources 1
pool 1.ubuntu.pool.ntp.org iburst maxsources 1
pool 2.ubuntu.pool.ntp.org iburst maxsources 2

這裡預設的都是公網上的 NTP 服務器,作為 NTP client 時,如果要換成內網的另一台,可以把這些註解掉,填入內網的某一台 NTP server:

server time.st.local

這裡的關鍵字 server 和上面的 pool 都是用於指示上游的 NTP server 位址,差別在 pool 表示一群 NTP server,而 server 表示單台 NTP server。

在預設的配置中,並沒有開啟 chrony 的 NTP server,要開起來也很簡單,在配置檔中加入一行:

allow

allow 也像 serverpool 一樣,後面還可以接一些參數,用於限定允許連入的 IP,但多數的情境下不會制定這麼細的規則,就一個 allow,誰想連就連吧。

重啟 chrony:

$ sudo systemctl restart chrony

chrony NTP server 啟動之後,拿別台機器當 NTP client 測一下沒問題就可以了。

chrony CLI 工具

chrony 的 CLI 工具為 chronyc,前面我們用過 chronyc tracking 來檢視 chrony 與上游 NTP server 的校時狀況,除此之外還有一些別的命令。

檢視上游 NTP server 的狀態:

$ chronyc sources

輸出如下:

MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^- prod-ntp-5.ntp1.ps5.cano>     2   8   377   108   -243us[ -299us] +/-  106ms
^- pugot.canonical.com           2   9   377   306    +19ms[  +19ms] +/-  156ms
^- alphyn.canonical.com          2   9   377   498  +3126us[+3080us] +/-  177ms
^- prod-ntp-4.ntp1.ps5.cano>     2   7   377   491   -192us[ -237us] +/-  100ms
^- time.cloudflare.com           3   9   377   377  -2563us[-2611us] +/-   58ms
^- 125-229-162-223.hinet-ip>     2   7   377   110   +328us[ +272us] +/-   58ms
^- 140.137.11.50                 2   9   277   512   -311us[ -356us] +/-   46ms
^* twtpe2-ntp-002.aaplimg.c>     1   9   377    48  -8300ns[  -66us] +/- 3441us

其中有些欄位沒有去深究它的意義,總之看起來很厲害。

檢視上游 NTP server 的一些統計數據:

$ chronyc sourcestats

輸出如下:

Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
prod-ntp-5.ntp1.ps5.cano>  23  15   56m     +0.198      0.662   -704us   807us
pugot.canonical.com        29  12   53m     +0.937      2.395    +17ms  2888us
alphyn.canonical.com       29  17   56m     +0.145      1.349   +628us  1520us
prod-ntp-4.ntp1.ps5.cano>   9   6   30m     -0.050      0.877   -255us   279us
time.cloudflare.com        29  15   60m     +0.113      0.099  -2907us   116us
125-229-162-223.hinet-ip>  22  11   28m     -0.378      0.776    +75us   418us
140.137.11.50              25  12   58m     -0.028      0.144   -166us   191us
twtpe2-ntp-002.aaplimg.c>  25  13   56m     -0.005      0.082  -1070ns    91us

厲害!

檢視上游 NTP 的存活狀態:

$ chronyc activity

輸出如下:

200 OK
8 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

都活著,厲害。

以上介紹 chrony 的小廢文結束。