標籤:指定 centos star sid cloc system ice 協議 說明
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 iburstserver 1.centos.pool.ntp.org iburstserver 2.centos.pool.ntp.org iburstserver 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: yesNTP synchronized: yes RTC in local TZ: no DST active: n/a如果你當前的時區不正確,請按照以下操作設定。查看所有可用的時區:$ timedatectl list-timezones篩選式查看在亞洲S開的上海可用時區:$ timedatectl list-timezones | grep -E "Asia/S.*"Asia/SakhalinAsia/SamarkandAsia/SeoulAsia/ShanghaiAsia/SingaporeAsia/Srednekolymsk設定當前系統為Asia/Shanghai上海時區:$ timedatectl set-timezone Asia/Shanghai設定完時區後,強制同步下系統時鐘:$ chronyc -a makestep200 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服務,否則可能會不生效。
centos 7 Chrony 叢集同步時間