Chrony是一個開源的自由軟件,它能保持系統時鍾與時鍾服務器(NTP)同步,讓時間保持精確。
它由兩個程序組成:chronyd和chronyc。
chronyd是一個后台運行的守護進程,用於調整內核中運行的系統時鍾和時鍾服務器同步。它確定計算機增減時間的比率,並對此進行補償。
chronyc提供了一個用戶界面,用於監控性能並進行多樣化的配置。它可以在chronyd實例控制的計算機上工作,也可以在一台不同的遠程計算機上工作。
安裝、配置chrony
# systemctl enable chronyd.service # systemctl restart chronyd.service # systemctl status chronyd.service 查看時間同步源: # chronyc sources -v 210 Number of sources = 1 .-- 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 || / xxxx = adjusted offset, || Log2(Polling interval) -. | yyyy = measured offset, || \ | zzzz = estimated error. || | | MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^? ys-ad1.ys.com 3 6 3 1 +12ms[ +12ms] +/- 268ms 查看時間同步源狀態: # chronyc sourcestats -v 210 Number of sources = 1 .- Number of sample points in measurement set. / .- Number of residual runs with same sign. | / .- Length of measurement set (time). | | / .- Est. clock freq error (ppm). | | | / .- Est. error in freq. | | | | / .- Est. offset. | | | | | | On the -. | | | | | | samples. \ | | | | | | | Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev ============================================================================== ys-ad1.ys.com 4 3 6 -939.214 56272.109 -4371us 5267us
配置chrony
當Chrony啟動時,它會讀取/etc/chrony.conf配置文件中的設置
參數文件說明:
# cat /etc/chrony.conf # 該參數可以多次用於添加時鍾服務器,必須以"server "格式使用。一般而言,你想添加多少服務器,就可以添加多少服務器。 server 0.rhel.pool.ntp.org iburst server 1.rhel.pool.ntp.org iburst server 2.rhel.pool.ntp.org iburst server 3.rhel.pool.ntp.org iburst # Ignore stratum in source selection. # stratumweight指令設置當chronyd從可用源中選擇同步源時,每個層應該添加多少距離到同步距離。默認情況下,設置為0,讓chronyd在選擇源時忽略源的層級。 stratumweight 0 # Record the rate at which the system clock gains/losses time. # chronyd程序的主要行為之一,就是根據實際時間計算出計算機增減時間的比率,將它記錄到一個文件中是最合理的,它會在重啟后為系統時鍾作出補償,甚至可能的話,會從時鍾服務器獲得較好的估值。 driftfile /var/lib/chrony/drift # Enable kernel RTC synchronization. # rtcsync指令將啟用一個內核模式,在該模式中,系統時間每11分鍾會拷貝到實時時鍾(RTC)。 rtcsync # In first three updates step the system clock instead of slew # if the adjustment is larger than 10 seconds. 通常,chronyd將根據需求通過減慢或加速時鍾,使得系統逐步糾正所有時間偏差。在某些特定情況下,系統時鍾可能會漂移過快,導致該調整過程消耗很長的時間來糾正系統時鍾。該指令強制chronyd在調整期大於某個閥值時步進調整系統時鍾,但只有在因為chronyd啟動時間超過指定限制(可使用負值來禁用限制),沒有更多時鍾更新時才生效。 makestep 10 3 # Allow NTP client access from local network. # 這里你可以指定一台主機、子網,或者網絡以允許或拒絕NTP連接到扮演時鍾服務器的機器。 #allow 192.168.56.6 #deny 192.168/16 # Listen for commands only on localhost. # 該指令允許你限制chronyd監聽哪個網絡接口的命令包(由chronyc執行)。該指令通過cmddeny機制提供了一個除上述限制以外可用的額外的訪問控制等級。 bindcmdaddress 127.0.0.1 bindcmdaddress ::1 # Serve time even if not synchronized to any NTP server. #local stratum 10 keyfile /etc/chrony.keys # Specify the key used as password for chronyc. commandkey 1 # Generate command key if missing. generatecommandkey # Disable logging of client accesses. noclientlog # Send a message to syslog if a clock adjustment is larger than 0.5 seconds. logchange 0.5 logdir /var/log/chrony #log measurements statistics tracking
Command Keys
在/etc/chrony.conf配置文件中,參數generatecommandkey指定了產生一個SHA1或MD5加密的密碼,存放在/etc/chrony.keys中:
# cat /etc/chrony.keys 1 MD5 HEX:BD359B2633CD6105AB8820E47A8D8EAB
密碼是"HEX:BD359B2633CD6105AB8820E47A8D8EAB",包含了前綴"HEX:"
可以自己指定密碼,比如:
# cat/etc/chrony.keys 1 xyzzy
在/etc/chrony.conf配置文件中,參數commandkey指定了密碼文件中那一條被使用。默認是1
在客戶端配置時間同步服務器
1.使用命令行工具:chronyc
2.圖形界面工具:system_config_date