Chrony是NTP(Network Time Protocol,網絡時間協議,服務器時間同步的一種協議)的另一種實現,與ntpd不同,它可以更快且更準確地同步系統時鐘,最大程度的減少時間和頻率誤差。
在CentOS6和CentOS7中,默認是使用ntp來同步時間的,但ntp同步時間並不理想,有可能需要數小時來同步時間。而且ntp也已經很老了。到了CentOS 8已經不在支持NTP服務了。chrony並且兼容ntpd監聽在udp123端口上,自己則監聽在udp的323端口上。
如果在chrony配置文件中指定了ntp服務器的地址,那麼chrony就是一臺客戶端,會去同步ntp服務器的時間,如果在chrony配置了允許某些客戶端來向自己同步時間,則chrony也充當了一臺服務器,所以,安裝了chrony即可充當客戶端也可以充當服務端。
chrony 的優勢:
(1)更快的同步,從而最大程度減少了時間和頻率誤差,對於並非全天 24 小時運行的虛擬計算機而言非常有用
能夠更好地響應時鐘頻率的快速變化,對於具備不穩定時鐘的虛擬機或導致時鐘頻率發生變化的節能技術而言非常有用
(2)在初始同步後,它不會停止時鐘,以防對需要系統時間保持單調的應用程序造成影響
(3)在應對臨時非對稱延遲時(例如,在大規模下載造成鏈接飽和時)提供了更好的穩定性
(4)無需對服務器進行定期輪詢,因此具備間歇性網絡連接的系統仍然可以快速同步時鐘
一、環境準備:
服務端(server):Linux CostOS 8.2 , IP:192.168.1.150
客戶端(client) : Linux CostOS 8.2 ,IP:192.168.1.151
工 具:VMware Workstation 12.5 Xshell 7
二、配置要求:
在Linux CentOS8.2上配置Chrony時間服務器,使得192.168.1.0/24網絡中的其它計算機通過此服務器進行網絡校時;拒絕192.168.2.0/24網絡中的計算機通過此服務器進行網絡校時。
1. CentOS8系統中,原有的時間同步服務 ntp/ntpdate服務已經無法使用,使用yum安裝,提示已不存在
[root@mzzz ~]# yum install ntp
Last metadata expiration check: 1:18:05 ago on Sun 03 Jan 2021 01:13:11 PM CST.
No match for argument: ntp
Error: Unable to find a match: ntp
2. 在CentOS8中,已使用chrony替代ntp,首先安裝chrony
[root@mzzz ~]# yum install chrony #二選其一安裝
[root@mzzz ~]# dnf install chrony #二選其一安裝
Last metadata expiration check: 1:19:57 ago on Sun 03 Jan 2021 01:13:11 PM CST.
Package chrony-3.5-1.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
提示已經安裝,CentOS 8已經默認安裝了chrony
3. 修改chrony配置文件
[root@mzzz ~]# vim /etc/chrony.conf
1 # Use public servers from the pool.ntp.org project.
2 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
3 #pool 2.centos.pool.ntp.org iburst ##註釋這一行
4 server 192.168.1.150 iburst ##增加這一行
5 # Record the rate at which the system clock gains/losses time.
6 driftfile /var/lib/chrony/drift
7
8 # Allow the system clock to be stepped in the first three updates
9 # if its offset is larger than 1 second.
10 makestep 1.0 3
11
12 # Enable kernel synchronization of the real-time clock (RTC).
13 rtcsync
14
15 # Enable hardware timestamping on all interfaces that support it.
16 #hwtimestamp *
17
18 # Increase the minimum number of selectable sources required to adjust
19 # the system clock.
20 #minsources 2
21
22 # Allow NTP client access from local network.
23 allow 192.168.1.0/24 #增加這一行
24 deny 192.168.2.0/24 #增加這一行
25 # Serve time even if not synchronized to a time source.
26 local stratum 10 #去掉註釋
27
28 # Specify file containing keys for NTP authentication.
29 keyfile /etc/chrony.keys
30
31 # Get TAI-UTC offset and leap seconds from the system tz database.
32 leapsectz right/UTC
33
34 # Specify directory for log files.
35 logdir /var/log/chrony
36
37 # Select which information is logged.
38 #log measurements statistics tracking
如果看不懂,這裏進行了中文的說明
server 192.168.1.150 iburst #從時間服務器192.168.1.150上獲取時間,如果有別的時間服務器,再添加一行就行,總數沒上限。
allow 192.168.1.0/24 #允許哪個IP段訪問本服務器的時間服務
deny 192.168.2.0/24 #禁止哪個IP段訪問本服務器的時間服務
local stratum 10 #即使自己未能同步網絡時間,也允許授時給其它客戶端
4.關閉防火牆功能
[root@mzzz ~]# systemctl stop firewalld.service
5.關閉系統核心防護 SELinux域
[root@mzzz ~]# setenface 0 #臨時關閉,如果要永久關閉,請修改SELinux的配置文件
6.啓動chrony服務和加入開機自啓動
[root@mzzz ~]# systemctl start chronyd
[root@mzzz ~]# systemctl enable chronyd
基本操作
systemctl stop chronyd #停止chronyd服務
systemctl start chronyd #啓動chronyd服務
systemctl restart chronyd #重啓chronyd服務
systemctl status chronyd #查看chronyd服務狀態
systemctl enable chronyd #開機自啓動chronyd服務
systemctl disable chronyd #禁止開機啓動chronyd服務
7. 客戶機配置及測試命令
8. 測試命令
查看當前的同步源
[root@Client ~]# 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
|| 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
===============================================================================
^* mzzz 11 6 17 13 -3839ns[ +17us] +/- 23ms
查看當前的同步狀態
[root@Client ~]# 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
==============================================================================
mzzz 4 3 6 +0.000 125.847 +0ns 14us
[root@Client ~]# date
Sun Jan 3 15:59:12 CST 2021