chrony 詳解


chrony 詳解

一、 簡介

Chrony是一個開源自由的網絡時間協議 NTP 的客戶端和服務器軟軟件。它能讓計算機保持系統時鍾與時鍾服務器(NTP)同步,因此讓你的計算機保持精確的時間,Chrony也可以作為服務端軟件為其他計算機提供時間同步服務。

Chrony由兩個程序組成,分別是chronydchronyc

chronyd是一個后台運行的守護進程,用於調整內核中運行的系統時鍾和時鍾服務器同步。它確定計算機增減時間的比率,並對此進行補償。
chronyc提供了一個用戶界面,用於監控性能並進行多樣化的配置。它可以在chronyd實例控制的計算機上工作,也可以在一台不同的遠程計算機上工作。

NTP 是網絡時間協議(Network Time Protocol)的簡稱,通過 udp 123 端口進行網絡時鍾同步。
RHEL7中默認使用chrony作為時間服務器,也支持NTP,需要額外安裝。
NTP與chrony不能同時存在,只能用其中一個,並將另一個mask掉。


一、安裝與配置

#yum -y install chrony
#systemctl enable chronyd
#systemctl start chronyd

chrony.conf 默認配置

Chrony的配置文件是/etc/chrony.conf

# 使用 pool.ntp.org 項目中的公共服務器。以server開,理論上想添加多少時間服務器都可以。
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

# 根據實際時間計算出服務器增減時間的比率,然后記錄到一個文件中,在系統重啟后為系統做出最佳時間補償調整。
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# 如果系統時鍾的偏移量大於1秒,則允許系統時鍾在前三次更新中步進。
# Allow the system clock to be stepped in the first three updates if its offset is larger than 1 second.
makestep 1.0 3

# 啟用實時時鍾(RTC)的內核同步。
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# 通過使用 hwtimestamp 指令啟用硬件時間戳
# 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

# 指定 NTP 客戶端地址,以允許或拒絕連接到扮演時鍾服務器的機器
# Allow NTP client access from local network.
#allow 192.168.0.0/16

# Serve time even if not synchronized to a time source.
#local stratum 10

# 指定包含 NTP 身份驗證密鑰的文件。
# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# 指定日志文件的目錄。
# Specify directory for log files.
logdir /var/log/chrony

# 選擇日志文件要記錄的信息。
# Select which information is logged.
#log measurements statistics tracking

二、使用

1.服務端配置

chrony.conf 修改兩處

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server s1a.time.edu.cn iburst
server ntp.aliyun.com iburst

# Allow NTP client access from local network.
allow 192.168.0.0/16

設置開機啟動,重啟服務

systemctl enable chronyd
systemctl restart chronyd

# 查看時間同步狀態
timedatectl status
# 開啟網絡時間同步
timedatectl set-ntp true

2.客戶端配置

chrony.conf 修改兩處

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 192.168.8.5 iburst

# Allow NTP client access from local network.
allow 192.168.8.5

設置開機啟動,重啟服務

systemctl enable chronyd
systemctl restart chronyd

查看時間同步狀態

timedatectl status

開啟網絡時間同步

timedatectl set-ntp true

三、chronyc 命令

查看 ntp_servers

chronyc sources -v

查看 ntp_servers 狀態

chronyc sourcestats -v

查看 ntp_servers 是否在線

chronyc activity -v

查看 ntp 詳細信息

chronyc tracking -v

強制同步下系統時鍾

chronyc -a makestep

四、timedatectl 命令

查看日期時間、時區及 NTP 狀態

timedatectl

查看時區列表

timedatectl list-timezones
timedatectl list-timezones |  grep  -E "Asia/S.*"

修改時區

timedatectl set-timezone Asia/Shanghai

修改日期時間(可以只修改其中一個)

timedatectl set-time "2021-04-15 15:50:20"

開啟 NTP

timedatectl set-ntp true/flase

五、授時中心

