1. 程式人生 > >時間同步chrony,最全最細

時間同步chrony,最全最細

時間同步服務
多主機協作工作時,各個主機的時間同步很重要,時間不一致會造成很多重要應用的故障,如:加密協
議,日誌,叢集等, 利用NTP(Network Time Protocol) 協議使網路中的各個計算機時間達到同步。
目前NTP協議屬於運維基礎架構中必備的基本服務之一
時間同步軟體
ntp
將系統時鐘和世界協調時UTC同步,精度在區域網內可達0.1ms,在網際網路上絕大多數的地方精度可以
達到1-50ms

chrony
實現NTP協議的的自由軟體。可使系統時鐘與NTP伺服器,參考時鐘(例如GPS接收器)以及使用手錶
和鍵盤的手動輸入進行同步。還可以作為NTPv4(RFC 5905)伺服器和對等體執行,為網路中的計算機
提供時間服務。設計用於在各種條件下良好執行,包括間歇性和高度擁擠的網路連線,溫度變化(計算
機時鐘對溫度敏感),以及不能連續執行或在虛擬機器上執行的系統。
chrony的優勢
更快的同步只需要數分鐘而非數小時時間,從而最大程度減少了時間和頻率誤差,對於並非全天
24 小時執行的虛擬計算機而言非常有用
能夠更好地響應時鐘頻率的快速變化,對於具備不穩定時鐘的虛擬機器或導致時鐘頻率發生變化的節
能技術而言非常有用

chrony官網:https://chrony.tuxfamily.org
chrony官方文件:https://chrony.tuxfamily.org/documentation.html
chrony檔案組成
rpm -ql chrony
/etc/NetworkManager/dispatcher.d/20-chrony
/etc/chrony.conf
/etc/chrony.keys
/etc/dhcp/dhclient.d/chrony.sh
/etc/logrotate.d/chrony
/etc/sysconfig/chronyd
/usr/bin/chronyc
/usr/lib/systemd/ntp-units.d/50-chronyd.list
/usr/lib/systemd/system/[email protected]
/usr/lib/systemd/system/[email protected]
/usr/lib/systemd/system/chrony-wait.service
/usr/lib/systemd/system/chronyd.service
/usr/libexec/chrony-helper
/usr/sbin/chronyd
/usr/share/doc/chrony-3.4
/usr/share/doc/chrony-3.4/COPYING
/usr/share/doc/chrony-3.4/FAQ
/usr/share/doc/chrony-3.4/NEWS
/usr/share/doc/chrony-3.4/README
/usr/share/man/man1/chronyc.1.gz
/usr/share/man/man5/chrony.conf.5.gz
/usr/share/man/man8/chronyd.8.gz
/var/lib/chrony
/var/lib/chrony/drift
/var/lib/chrony/rtc
/var/log/chrony

 

 



監聽埠: 323/udp,123/udp 配置檔案: /etc/chrony.conf 規劃如下:
10.0.0.8 充當NTP伺服器
10.0.0.30 客戶端
10.0.0.190 客戶端
10.0.0.9 客戶端
具體配置如下
# vim /etc/chrony.conf

pool  ntp.aliyun.com iburst   #可以寫阿里雲的,也可以寫其他的。
重啟配置檔案後檢視
[root@localhost ~]# 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               
===============================================================================
^* 203.107.6.88                  2   6    17    51   +411us[-2720us] +/-   37ms

我們可以看到已經同步OK,檢視埠
埠是udp的323和123

ss -ntul
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 127.0.0.1:876 *:*
udp UNCONN 0 0 *:904 *:*
udp UNCONN 0 0 *:38293 *:*
udp UNCONN 0 0 *:2049 *:*
udp UNCONN 0 0 *:20048 *:*
udp UNCONN 0 0 *:111 *:*
udp UNCONN 0 0 *:123 *:*

 

ss -ntulp
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 127.0.0.1:876 *:* users:(("rpc.statd",pid=1123,fd=7))
udp UNCONN 0 0 *:904 *:* users:(("rpcbind",pid=743,fd=7))
udp UNCONN 0 0 *:38293 *:* users:(("rpc.statd",pid=1123,fd=8))
udp UNCONN 0 0 *:2049 *:*
udp UNCONN 0 0 *:20048 *:* users:(("rpc.mountd",pid=1143,fd=7))
udp UNCONN 0 0 *:111 *:* users:(("rpcbind",pid=743,fd=6))
udp UNCONN 0 0 *:123 *:* users:(("chronyd",pid=2542,fd=7))
udp UNCONN 0 0 *:50933 *:*
udp UNCONN 0 0 127.0.0.1:323 *:* users:(("chronyd",pid=2542,fd=5))
udp UNCONN 0 0 [::]:42338 [::]:*
udp UNCONN 0 0 [::]:904 [::]:*

 

 

ntpdate 10.0.0.8
17 Mar 16:40:44 ntpdate[1483]: no server suitable for synchronization found
沒有找到適合同步的伺服器

#設定本地網路可以同步我的ntp服務請求
#按照別人的配置往下寫就好了
#allow 192.168.0.0/16
allow 10.0.0.0/24

#重啟測試
# systemctl restart chronyd

ntpdate 10.0.0.8
-bash: ntpdate: 未找到命令

#解決方法:

apt install ntpdate

yum install ntpdate




#都同步OK ntpdate 10.0.0.8 17 Mar 16:49:14 ntpdate[1502]: adjust time server 10.0.0.8 offset -0.005113 sec ntpdate 10.0.0.8 17 Mar 16:51:05 ntpdate[4147]: adjust time server 10.0.0.8 offset 0.009203 sec ntpdate 10.0.0.8 17 Mar 17:03:52 ntpdate[1727]: adjust time server 10.0.0.8 offset 0.006905 sec
#客戶端檢視
chronyc
chrony version 3.4
Copyright (C) 1997-2003, 2007, 2009-2018 Richard P. Curnow and others
chrony comes with ABSOLUTELY NO WARRANTY.  This is free software, and
you are welcome to redistribute it under certain conditions.  See the
GNU General Public License version 2 for details.

chronyc> clinets
Unrecognized command
chronyc> clients
Hostname                      NTP   Drop Int IntL Last     Cmd   Drop Int  Last
===============================================================================
10.0.0.190                      4      0   1   -   20m       0      0   -     -
10.0.0.9                        4      0   1   -  1140       0      0   -     -
10.0.0.30                       4      0   1   -   373       0      0   -     -

NTP伺服器已經全部OK!!!希望對有所幫助!!!

 

&n