Linux系統chrony設置服務器集群同步時間
10.28.204.65 客戶端
10.28.204.66 服務端
CentOS?Linux?release 7.4.1708 (Core)
情況說明:兩臺機器都是內網,將204.66作為NTP時間服務器,204.65到此機器上同步時間。
2.安裝Chrony
系統默認已經安裝,如未安裝,請執行以下命令安裝:(兩臺都安裝)
$ yum install chrony -y
3.啟動並加入開機自啟動
$ systemctl enable chronyd.service (開機啟動設置)
$ systemctl restart chronyd.service (重啟啟動)
$ systemctl status chronyd.service (啟動狀態)
4.Firewalld設置
$ firewall-cmd --add-service=ntp --permanent
$ firewall-cmd --reload
因NTP使用123/UDP端口協議,所以允許NTP服務即可。
5.配置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 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
根據實際時間計算出服務器增減時間的比率,然後記錄到一個文件中,在系統重啟後為系統做出最佳時間補償調整。
driftfile /var/lib/chrony/drift
chronyd根據需求減慢或加速時間調整,
在某些情況下系統時鐘可能漂移過快,導致時間調整用時過長。
該指令強制chronyd調整時期,大於某個閥值時步進調整系統時鐘。
只有在因chronyd啟動時間超過指定的限制時(可使用負值來禁用限制)沒有更多時鐘更新時才生效。
將啟用一個內核模式,在該模式中,系統時間每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
設置時區
查看當前系統時區:
$ 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
7.服務器集群之間的系統時間同步
在生產環境中,其網絡都是內網結構,那麽內網如何保證服務器之間的時間同步呢?其實這個問題很簡單,只需要搭建一臺內網時間服務器,然後讓所有計算機都到服務端(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
8.常用命令
查看時間同步源:
$ chronyc sources -v
查看時間同步源狀態:
$ chronyc sourcestats -v
設置硬件時間
硬件時間默認為UTC:
$ timedatectl set-local-rtc 1
啟用NTP時間同步:
$ timedatectl set-ntp yes
校準時間服務器:
$ chronyc tracking
最後需要註意的是,配置完/etc/chrony.conf後,需重啟chrony服務,否則可能會不生效。
Linux系統chrony設置服務器集群同步時間