210.72.145.44 國家授時中心
ntp.aliyun.com 阿里雲
s1a.time.edu.cn 北京郵電大學
s1b.time.edu.cn 清華大學
s1c.time.edu.cn 北京大學
s1d.time.edu.cn 東南大學
s1e.time.edu.cn 清華大學
s2a.time.edu.cn 清華大學
s2b.time.edu.cn 清華大學
s2c.time.edu.cn 北京郵電大學
s2d.time.edu.cn 西南地區網絡中心
s2e.time.edu.cn 西北地區網絡中心
s2f.time.edu.cn 東北地區網絡中心
s2g.time.edu.cn 華東南地區網絡中心
s2h.time.edu.cn 四川大學網絡管理中心
s2j.time.edu.cn 大連理工大學網絡中心
s2k.time.edu.cn CERNET桂林主節點
s2m.time.edu.cn 北京大學
ntp.sjtu.edu.cn 202.120.2.101 上海交通大學

六、系統時鍾與硬件時鍾之間同步

設置硬件時鍾
-w,--systohc

hwclock -w

設置系統時鍾
-s, --hctosys

hwclock -s

修改配置文件方式
vim /etc/sysconfig/ntpd
將系統時間寫入BIOS,與 hwclock -w 效果相同

SYNC_HWCLOCK=yes

七、常見時區

(1)UTC 整個地球分為二十四時區,每個時區都有自己的本地時間。在國際無線電通信場合,為了統一起見,使用一個統一的時間,稱為通用協調時(UTC, Universal Time Coordinated)。

(2)GMT  格林威治標准時間 (Greenwich Mean Time)指位於英國倫敦郊區的×××格林尼治天文台的標准時間,因為本初子午線被定義在通過那里的經線。(UTC與GMT時間基本相同,本文中不做區分)

(3)CST  中國標准時間 (China Standard Time)
GMT + 8 = UTC + 8 = CST

(4)DST夏令時(Daylight Saving Time) 指在夏天太陽升起的比較早時,將時間撥快一小時,以提早日光的使用。(中國不使用)


八、實例

