Chronyd同步時間(Server/Client)配置


chronyd時間服務器,和之前NTP的功能一樣,提供時間的。

基礎概念我就不說了,不擅長。chronyd是NTP之后誕生的。

最近下載了最新的Centos Linux 8.1,發現里面沒有ntp服務了,百度一搜發現改為了Chronyd,這篇文章就是部署一個服務器+客戶端測試。

1、服務器端

Centos Linux 8.1 默認安裝了chronyd服務。我們只需要配置即可。

配置文件配置

紅色部分是需要服務器配置的,這是基本配置,主要在於實現基本功能。

vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# pool 2.centos.pool.ntp.org iburst
# 同步哪台服務器上的時間 server 192.168.94.133 iburst
# 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

# Allow NTP client access from local network.
# allow 192.168.0.0/16
# 允許哪個網段的客戶端同步時間,133這台是服務器。 allow 192.168.94.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

# Get TAI-UTC offset and leap seconds from the system tz database.
leapsectz right/UTC

啟動chronyd,並設置開機自啟

[root@filesystem ~]# systemctl start chronyd
[root@filesystem ~]# systemctl enable chronyd

查看時間同步情況,本地能夠同步才能夠讓其他節點進行同步

timedatectl是一個時間配置工具,用於設定時間、與時間相關的配置等。

[root@filesystem ~]# timedatectl
               Local time: 五 2020-05-29 11:17:46 EDT
           Universal time: 五 2020-05-29 15:17:46 UTC
                 RTC time: 五 2020-05-29 15:17:46
                Time zone: America/New_York (EDT, -0400)
# 當此值為yes代表同步成功,如果為no就代表未同步,或同步失敗。 System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@filesystem ~]#

查看當前時間是從哪里同步過來的,這里顯示的是主機名。不影響。

[root@filesystem ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* filesystem                   10   7   377   537    +11us[  +25us] +/-   30us
[root@filesystem ~]#

防火牆配置

防火牆要能放行此服務,關閉防火牆或是放行相應的端口及服務。

關閉防火牆,並設置開機不啟動

[root@filesystem ~]# systemctl stop firewalld
[root@filesystem ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@filesystem ~]#

設置防火牆放行規則

[root@filesystem ~]# netstat -antup | grep chrony
udp        0      0 127.0.0.1:323           0.0.0.0:*                           35191/chronyd
udp        0      0 0.0.0.0:123             0.0.0.0:*                           35191/chronyd
udp6       0      0 ::1:323                 :::*                                35191/chronyd
[root@filesystem ~]#
# 只有upd 123端口
[root@filesystem ~]# firewall-cmd --permanent --add-port=123/udp
success
[root@filesystem ~]# firewall-cmd --reload
success
[root@filesystem ~]#

2、客戶端配置

配置文件配置

vim /etc/chrony.conf

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

配置開機自啟

[root@node1 ~]# systemctl restart chronyd
[root@node1 ~]# systemctl enable chronyd
[root@node1 ~]#

查看同步狀態及信息

[root@node1 ~]# timedatectl
               Local time: 五 2020-05-29 11:28:12 EDT
           Universal time: 五 2020-05-29 15:28:12 UTC
                 RTC time: 五 2020-05-29 15:28:12
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@node1 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* filesystem                   11   6    17    34   -520ns[+7955ns] +/-   15ms
[root@node1 ~]#

我們看到是yes,但是如何判斷它是原來的時間還是同步好了。畢竟很多系統不同步時間也是近似的。

3、設置時間

我們對chronyd服務器端進行時間修改。

這里說明一點,時間的修改只能在時間不進行同步的狀態下進行,也就是說,如果時間是同步中的,是不允許修改時間的。對時間進行保護。

[root@filesystem ~]# timedatectl
               Local time: 五 2020-05-29 11:30:40 EDT
           Universal time: 五 2020-05-29 15:30:40 UTC
                 RTC time: 五 2020-05-29 15:30:40
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@filesystem ~]#

我們測試效果,關掉ntp,修改時間

timedatectl set-ntp 0

[root@filesystem ~]# timedatectl set-ntp 0
[root@filesystem ~]# timedatectl set-time "2020-06-01 01:01:01"
[root@filesystem ~]# timedatectl set-ntp 1
[root@filesystem ~]# systemctl restart chronyd
[root@filesystem ~]# timedatectl
               Local time: 一 2020-06-01 01:01:20 EDT
           Universal time: 一 2020-06-01 05:01:20 UTC
                 RTC time: 一 2020-06-01 05:01:20
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@filesystem ~]#

客戶端查看時間

[root@node1 ~]# systemctl restart chronyd
[root@node1 ~]#
[root@node1 ~]# timedatectl
               Local time: 一 2020-06-01 01:01:55 EDT
           Universal time: 一 2020-06-01 05:01:55 UTC
                 RTC time: 五 2020-05-29 15:46:52
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@node1 ~]#

客戶端不重啟服務也是能同步的,如果你下去做實驗的話,可以測試下。


讀書和健身總有一個在路上


免責聲明!

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



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