Chrony是一個開源的自由軟件,它能保持系統時鍾與時鍾服務器(NTP)同步,讓時間保持精確。
它由兩個程序組成:chronyd和chronyc。
chronyd是一個后台運行的守護進程,用於調整內核中運行的系統時鍾和時鍾服務器同步。它確定計算機增減時間的比率,並對此進行補償。
chronyc提供了一個用戶界面,用於監控性能並進行多樣化的配置。它可以在chronyd實例控制的計算機上工作,也可以在一台不同的遠程計算機上工作。
Chrony時間同步程序特點:
准確的時間同步:
Chrony 是網絡時間協議的另一種實現,與網絡時間協議后台程序(ntpd)不同,它可以更快地且准確的同步系統時鍾。
Chrony 的優勢包括:
- 更快的同步只需要數分鍾而非數小時時間,從而最大程度的減少時間和頻率誤差,這對於並非全天運行的台式計算機或系統而言非常有用。
- 能夠更好的響應時間頻率的快速變化,這對於具備不穩定時鍾的虛擬機或導致時鍾頻率反生變化的節能技術而言非常有用。
- 在初始同步后,它並不會停止時鍾,以防對需要系統時間保持單調的程序造成影響。
- 在應對臨時非對稱延遲時,(例如,大規模下載造成鏈接飽和時)提供了更好的穩定性。
- 無需對服務器進行定期輪詢,因此具備間歇性網絡連接的系統仍然可以快速同步時鍾。
一、配置時間服務器
1、軟件部署
[root@ntp ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@ntp ~]# yum -y install chrony
2、配置文件
[root@ntp ~]# cat /etc/chrony.conf //配置文件 # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html).
//該參數可以多次用於添加時鍾服務器,必須以"server "格式使用。一般而言,你想添加多少服務器,就可以添加多少服務器。 #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
#server edu.ntp.org.cn iburst
#server pool.ntp.org iburst server news.neu.edu.cn iburst //時間同步設置(外網)
server 202.120.2.101 iburst //上海交通大學網絡中心NTP服務器地址,也可以寫成域名:ntp.sjtu.edu.cn # Record the rate at which the system clock gains/losses time.
//chronyd程序的主要行為之一,就是根據實際時間計算出計算機增減時間的比率,將它記錄到一個文件中是最合理的,它會在重啟后為系統時鍾作出補償,甚至可能的話,會從時鍾服務器獲得較好的估值。
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.
//通常,chronyd將根據需求通過減慢或加速時鍾,使得系統逐步糾正所有時間偏差。在某些特定情況下,系統時鍾可能會漂移過快,導致該調整過程消耗很長的時間來糾正系統時鍾。該指令強制chronyd在調整期大於某個閥值時步進調整系統時鍾,但只有在因為chronyd啟動時間超過指定限制(可使用負值來禁用限制),沒有更多時鍾更新時才生效。
makestep 1.0 3
# Enable kernel synchronization of the real-time clock (RTC).
//rtcsync指令將啟用一個內核模式,在該模式中,系統時間每11分鍾會拷貝到實時時鍾(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.
//這里你可以指定一台主機、子網,或者網絡以允許或拒絕NTP連接到扮演時鍾服務器的機器。
allow 192.168.10.0/24 //允許同步的網段
# Serve time even if not synchronized to a time source.
local stratum 10
# 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
3、啟動服務
[root@ntp ~]# systemctl start chronyd.service //啟動服務
[root@ntp ~]# systemctl enable chronyd.service //設置開機自啟動
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
[root@ntp ~]# systemctl status chronyd.service
● chronyd.service - NTP client/server //查看當前狀態
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2018-01-02 18:57:34 CST; 2h 1min ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Main PID: 1695 (chronyd)
CGroup: /system.slice/chronyd.service
└─1695 /usr/sbin/chronyd
Jan 02 18:57:34 director.contoso.com systemd[1]: Starting NTP client/server...
Jan 02 18:57:34 director.contoso.com chronyd[1695]: chronyd version 3.1 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SECHASH ...+DEBUG)
Jan 02 18:57:34 director.contoso.com systemd[1]: Started NTP client/server.
Jan 02 18:57:38 director.contoso.com chronyd[1695]: Selected source 192.168.10.8
Hint: Some lines were ellipsized, use -l to show in full.
[root@ntp ~]# ss -tulp | grep chronyd
udp UNCONN 0 0 *:ntp *:* users:(("chronyd",pid=1695,fd=3))
udp UNCONN 0 0 127.0.0.1:323 *:* users:(("chronyd",pid=1695,fd=1))
udp UNCONN 0 0 ::1:323 :::* users:(("chronyd",pid=1695,fd=2))
3、驗證服務
1 [root@ntp ~]# chronyc sources -v //查看時間同步源: 2 210 Number of sources = 1 3 4 .-- Source mode '^' = server, '=' = peer, '#' = local clock. 5 / .- Source state '*' = current synced, '+' = combined , '-' = not combined, 6 | / '?' = unreachable, 'x' = time may be in error, '~' = time too variable. 7 || .- xxxx [ yyyy ] +/- zzzz 8 || Reachability register (octal) -. | xxxx = adjusted offset, 9 || Log2(Polling interval) --. | | yyyy = measured offset, 10 || \ | | zzzz = estimated error. 11 || | | \ 12 MS Name/IP address Stratum Poll Reach LastRx Last sample 13 =============================================================================== 14 ^? ns.bjnet-pku.edu.cn 0 6 0 - +0ns[ +0ns] +/- 0ns 15 [root@ntp ~]# ping -c 2 s2m.time.edu.cn 16 PING s2m.time.edu.cn (202.112.7.13) 56(84) bytes of data. 17 64 bytes from ns.pku.cn (202.112.7.13): icmp_seq=1 ttl=237 time=36.9 ms 18 64 bytes from ns.pku.cn (202.112.7.13): icmp_seq=2 ttl=237 time=36.9 ms 20 --- s2m.time.edu.cn ping statistics --- 21 2 packets transmitted, 2 received, 0% packet loss, time 1001ms 22 rtt min/avg/max/mdev = 36.927/36.928/36.929/0.001 ms
[root@ntp ~]# 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
==============================================================================
202.120.2.101.dns.sjtu.e> 0 0 0 +0.000 2000.000 +0ns 4000ms
news.neu.edu.cn 0 0 0 +0.000 2000.000 +0ns 4000ms
二、其它客戶端配置
1、軟件部署
[root@CentOS7 ~]# yum -y install chrony
2、配置文件
[root@CentOS7 ~]# egrep -v "^#|^$" /etc/chrony.conf server 192.168.10.8 iburst //同步時間服務器 stratumweight 0 driftfile /var/lib/chrony/drift rtcsync makestep 10 3 bindcmdaddress 127.0.0.1 bindcmdaddress ::1 keyfile /etc/chrony.keys commandkey 1 generatecommandkey noclientlog logchange 0.5 logdir /var/log/chrony
3、啟動服務
[root@CentOS7 ~]# systemctl start chronyd.service [root@CentOS7 ~]# systemctl enable chronyd.service Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
4、驗證服務
[root@CentOS7 ~]# ntpdate 192.168.10.8 2 Jan 21:39:34 ntpdate[43563]: adjust time server 192.168.10.8 offset -0.000041 sec [root@CentOS7 ~]# 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 ============================================================================== 192.168.10.8 14 8 118m +0.002 0.023 +1305ns 48us [root@CentOS7 ~]#
4、設置任務計划
[root@CentOS7 ~]# crontab -e //編輯crontab文件 [root@CentOS7 ~]# crontab -l //查看任務計划 #synchronization time //每隔五分鍾進行同步一次 */5 * * * * /usr/sbin/ntpdate 192.168.10.8 > /dev/null 2>&1
三、相關概念及命令介紹
1、GMT、UTC、CST、DST 時間
- UTC 整個地球分為二十四時區,每個時區都有自己的本地時間。在國際無線電通信場合,為了統一起見,使用一個統一的時間,稱為通用協調時(UTC, Universal Time Coordinated)。
- GMT 格林威治標准時間 (Greenwich Mean Time)指位於英國倫敦郊區的皇家格林尼治天文台的標准時間,因為本初子午線被定義在通過那里的經線。(UTC與GMT時間基本相同,本文中不做區分)
- CST 中國標准時間 (China Standard Time)
GMT + 8 = UTC + 8 = CST - DST夏令時(Daylight Saving Time) 指在夏天太陽升起的比較早時,將時間撥快一小時,以提早日光的使用。(中國不使用)
2、硬件時間和系統時間
- 硬件時間
RTC(Real-Time Clock)或CMOS時間,一般在主板上靠電池供電,服務器斷電后也會繼續運行。僅保存日期時間數值,無法保存時區和夏令時設置。
- 系統時間
一般在服務器啟動時復制RTC時間,之后獨立運行,保存了時間、時區和夏令時設置。
3、timedatectl 命令
- 顯示系統的當前時間和日間
timedatectl # timedatectl status # 兩條命令效果等同
顯示系統的當前時間和日間
timedatectl set-time "YYYY-MM-DD HH:MM:SS" timedatectl set-time "YYYY-MM-DD" timedatectl set-time "HH:MM:SS"
- 查看所有可用的時區
timedatectl list-timezones # 亞洲 timedatectl list-timezones | grep -E "Asia/S.*"
- 設置時區
timedatectl set-timezone Asia/Shanghai
- 設置硬件時間
# 硬件時間默認為UTC timedatectl set-local-rtc 1 # hwclock --systohc --localtime # 兩條命令效果等同
- 啟用時間同步
timedatectl set-ntp yes # yes或no; 1或0也可以