1. 程式人生 > 實用技巧 >Linux 時間同步 Chrony

Linux 時間同步 Chrony

Linux 時間同步 Chrony

chrony 是網路時間協議(NTP)的通用實現。

chrony 包含兩個程式:chronyd 是一個可以在啟動時啟動的守護程式。chronyc 是一個命令列介面程式,用於監視 chronyd 的效能並在執行時更改各種操作引數。

與其它時間同步軟體的對比:https://chrony.tuxfamily.org/comparison.html

一、安裝與配置

yum -y install chrony

systemctl enable chronyd
systemctl start chronyd

vim /etc/chrony.conf

chrony.conf 預設配置

# 使用 pool.ntp.org 專案中的公共伺服器。以server開,理論上想新增多少時間伺服器都可以。
# Use public servers from the pool.ntp.org project.
# 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

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

# 如果系統時鐘的偏移量大於1秒,則允許系統時鐘在前三次更新中步進。
# Allow the system clock to be stepped in the first three updates if its offset is larger than 1 second.
makestep 1.0 3

# 啟用實時時鐘(RTC)的核心同步。
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# 通過使用 hwtimestamp 指令啟用硬體時間戳
# 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

# 指定 NTP 客戶端地址,以允許或拒絕連線到扮演時鐘伺服器的機器
# Allow NTP client access from local network.
#allow 192.168.0.0/16

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

# 指定包含 NTP 身份驗證金鑰的檔案。
# 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

二、使用

ntp 伺服器:https://www.cnblogs.com/jhxxb/p/10579816.html

1.服務端配置

chrony.conf 修改兩處

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server s1a.time.edu.cn iburst
server ntp.aliyun.com iburst

# Allow NTP client access from local network.
allow 192.168.8.0/24

開啟同步

systemctl enable chronyd
systemctl restart chronyd

# 檢視時間同步狀態
timedatectl status
# 開啟網路時間同步
timedatectl set-ntp true

2.客戶端配置

chrony.conf 修改兩處

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 192.168.8.5 iburst

# Allow NTP client access from local network.
allow 192.168.8.5

開啟同步

systemctl enable chronyd
systemctl restart chronyd

# 檢視時間同步狀態
timedatectl status
# 開啟網路時間同步
timedatectl set-ntp true

三、命令

chronyc 用法

# 檢視 ntp_servers
chronyc sources -v

# 檢視 ntp_servers 狀態
chronyc sourcestats -v

# 檢視 ntp_servers 是否線上
chronyc activity -v

# 檢視 ntp 詳細資訊
chronyc tracking -v

修改時區

# 檢視日期時間、時區及 NTP 狀態
timedatectl

# 檢視時區列表
timedatectl list-timezones
timedatectl list-timezones |  grep  -E "Asia/S.*"

# 修改時區
timedatectl set-timezone Asia/Shanghai

# 修改日期時間(可以只修改其中一個)
timedatectl set-time "2019-09-19 15:50:20"

# 開啟 NTP
timedatectl set-ntp true/flase


https://chrony.tuxfamily.org/

https://www.linuxprobe.com/centos7-chrony-time.html

https://www.zfl9.com/chrony.html