一、chrony簡(jiǎn)介
chrony是一款開源的NTP時(shí)間同步軟件,主要用于Unix-like計(jì)算機(jī)系統(tǒng)(如Linux、BSD和macOS)來提供高精度的時(shí)間同步。Chrony的設(shè)計(jì)注重性能和可靠性,具有低延遲、低CPU占用率、低內(nèi)存消耗等優(yōu)點(diǎn)。chrony由兩個(gè)程序組成,chronyd(服務(wù)器端)和chronyc(客戶端)
chronyd:后臺(tái)運(yùn)行的守護(hù)進(jìn)程,用于調(diào)整內(nèi)核中運(yùn)行的系統(tǒng)時(shí)鐘和時(shí)鐘服務(wù)器同步,它確定了計(jì)算機(jī)增減時(shí)間的比率,并對(duì)此進(jìn)行平滑調(diào)整
chronyc:提供用戶界面,用于監(jiān)控性能并進(jìn)行多樣化的配置??稍赾hronyd實(shí)例控制的計(jì)算機(jī)上工作,也可在一臺(tái)不同的遠(yuǎn)程計(jì)算機(jī)工作
二、實(shí)驗(yàn)環(huán)境
2.1 實(shí)驗(yàn)拓?fù)?/p>
如圖,兩臺(tái)服務(wù)器Server1、Server2連接到同一網(wǎng)絡(luò)

2.2 本地環(huán)境規(guī)劃
| 主機(jī)名 | IP地址 | 操作系統(tǒng)版本 | 內(nèi)核版本 |
| Server1 | 172.16.0.134/24 | Rocky Linux 8.9 (Green Obsidian) | Linux 4.18.0-513.5.1.el8_9.x86_64 |
| Server2 | 172.16.0.150/24 | Rocky Linux 8.9 (Green Obsidian) | Linux 4.18.0-513.5.1.el8_9.x86_64 |
Net1網(wǎng)絡(luò)中配置了NAT(網(wǎng)絡(luò)地址轉(zhuǎn)換),兩臺(tái)服務(wù)器均可以訪問公網(wǎng)
三、配置時(shí)間同步
3.1 安裝chrony
[root@Server1 ~]# rpm -qa | grep chrony chrony-4.2-1.el8.rocky.1.0.x86_64
Rokcy Linux 8.9版本默認(rèn)安裝了chrony,驗(yàn)證安裝狀態(tài):
3.2 配置Server1從公網(wǎng)時(shí)間源服務(wù)器同步時(shí)間
編輯“/etc/chrony.conf”文件:
[root@Server1 ~]# vim /etc/chrony.conf # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). # pool 2.rocky.pool.ntp.org iburst # 注釋該行,添加如下兩行,理論上想添加多少時(shí)間服務(wù)器都可以 Server ntp.aliyun.com iburst # 阿里時(shí)間源服務(wù)器 Server time1.cloud.tencent.com iburst # 騰訊時(shí)間源服務(wù)器
重啟chronyd服務(wù),并設(shè)置開機(jī)自啟:
[root@Server1 ~]# systemctl restart chronyd [root@Server1 ~]# systemctl enable chronyd [root@Server1 ~]# systemctl is-active chronyd # 查看chronyd服務(wù)是否正常運(yùn)行 active [root@Server1 ~]#
查看時(shí)間同步狀態(tài):
[root@Server1 ~]# chronyc sources MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 203.107.6.88 2 6 37 63 +49us[+2956us] +/- 23ms ^- 106.55.184.199 2 6 127 60 +1891us[+1891us] +/- 47ms [root@Server1 ~]# [root@Server1 ~]# chronyc sources -v .-- Source mode '^' = server, '=' = peer, '#' = local clock. / .- Source state '*' = current best, '+' = combined, '-' = not combined, | / 'x' = may be in error, '~' = too variable, '?' = unusable. || .- xxxx [ yyyy ] +/- zzzz || Reachability register (octal) -. | xxxx = adjusted offset, || Log2(Polling interval) --. | | yyyy = measured offset, || | | zzzz = estimated error. || | | MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 203.107.6.88 2 6 77 3 -3557us[-3824us] +/- 26ms ^+ 106.55.184.199 2 6 257 1 +2021us[+2021us] +/- 46ms [root@Server1 ~]#
chronyc sources輸出結(jié)果解析:
<1> M
這表示信號(hào)源的模式。^表示服務(wù)器,=表示對(duì)等方,#表示本地連接的參考時(shí)鐘。
<2> S
此列指示源的狀態(tài)。
* 表示chronyd當(dāng)前同步到的源。
+ 表示可接受的信號(hào)源,與選定的信號(hào)源組合在一起。
- 表示被合并算法排除的可接受源。
?指示已失去連接性或其數(shù)據(jù)包未通過所有測(cè)試的源。它也顯示在啟動(dòng)時(shí),直到從中至少收集了3個(gè)樣本為止。
x 表示chronyd認(rèn)為是虛假行情的時(shí)鐘(即,其時(shí)間與大多數(shù)其他來源不一致)
? 表示時(shí)間似乎具有太多可變性的來源。
<3> Name/IP address
這顯示了源的名稱或IP地址,或參考時(shí)鐘的參考ID。
<4> Stratum
這顯示了來源的層,如其最近收到的樣本中所報(bào)告的那樣。層1表示一臺(tái)具有本地連接的參考時(shí)鐘的計(jì)算機(jī)。與第1層計(jì)算機(jī)同步的計(jì)算機(jī)位于第2層。與第2層計(jì)算機(jī)同步的計(jì)算機(jī)位于第3層,依此類推。
<5> Poll
這顯示輪詢?cè)吹乃俾?,以秒為單位的時(shí)間間隔的以2為底的對(duì)數(shù)。因此,值為6表示每64秒進(jìn)行一次測(cè)量。chronyd會(huì)根據(jù)當(dāng)前情況自動(dòng)更改輪詢速率。
<6> Reach
這顯示了源的可達(dá)性寄存器以八進(jìn)制數(shù)字打印。寄存器有8位,并在每個(gè)從源接收或丟失的數(shù)據(jù)包上更新。值377表示從最后八次傳輸中收到了對(duì)所有用戶的有效答復(fù)。
<7> LastRx
此列顯示多長(zhǎng)時(shí)間前從來源接收到了最后一個(gè)好的樣本(在下一列中顯示)。未通過某些測(cè)試的測(cè)量將被忽略。通常以秒為單位。字母m,h,d或y表示分鐘,小時(shí),天或年。
<8> Last sample
此列顯示上次測(cè)量時(shí)本地時(shí)鐘與源之間的偏移。方括號(hào)中的數(shù)字表示實(shí)際測(cè)得的偏移量??梢杂胣s(表示納秒),us (表示微秒),ms(表示毫秒)或s(表示秒)作為后綴。方括號(hào)左側(cè)的數(shù)字表示原始測(cè)量值,已調(diào)整為允許此后施加于本地時(shí)鐘的任何擺度。
<9> +/-指示器后面的數(shù)字表示測(cè)量中的誤差范圍。正偏移表示本地時(shí)鐘位于源時(shí)鐘之前。
例如:現(xiàn)有時(shí)鐘服務(wù)器time_server:它的層級(jí)是2,一臺(tái)主機(jī)node1從time_server同步時(shí)間,那么node1主機(jī)的層級(jí)就是3,以此類推另一臺(tái)在node1同步時(shí)間,那它的層級(jí)就是4。