[root@rhel7.9 ~]# timedatectl set-time "2020-02-23 12:23:01"       <<========設置系統時間,因為開啟了時間同步所以報錯
Failed to set time: Automatic time synchronization is enabled
[root@rhel7.9 ~]# systemctl stop chronyd
[root@rhel7.9 ~]# timedatectl set-time "2020-02-23 12:23:01"         <<==========stop chronyd 后修改系統時間,報錯依舊
Failed to set time: Automatic time synchronization is enabled
[root@rhel7.9 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since 四 2021-04-15 15:45:37 CST; 21s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 13722 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 13716 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 13720 (code=exited, status=0/SUCCESS)

4月 15 15:40:54 rhel7.9 systemd[1]: Starting NTP client/server...
4月 15 15:40:54 rhel7.9 chronyd[13720]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
4月 15 15:40:54 rhel7.9 chronyd[13720]: Frequency 8.664 +/- 0.118 ppm read from /var/lib/chrony/drift
4月 15 15:40:54 rhel7.9 systemd[1]: Started NTP client/server.
4月 15 15:41:02 rhel7.9 chronyd[13720]: Selected source 119.28.206.193
4月 15 15:43:16 rhel7.9 chronyd[13720]: Source 124.108.20.1 replaced with 84.16.67.12
4月 15 15:45:37 rhel7.9 systemd[1]: Stopping NTP client/server...
4月 15 15:45:37 rhel7.9 chronyd[13720]: chronyd exiting
4月 15 15:45:37 rhel7.9 systemd[1]: Stopped NTP client/server.
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# date
2021年 04月 15日 星期四 15:46:13 CST
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# timedatectl set-ntp false
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# timedatectl set-time "2020-02-23 12:23:01"
[root@rhel7.9 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:chronyd(8)
           man:chrony.conf(5)

4月 15 15:40:34 rhel7.9 systemd[1]: Stopped NTP client/server.
4月 15 15:40:54 rhel7.9 systemd[1]: Starting NTP client/server...
4月 15 15:40:54 rhel7.9 chronyd[13720]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
4月 15 15:40:54 rhel7.9 chronyd[13720]: Frequency 8.664 +/- 0.118 ppm read from /var/lib/chrony/drift
4月 15 15:40:54 rhel7.9 systemd[1]: Started NTP client/server.
4月 15 15:41:02 rhel7.9 chronyd[13720]: Selected source 119.28.206.193
4月 15 15:43:16 rhel7.9 chronyd[13720]: Source 124.108.20.1 replaced with 84.16.67.12
4月 15 15:45:37 rhel7.9 systemd[1]: Stopping NTP client/server...
4月 15 15:45:37 rhel7.9 chronyd[13720]: chronyd exiting
4月 15 15:45:37 rhel7.9 systemd[1]: Stopped NTP client/server.
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# timedatectl status          <<============ 顯示當前系統和RTC設置
      Local time: 日 2020-02-23 12:23:39 CST
  Universal time: 日 2020-02-23 04:23:39 UTC
        RTC time: 日 2020-02-23 04:23:39
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@rhel7.9 ~]# timedatectl set-ntp true
[root@rhel7.9 ~]# timedatectl status      
      Local time: 日 2020-02-23 12:24:14 CST
  Universal time: 日 2020-02-23 04:24:14 UTC
        RTC time: 日 2020-02-23 04:24:14
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@rhel7.9 ~]# systemctl start chronyd
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# timedatectl status
      Local time: 四 2021-04-15 15:48:52 CST
  Universal time: 四 2021-04-15 07:48:52 UTC
        RTC time: 日 2020-02-23 04:24:44
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
[root@rhel7.9 ~]# timedatectl set-time "2020-02-23 12:23:01"
Failed to set time: Automatic time synchronization is enabled
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# timedatectl set-ntp false          <<============= 禁用基於NTP的網絡時間同步           
[root@rhel7.9 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:chronyd(8)
           man:chrony.conf(5)

2月 23 12:24:12 rhel7.9 systemd[1]: Starting NTP client/server...
2月 23 12:24:12 rhel7.9 chronyd[15212]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
2月 23 12:24:12 rhel7.9 chronyd[15212]: Frequency 8.646 +/- 0.520 ppm read from /var/lib/chrony/drift
2月 23 12:24:12 rhel7.9 systemd[1]: Started NTP client/server.
2月 23 12:24:18 rhel7.9 chronyd[15212]: Selected source 84.16.67.12
2月 23 12:24:18 rhel7.9 chronyd[15212]: System clock wrong by 36041047.965352 seconds, adjustment started
4月 15 15:48:26 rhel7.9 chronyd[15212]: System clock was stepped by 36041047.965352 seconds
4月 15 15:51:05 rhel7.9 chronyd[15212]: chronyd exiting
4月 15 15:51:05 rhel7.9 systemd[1]: Stopping NTP client/server...
4月 15 15:51:05 rhel7.9 systemd[1]: Stopped NTP client/server.
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# timedatectl set-time "2020-02-23 12:23:01"      <<=========== 再次設置時間成功
[root@rhel7.9 ~]# timedatectl set-ntp true      <<============ 啟用基於NTP的網絡時間同步
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# systemctl status chronyd                  
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since 日 2020-02-23 12:23:25 CST; 4s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 16110 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 16103 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 16105 (chronyd)
    Tasks: 1
   CGroup: /system.slice/chronyd.service
           └─16105 /usr/sbin/chronyd

2月 23 12:23:25 rhel7.9 systemd[1]: Starting NTP client/server...
2月 23 12:23:25 rhel7.9 chronyd[16105]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
2月 23 12:23:25 rhel7.9 chronyd[16105]: Frequency 8.645 +/- 0.714 ppm read from /var/lib/chrony/drift
2月 23 12:23:25 rhel7.9 systemd[1]: Started NTP client/server.
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# date
2021年 04月 15日 星期四 15:52:14 CST

在使用timedatectl 設置系統時間時間必須停止NTP時間同步

chronyc sources 輸出結果解析

M

 這表示信號源的模式。^表示服務器,=表示對等方,表示本地連接的參考時鍾。

S

 此列指示源的狀態。

  •  		* 表示chronyd當前同步到的源。
    
  •  		+ 表示可接受的信號源,與選定的信號源組合在一起。
    
  •  		- 表示被合並算法排除的可接受源。
    
  •  		? 指示已失去連接性或其數據包未通過所有測試的源。它也顯示在啟動時,直到從中至少收集了3個樣本為止。
    
  •  		x 表示chronyd認為是虛假行情的時鍾(即,其時間與大多數其他來源不一致)。
    
  •  		〜 表示時間似乎具有太多可變性的來源。
    

Name/IP address

這顯示了源的名稱或IP地址,或參考時鍾的參考ID。

Stratum

這顯示了來源的層,如其最近收到的樣本中所報告的那樣。層1表示一台具有本地連接的參考時鍾的計算機。與第1層計算機同步的計算機位於第2層。與第2層計算機同步的計算機位於第3層,依此類推。

Poll

這顯示輪詢源的速率,以秒為單位的時間間隔的以2為底的對數。因此,值為6表示每64秒進行一次測量。chronyd會根據當前情況自動更改輪詢速率。

Reach

這顯示了源的可達性寄存器以八進制數字打印。寄存器有8位,並在每個從源接收或丟失的數據包上更新。值377表示從最后八次傳輸中收到了對所有用戶的有效答復。

LastRx

此列顯示多長時間前從來源接收到了最后一個好的樣本(在下一列中顯示)。未通過某些測試的測量將被忽略。通常以秒為單位。字母mhdy表示分鍾,小時,天或年。

Last sample

此列顯示上次測量時本地時鍾與源之間的偏移。方括號中的數字表示實際測得的偏移量。可以用ns(表示納秒),us (表示微秒),ms(表示毫秒)或s(表示秒)作為后綴。方括號左側的數字表示原始測量值,已調整為允許此后施加於本地時鍾的任何擺度。

+/-指示器后面的數字表示測量中的誤差范圍。正偏移表示本地時鍾位於源時鍾之前。

[root@rhel7.9 ~]# chronyc add server 10.210.57.78
200 OK
[root@rhel7.9 ~]# chronyc sources -v
210 Number of sources = 5

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- 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               
===============================================================================
^- ntp1.ams1.nl.leaseweb.net     3  10   377   651  -4572us[-4572us] +/-  227ms
^- 119.29.13.98                  2   9   377    17  +2553us[+2553us] +/-   26ms
^* 203.107.6.88                  2  10   377   656  -1432us[-1106us] +/-   19ms
^- tock.ntp.infomaniak.ch        1   9   377   52m    -16ms[  -16ms] +/-  103ms
^? 10.211.57.78                  3   6     1    10  +1089us[+1089us] +/-   34ms
[root@rhel7.9 ~]# chronyc sources -v
210 Number of sources = 5

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- 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               
===============================================================================
^- ntp1.ams1.nl.leaseweb.net     3  10   377   843  -4572us[-4572us] +/-  227ms
^- 119.29.13.98                  2   9   377   208  +2553us[+2553us] +/-   26ms
^* 203.107.6.88                  2  10   377   847  -1432us[-1106us] +/-   19ms
^- tock.ntp.infomaniak.ch        1   9   377   56m    -16ms[  -16ms] +/-  103ms
^- 10.211.57.78                  3   6    17     6  +1225us[+1225us] +/-   37ms

上面示例中剛加入的NTP部分顯示是 ?即不可達的,過了兩分鍾再查看狀態變成了 -


https://www.cnblogs.com/jhxxb/p/11526098.html

https://www.cnblogs.com/jhxxb/p/10579816.html

https://chrony.tuxfamily.org/doc/3.5/chronyc.html

https://www.aboutyun.com//forum.php/?mod=viewthread&tid=11395&extra=page%3D1&page=1&


免責聲明!

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



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