CentOS7.x搭建時間同步服務器


關於chrony

Chrony是一個開源的自由軟件,像CentOS7或基於RHEL 7操作系統,已經是默認服務,默認配置文件在 /etc/chrony.conf 它能保持系統時間與時間服務器(NTP)同步,讓時間始終保持同步。相對於NTP時間同步軟件,占據很大優勢。其用法也很簡單。

Chrony有兩個核心組件,分別是:chronyd:是守護進程,主要用於調整內核中運行的系統時間和時間服務器同步。它確定計算機增減時間的比率,並對此進行調整補償。chronyc:提供一個用戶界面,用於監控性能並進行多樣化的配置。它可以在chronyd實例控制的計算機上工作,也可以在一台不同的遠程計算機上工作。

安裝環境

# hostname

template

# hostname -I

10.0.0.100 192.168.1.100

# cat /etc/centos-release

CentOS Linux release 7.6.1810 (Core)

# uname -r

3.10.0-957.el7.x86_64

# setenforce 0 #關閉selinux

Disabled

# systemctl stop firewalld.service

安裝chrony

# yum install chrony -y

開啟並自啟動

# systemctl restart chronyd

# systemctl enable chronyd

 

修改配置文件

# vim /etc/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

server ntp1.aliyun.com iburst #阿里雲的時間同步服務器

server ntp2.aliyun.com iburst

server 10.0.0.100 iburst   #添加本機到時間同步服務器中

# 根據實際時間計算出服務器增減時間的比率,然后記錄到一個文件中,在系統重啟后為系統做出最佳時間補償調整。

# Record the rate at which the system clock gains/losses time.

driftfile /var/lib/chrony/drift

 

# chronyd根據需求減慢或加速時間調整,

# 在某些情況下系統時鍾可能漂移過快,導致時間調整用時過長。

# 該指令強制chronyd調整時期,大於某個閥值時步進調整系統時鍾。

# 只有在因chronyd啟動時間超過指定的限制時(可使用負值來禁用限制)沒有更多時鍾更新時才生效。

# Allow the system clock to be stepped in the first three updates

# if its offset is larger than 1 second.

makestep 1.0 3

 

# 將啟用一個內核模式,在該模式中,系統時間每11分鍾會拷貝到實時時鍾(RTC)。

# Enable kernel synchronization of the real-time clock (RTC).

rtcsync

 

# 通過使用hwtimestamp指令啟用硬件時間戳

# Enable hardware timestamping on all interfaces that support it.

#hwtimestamp eth0

#hwtimestamp eth1

#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

allow 10.0.0.0/24

allow 192.168.1.0/24

 

# Serve time even if not synchronized to a time source.

#local stratum 10

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

 

設置時區

# 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

 

配置集群其他服務器同步到時間服務器即可

服務端添加此字段

server 10.0.0.100 iburst

這樣我們需求的一台內網時間服務器已經配置完畢。

 

同樣在客戶端注釋掉其他server,並在客戶端添加以下即可:

server 10.0.0.100 iburst

 

常用命令

查看時間同步源:

# chronyc sources -v

 

查看時間同步源狀態:

# chronyc sourcestats -v

 

設置硬件時間

 

硬件時間默認為UTC:

 

# timedatectl set-local-rtc 1

啟用NTP時間同步:

# timedatectl set-ntp yes

 

校准時間服務器:

# chronyc tracking

最后需要注意的是,配置完/etc/chrony.conf后,需重啟chrony服務,否則修改不生效。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM