天天看點

CentOS 7 伺服器配置NTP服務(chrony)

配置環境:伺服器系統:CentOS 7.6;本地NTP伺服器10.0.0.11(聯網),客戶機10.0.0.31(未聯網)

想法:将本地伺服器時鐘同步阿裡雲NTP伺服器,本地客戶機時鐘同步本地伺服器。

目的:防止因為伺服器間時鐘不同步的問題,出現安全隐患或者未知的錯誤。

chrony簡介

chrony是一個開源的自由軟體,它能保持系統時鐘與時間伺服器同步,讓時間保持精确。它由兩個程式組成:chrongd和chronyc 。chronyd是一個背景運作的守護程序,用于調整核心運作的系統時鐘和時間伺服器同步,它确定計算機增減時間的比率,并對此進行補償。

1、CentOS系統自帶chrony,但是為了防止出錯,還是要重新安裝/更新一下它:

伺服器/客戶機都需要安裝!

yum -y install chrony
           

2、安裝完成後,啟動服務并設定為開機自動啟動,然後檢查一下服務狀态:

systemctl start chronyd.service
#啟動服務
systemctl enable chronyd.service
#設定為開機自動啟動
systemctl status chronyd.service
#檢查目前服務狀态
           

服務狀态正常的話應該如下:

CentOS 7 伺服器配置NTP服務(chrony)

3、防火牆設定

NTP使用UDP協定123号端口,防火牆設定為允許

NTP

即可

firewall-cmd  --add-service=ntp  --permanent
firewall-cmd  --reload
           

4、預設配置檔案 /etc/chrony.conf 部分說明:

# Use public servers from the pool.ntp.org project.(使用pool.ntp.org項目中的公共伺服器)
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#理論上,以server開頭添加的公共伺服器可以添加任意台
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


# Record the rate at which the system clock gains/losses time.
#chronyd程式的主要行為之一,根據實際時間計算出伺服器增減時間的比率,
#然後記錄到一個檔案中,在系統重新開機後為系統做出最佳時間補償調整。
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
#通常,chronyd将根據需求通過減慢或加速時鐘,
#使得系統逐漸糾正所有時間偏差。在某些特定情況下,
#系統時鐘可能會漂移過快,導緻該調整過程消耗很長的時間來糾正系統時鐘。
#該指令強制chronyd在調整期大于某個閥值時步進調整系統時鐘,
#但隻有在因為chronyd啟動時間超過指定限制(可使用負值來禁用限制),沒有更多時鐘更新時才生效。
makestep 1.0 3


# Enable kernel synchronization of the real-time clock (RTC).
#将啟用一個核心模式,在該模式中,系統時間每11分鐘會拷貝到實時時鐘(RTC)
rtcsync


# Enable hardware timestamping on all interfaces that support it.
#在支援它的所有接口上啟用硬體時間戳。
#hwtimestamp *
#hwtimestamp eth0
#hwtimestamp ens33

# Increase the minimum number of selectable sources required to adjust
#增加調整所需的最小可選來源數量
# the system clock.
#minsources 2

# Allow NTP client access from local network.
#允許NTP用戶端從本地網絡通路。可以指定一台主機或者一個網段允許或拒絕通路
#allow 192.168.0.0/16
#deny 192.168.10.0/24

# Serve time even if not synchronized to a time source.
#local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

           

上文部分引用《使用chrony代替ntp同步時間》

5、配置本地伺服器的

/etc/chrony.conf

檔案

一個有三處需要修改:

注釋掉原有的授時伺服器位址;添加阿裡雲授時伺服器位址(也可以換成國家授時伺服器位址:

210.72.145.44

);修改允許通路的網段(根據實際情況修改,我們的實驗機網段是10.0.0.0/8),

修改完成後儲存并退出。

CentOS 7 伺服器配置NTP服務(chrony)

6、重新開機服務,檢查伺服器端口情況

systemctl restart chronyd.service
#重新開機服務
ss -lun
#檢查伺服器UDP端口
#或者用
ss -lu
#或者用
netstat -lnup
           

使用ss -lun指令的

CentOS 7 伺服器配置NTP服務(chrony)

使用ss -lu指令的

CentOS 7 伺服器配置NTP服務(chrony)

使用netstat -lnup的

CentOS 7 伺服器配置NTP服務(chrony)

7、用戶端

/etc/chrony.conf

檔案配置

僅需要将原有的server伺服器注釋掉,新增我們本地伺服器的位址,其他的配置項不需要修改。

CentOS 7 伺服器配置NTP服務(chrony)

然後重新開機服務

systemctl restart chronyd.service
           

8、校準時間并驗證

注意:先校準本地伺服器的時間,再去校準本地客戶機的時間

chronyc tracking
#校準時間
           
CentOS 7 伺服器配置NTP服務(chrony)

兩台機器同時使用

date

檢視系統時間,如果時間一樣就是同步成功:

CentOS 7 伺服器配置NTP服務(chrony)

可以看到兩台機器的時間是一樣的。

其他常用指令:

檢視時間同步源:
chronyc sources -v

檢視時間同步源狀态:
chronyc sourcestats -v

硬體時間預設為UTC:
timedatectl set-local-rtc 1

啟用NTP時間同步:
timedatectl set-ntp yes

檢查NTP通路是否對特定主機可用
chronyc accheck

顯示NTP源線上/離線
chronyc activity 

手動添加一台新的NTP伺服器
chronyc add server

在用戶端報告已通路到伺服器
chronyc clients 

手動移除NTP伺服器或對等伺服器
chronyc delete 

手動設定守護程序時間
chronyc settime
           

其他的指令可以參考:chronyc指令使用幫助 或者使用 chronyc --help

繼續閱讀