CentOS7配置時間源
在一些特定的場景下, 需要系統的時間與其他伺服器,或者網際網路标準時間進行時間同步,一般用來實作身份認證等機制。因為身份認證機制中一般會加入時間戳作為每次請求的變量,防止對伺服器進行重播攻擊等。是以一般在伺服器之間需要進行身份鑒别和認證等操作的時候,都需要将時間進行同步。
時間同步根據場景不同,一般可以分為兩種。主要的差別在于時間源的提供者。
- 如果具備網際網路環境,可以通過網際網路上的時間源進行時間同步,例如阿裡時間源,
ntp.aliyun.com
- 如果不具備網際網路環境,例如私有雲或者區域網路中有需要進行時間同步,那麼就需要一台提供時間服務的伺服器。時間源伺服器有專業的裝置,通過衛星原子鐘等確定時間準确。也可以通過普通的伺服器安裝時間源軟體提供時間源。但是與專業時間源伺服器相比,時間的準确性要差很多。運作一段時間後,系統内的時間可能和标準時間之間相差幾分甚至幾十分鐘。
這裡介紹第二種情況,在伺服器上通過時間源軟體如何提供時間服務。
1. 時間源伺服器搭建
通過軟體搭建時間源伺服器,比較常用的是
chrony
。
Chrony 的優勢包括:
- 更快的同步隻需要數分鐘而非數小時時間,進而最大程度的減少時間和頻率誤差,這對于并非全天運作的台式計算機或系統而言非常有用。
- 能夠更好的響應時間頻率的快速變化,這對于具備不穩定時鐘的虛拟機或導緻時鐘頻率反生變化的節能技術而言非常有用。
- 在初始同步後,它并不會停止時鐘,以防對需要系統時間保持單調的程式造成影響。
- 在應對臨時非對稱延遲時,(例如,大規模下載下傳造成連結飽和時)提供了更好的穩定性。
- 無需對伺服器進行定期輪詢,是以具備間歇性網絡連接配接的系統仍然可以快速同步時鐘。
1.1 安裝chrony程式
1.2 配置chrony.conf檔案
[[email protected] ~]# cat /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# 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
#server edu.ntp.org.cn iburst
#server pool.ntp.org iburst
server ntp.aliyun.com 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啟動時間
# 超過指定限制(可使用負值來禁用限制),沒有更多時鐘更新時才生效。
# 此處表示如果調整值大于1秒,則這将使系統時鐘步進,但僅在前十個時鐘更新中。
makestep 1.0 3
# Enable kernel synchronization of the real-time clock (RTC).
# rtcsync指令将啟用一個核心模式,在該模式中,系統時間每11分鐘會拷貝到實時時鐘(RTC)
rtcsync
# Enable hardware timestamping on all interfaces that support it.
# 在所有支援硬體的接口上啟用硬體時間戳。系統預設
#hwtimestamp *
# Increase the minimum number of selectable sources required to adjust
# the system clock.
# 增加需要調整的最小可選信号源數量 系統時鐘。系統預設
#minsources 2
# Allow NTP client access from local network.
# 這裡你可以指定一台主機、子網,或者網絡以允許或拒絕NTP連接配接到扮演時鐘伺服器的機器。白名單機制。
# 可以使用deny all拒絕所有用戶端。黑名單機制。
deny all
# 注意,如果主機位是0的話可以簡寫,比如下面的位址可以簡寫為"127/8",不過建議大家還是寫完整,可讀性更強。
allow 127.0.0.0/8 #允許同步的網段
# 預設是不允許外部連接配接,監聽在127.0.0.1。
# 以下配置允許所有連接配接
allow all
# Serve time even if not synchronized to a time source.
# 如果上面使用server字段配置的時間伺服器同步時間失敗,預設情況下目前時間伺服器是不會向
# 用戶端同步時間的。這是因為擔心目前節點的時間不準确(因為目前節點沒有和定義中的server
# 時間伺服器進行同步)。如果我們想要在server指定的時間伺服器同步失敗的情況下依舊傳回當
# 前時間伺服器的時間給用戶端,需要開啟該參數。
# 生産環境開啟該參數會有一個問題,如果server有配置,那麼可能會導緻所有伺服器的時間都是
# 一緻的,并且都是錯誤的。如果是非網際網路環境,例如私有雲等或獨立區域網路,即使都出錯也可以接受。
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
# 其他未在預設配置檔案的配置項
# 在第一次時鐘更新之後,chronyd将檢查每次時鐘更新的偏移量,它将忽略兩次大于1000秒的調整,并退出另一個調整。
maxchange 1000 1 2
# 該rtcfile指令定義中的檔案名chronyd可以儲存跟蹤系統的實時時鐘(RTC)的精度相關的參數。
rtcfile /var/lib/chrony/rtc
1.3 啟動chrony服務
[[email protected] ~]# systemctl start chronyd
[[email protected] ~]# systemctl enable chronyd
[[email protected] ~]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2021-09-18 12:07:39 CST; 3 weeks 2 days ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 877 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
Process: 860 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 863 (chronyd)
Tasks: 1
Memory: 1.1M
CGroup: /system.slice/chronyd.service
└─863 /usr/sbin/chronyd
Sep 18 12:07:39 kdc chronyd[863]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
Sep 18 12:07:39 kdc chronyd[863]: Frequency -16.644 +/- 0.007 ppm read from /var/lib/chrony/drift
Sep 18 12:07:39 kdc systemd[1]: Started NTP client/server.
Sep 18 12:08:13 kdc chronyd[863]: Selected source 202.118.1.130
Sep 18 12:08:13 kdc chronyd[863]: System clock wrong by 344.051256 seconds, adjustment started
Sep 18 12:13:58 kdc chronyd[863]: System clock was stepped by 344.051256 seconds
Oct 01 15:54:41 kdc chronyd[863]: Can't synchronise: no selectable sources
Oct 09 09:05:54 kdc chronyd[863]: Selected source 185.209.85.222
Oct 09 09:08:22 kdc chronyd[863]: Selected source 202.118.1.130
Oct 11 16:15:34 kdc chronyd[863]: Source 119.28.183.184 replaced with 139.199.215.251
# 檢視網絡端口監聽
[[email protected] ~]# ss -tulp | grep chronyd
udp UNCONN 0 0 127.0.0.1:323 *:* users:(("chronyd",pid=863,fd=5))
udp UNCONN 0 0 ::1:323 :::* users:(("chronyd",pid=863,fd=6))
[[email protected] ~]# netstat -tunpl |grep chrony
udp 0 0 0.0.0.0:123 0.0.0.0:* 31186/chronyd
udp 0 0 127.0.0.1:323 0.0.0.0:* 31186/chronyd
udp6 0 0 :::123 :::* 31186/chronyd
udp6 0 0 ::1:323 :::* 31186/chronyd
1.4 驗證chronyd服務
# 檢視源的詳細資訊
[[email protected] ~]# chronyc sources -v
210 Number of sources = 4
.-- 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
===============================================================================
^- stratum2-1.ntp.led01.ru.> 2 10 377 20m +52ms[ +52ms] +/- 113ms
^- ntp1.flashdance.cx 2 10 377 812 -34ms[ -34ms] +/- 202ms
^* 202.118.1.130 1 10 377 519 -1066us[-1057us] +/- 8320us
^? 139.199.215.251 2 10 3 151 -1871us[-1871us] +/- 44ms
# 檢視時間同步狀态
[[email protected] ~]# chronyc sourcestats -v
210 Number of sources = 4
.- Number of sample points in measurement set.
/ .- Number of residual runs with same sign.
| / .- Length of measurement set (time).
| | / .- Est. clock freq error (ppm).
| | | / .- Est. error in freq.
| | | | / .- Est. offset.
| | | | | | On the -.
| | | | | | samples. \
| | | | | | |
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
stratum2-1.ntp.led01.ru.> 10 7 55h -0.007 0.008 +23ms 359us
ntp1.flashdance.cx 64 33 19h +0.037 0.145 -35ms 6342us
202.118.1.130 64 36 19h +0.000 0.004 +24ns 174us
139.199.215.251 2 0 1035 +0.000 2000.000 -1871us 4000ms
# 檢視目前時間同步服務開啟情況
[[email protected] ~]# timedatectl
Local time: Mon 2021-10-11 19:06:40 CST
Universal time: Mon 2021-10-11 11:06:40 UTC
RTC time: Mon 2021-10-11 11:00:51
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
# 校準時間伺服器
[[email protected] ~]# chronyc tracking
Reference ID : 771CCEC1 (119.28.206.193)
Stratum : 3
Ref time (UTC) : Mon Oct 11 10:59:05 2021
System time : 0.000354706 seconds slow of NTP time
Last offset : -0.000433533 seconds
RMS offset : 0.003306682 seconds
Frequency : 16.784 ppm slow
Residual freq : -0.015 ppm
Skew : 0.562 ppm
Root delay : 0.062343303 seconds
Root dispersion : 0.005311402 seconds
Update interval : 1549.6 seconds
Leap status : Normal
1.5 防火牆放開udp的323,123端口
[[email protected] ~]# firewall-cmd --zone=public --permanent --add-port=123/udp
[[email protected] ~]# firewall-cmd --reload
2. 用戶端與時間源進行同步配置
2.1 安裝chrony軟體
2.2 修改chrony.conf配置檔案
[[email protected] ~]# egrep -v "^#|^$" /etc/chrony.conf
server kdc iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
2.3 啟動chronyd服務
[[email protected] ~]# systemctl start chronyd
[[email protected] ~]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-10-11 17:35:01 CST; 1min 13s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 5458 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
Process: 5454 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 5456 (chronyd)
Tasks: 1
Memory: 316.0K
CGroup: /system.slice/chronyd.service
└─5456 /usr/sbin/chronyd
Oct 11 17:35:01 ns systemd[1]: Starting NTP client/server...
Oct 11 17:35:01 ns chronyd[5456]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
Oct 11 17:35:01 ns chronyd[5456]: Frequency -13.853 +/- 0.255 ppm read from /var/lib/chrony/drift
Oct 11 17:35:01 ns systemd[1]: Started NTP client/server.
Oct 11 17:35:05 ns chronyd[5456]: Selected source 192.168.96.33
[[email protected] ~]# 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
===============================================================================
^* kdc.example.com 3 6 377 42 -39us[ -125us] +/- 36ms
2.4 手動進行時間同步
理論上,啟動chronyd服務以後,chrony會根據配置檔案裡的配置自動進行時間同步。如果想要手動進行同步,一般使用
ntpdate
指令。
# 安裝ntpdate
[[email protected] ~]# yum install -y ntpdate
# 手動進行時間同步
[[email protected] ~]# ntpdate kdc
11 Oct 18:36:36 ntpdate[11782]: adjust time server 192.168.96.33 offset -0.000098 sec
2.5 設定計劃任務
# 編輯crontab檔案
[[email protected] ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[[email protected] ~]# crontab -l
#synchronization time, 每隔五分鐘進行同步一次
*/5 * * * * /usr/sbin/ntpdate kdc > /dev/null 2>&1