詳解:Linux Chrony 設定伺服器叢集同步時間
阿新 • • 發佈:2019-02-10
導讀 | Chrony是一個開源的自由軟體,像CentOS 7或基於RHEL 7作業系統,已經是預設服務,預設配置檔案在 /etc/chrony.conf 它能保持系統時間與時間伺服器(NTP)同步,讓時間始終保持同步。相對於NTP時間同步軟體,佔據很大優勢。其用法也很簡單。 |
Chrony有兩個核心元件,分別是:chronyd:是守護程序,主要用於調整核心中執行的系統時間和時間伺服器同步。它確定計算機增減時間的比率,並對此進行調整補償。chronyc:提供一個使用者介面,用於監控效能並進行多樣化的配置。它可以在chronyd例項控制的計算機上工作,也可以在一臺不同的遠端計算機上工作。
OS環境:
10.28.204.65 客戶端
10.28.204.66 服務端
CentOS Linux release 7.4.1708 (Core)
情況說明:兩臺機器都是內網,將204.66作為NTP時間伺服器,204.65到此機器上同步時間。
1.安裝Chrony
系統預設已經安裝,如未安裝,請執行以下命令安裝:
$ yum install chrony -y
2.啟動並加入開機自啟動
$ systemctl enable chronyd.service $ systemctl restart chronyd.service $ systemctl status chronyd.service
3.Firewalld設定
$ firewall-cmd --add-service=ntp --permanent $ firewall-cmd --reload
因NTP使用123/UDP埠協議,所以允許NTP服務即可。
4.配置Chrony
以下是系統預設配置檔案,我對此加以說明:
$ cat /etc/chrony.conf # 使用pool.ntp.org專案中的公共伺服器。以server開,理論上你想新增多少時間伺服器都可以。 # 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 # 根據實際時間計算出伺服器增減時間的比率,然後記錄到一個檔案中,在系統重啟後為系統做出最佳時間補償調整。 driftfile /var/lib/chrony/drift # chronyd根據需求減慢或加速時間調整, # 在某些情況下系統時鐘可能漂移過快,導致時間調整用時過長。 # 該指令強制chronyd調整時期,大於某個閥值時步進調整系統時鐘。 # 只有在因chronyd啟動時間超過指定的限制時(可使用負值來禁用限制)沒有更多時鐘更新時才生效。 makestep 1.0 3 # 將啟用一個核心模式,在該模式中,系統時間每11分鐘會拷貝到實時時鐘(RTC)。 rtcsync # Enable hardware timestamping on all interfaces that support it. # 通過使用hwtimestamp指令啟用硬體時間戳 #hwtimestamp eth0 #hwtimestamp eth1 #hwtimestamp * # Increase the minimum number of selectable sources required to adjust # the system clock. #minsources 2 # 指定一臺主機、子網,或者網路以允許或拒絕NTP連線到扮演時鐘伺服器的機器 #allow 192.168.0.0/16 #deny 192.168/16 # Serve time even if not synchronized to a time source. local stratum 10 # 指定包含NTP驗證金鑰的檔案。 #keyfile /etc/chrony.keys # 指定日誌檔案的目錄。 logdir /var/log/chrony # Select which information is logged. #log measurements statistics tracking
5.設定時區
檢視當前系統時區:
$ timedatectl Local time: Fri 2018-2-29 13:31:04 CST Universal time: Fri 2018-2-29 05:31:04 UTC RTC time: Fri 2018-2-29 08:17:20 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: yes RTC in local TZ: no DST active: n/a 如果你當前的時區不正確,請按照以下操作設定。 檢視所有可用的時區: $ timedatectl list-timezones 篩選式檢視在亞洲S開的上海可用時區: $ timedatectl list-timezones | grep -E "Asia/S.*" Asia/Sakhalin Asia/Samarkand Asia/Seoul Asia/Shanghai Asia/Singapore Asia/Srednekolymsk 設定當前系統為Asia/Shanghai上海時區: $ timedatectl set-timezone Asia/Shanghai 設定完時區後,強制同步下系統時鐘: $ chronyc -a makestep 200 OK
6.伺服器叢集之間的系統時間同步
在生產環境中,其網路都是內網結構,那麼內網如何保證伺服器之間的時間同步呢?其實這個問題很簡單,只需要搭建一臺內網時間伺服器,然後讓所有計算機都到服務端(10.28.204.66)去同步時間即可。
具體操作:在服務端註釋以下內容:
#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 10.28.204.66 iburst 這樣我們需求的一臺內網時間伺服器已經配置完畢。 同樣在客戶端註釋掉其他server,並在客戶端(10.28.204.65)新增以下: server 10.28.204.66 iburst
到此已經完成系統時間的同步。如有多臺機器,操作也是如此。
7.常用命令
檢視時間同步源:
$ chronyc sources -v 檢視時間同步源狀態: $ chronyc sourcestats -v 設定硬體時間 硬體時間預設為UTC: $ timedatectl set-local-rtc 1 啟用NTP時間同步: $ timedatectl set-ntp yes 校準時間伺服器: $ chronyc tracking
最後需要注意的是,配置完/etc/chrony.conf後,需重啟chrony服務,否則可能會不生效。