centos7配置時間同步服務器,centos7時間同步命令

背景

最近要做阿里雲遷移 IDC 機房,整理下 Linux 運維基線,簡單記錄,以備後用~

安裝

# 默認已經安裝
$ yum install -y chrony

配置文件

$ 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 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org
# 阿里
server ntp.aliyun.com
# 騰訊
server time1.cloud.tencent.com
server time2.cloud.tencent.com
server time3.cloud.tencent.com
server time4.cloud.tencent.com
server time5.cloud.tencent.com
# 蘋果
server time.asia.apple.com
# 微軟
server time.windows.com
# 其他
server cn.ntp.org.cn

# Record the rate at which the system clock gains/losses time.
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.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (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.
#allow 192.168.0.0/16

# 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

啟動服務及時區設置

# 啟動服務
$ systemctl start chronyd

# 開機啟動
$ systemctl enable chronyd

# 查看當前狀態
$ systemctl status chronyd

# 查看亞洲時區
$ timedatectl list-timezones | grep Asia

# 設置時區
$ timedatectl set-timezone Asia/Shanghai

驗證服務

# 查看現有的時間服務器
$ chronyc sources -v

# 查看時間服務器狀態
$ chronyc sourcestats -v

# 顯示時鐘同步相關參數
$ chronyc tracking

# 查看當前時區及時間
$ timedatectl 

手動同步時間

# 使用 ntpdate 同步時間
$ ntpdate ntp.aliyun.com

# chronyd 未啟動時,如下命令同步時間
$ chronyd -q 'server pool.ntp.org iburst'

# chronyd 啟動時,使用如下命令同步時間
$ chronyc -a 'burst 4/4' && sleep 10 && chronyc -a makestep

手動設置時間

# date 設置時間
$ date -s '2021-06-03 19:00:00'

# 關閉 ntp 同步後,才可以使用 timedatectl 進行時間設置
$ timedatectl set-ntp false

# 設置日期和時間
$ timedatectl set-time '2021-06-03 19:00:00'

# 設置日期
$ timedatectl set-time '2021-06-03'

# 設置時間
$ timedatectl set-time '19:00:00'

# 設置完成後,再開啟
$ timedatectl set-ntp true

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/234226.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-11 20:12
下一篇 2024-12-11 20:12

相關推薦

發表回復

登錄後才能評論