3.3 配置Server2從Server1時(shí)間源同步時(shí)間
Server1上再次編輯“/etc/chrony.conf”文件:
[root@Server1 ~]# vim /etc/chrony.conf # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). # pool 2.rocky.pool.ntp.org iburst Server ntp.aliyun.com iburst Server time1.cloud.tencent.com iburst # Record the rate at which the system clock gains/losses time. driftfile /var/lib/chrony/drift # Allow the system clock to be stepped in the first three updates # if its offset is larger than 1 second. makestep 1.0 3 # Enable kernel synchronization of the real-time clock (RTC). rtcsync # Enable hardware timestamping on all interfaces that support it. #hwtimestamp * # Increase the minimum number of selectable sources required to adjust # the system clock. #minsources 2 # Allow NTP client access from local network. #allow 192.168.0.0/16 allow 172.16.0.0/24 # 配置訪問規(guī)則,僅允許該網(wǎng)絡(luò)的客戶端訪問 # Serve time even if not synchronized to a time source. #local stratum 10 local stratum 10 # 即使未同步到時(shí)間源,也要提供時(shí)間同步服務(wù),當(dāng)前系統(tǒng)層級(jí)為10 # Specify file containing keys for NTP authentication. keyfile /etc/chrony.keys # Get TAI-UTC offset and leap seconds from the system tz database. leapsectz right/UTC # Specify directory for log files. logdir /var/log/chrony # Select which information is logged. #log measurements statistics tracking
重啟chronyd服務(wù):
[root@Server1 ~]# systemctl restart chronyd
防火墻放行ntp服務(wù):
[root@Server1 ~]# firewall-cmd --add-service=ntp --permanent success [root@Server1 ~]# firewall-cmd --reload success
Server2上編輯“/etc/chrony.conf”文件:
[root@Server2 ~]# vim /etc/chrony.conf # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). # pool 2.rocky.pool.ntp.org iburst # 注釋該行,添加下面一行 Server 172.16.0.134 iburst
重啟chronyd服務(wù),并設(shè)置開機(jī)自啟:
[root@Server2 ~]# systemctl restart chronyd [root@Server2 ~]# systemctl enable chronyd
查看時(shí)間同步狀態(tài):

審核編輯:湯梓紅
-
Linux
+關(guān)注
關(guān)注
88文章
11822瀏覽量
219600 -
計(jì)算機(jī)
+關(guān)注
關(guān)注
19文章
7841瀏覽量
93492 -
操作系統(tǒng)
+關(guān)注
關(guān)注
37文章
7443瀏覽量
129643 -
開源
+關(guān)注
關(guān)注
3文章
4368瀏覽量
46464
原文標(biāo)題:Linux配置時(shí)間同步 - chrony
文章出處:【微信號(hào):magedu-Linux,微信公眾號(hào):馬哥Linux運(yùn)維】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
NTP協(xié)議簡(jiǎn)介
通過NTP獲取Epoch/Unix時(shí)間NTP代表網(wǎng)絡(luò)時(shí)間
求解關(guān)于AT ESP8266 無法實(shí)現(xiàn)NTP時(shí)間同步的問題?
NTP網(wǎng)絡(luò)時(shí)間服務(wù)器在大型局域網(wǎng)的同步技術(shù)
NTP服務(wù)器配置概述
docker部署NTP時(shí)間服務(wù)器chrony
Linux上怎么配置NTP時(shí)間同步呢?
ntp的同步原理是什么
京準(zhǔn)電鐘 | NTP網(wǎng)絡(luò)時(shí)間同步協(xié)議原理及其應(yīng)用介紹
NTP服務(wù)器配置步驟 NTP服務(wù)器服務(wù)的好處
常見NTP服務(wù)器軟件比較
NTP時(shí)間同步服務(wù)器如何工作
Chrony高精度時(shí)間同步配置
NTP時(shí)間同步技術(shù):保證網(wǎng)絡(luò)時(shí)間精確無比
NTP時(shí)間同步軟件chrony簡(jiǎn)介和配置方法
評(píng)論