時刻同期chronyサービスの設定

1.クロニーサービス

chrony は、Network Time Protocol (NTP) の汎用実装です。システム クロックを NTP サーバー、基準クロック (GPS 受信機など)、および時計とキーボードを使用した手動入力と同期できます。また、NTPv4 (RFC 5905) サーバーおよびピアとしても動作し、ネットワーク上の他のコンピューターにタイム サービスを提供します。

断続的なネットワーク接続、極度に混雑したネットワーク、温度の変化 (通常のコンピューターのクロックは温度に敏感です)、継続的に実行できないシステムや仮想マシン上で実行できないシステムなど、幅広い条件下で適切に動作するように設計されています。

インターネット上で同期された 2 台のマシン間の一般的な精度は数ミリ秒以内ですが、LAN では、多くの場合、精度は数十マイクロ秒になります。ハードウェア タイムスタンプまたはハードウェア基準クロックを使用すると、マイクロ秒未満の精度が可能になる場合があります。

chrony には 2 つのプログラムが含まれています。chronyd はブート時に開始できるデーモン プロセスで、chronyc は chronyd のパフォーマンスを監視し、実行中にさまざまな動作パラメータを変更するために使用できるコマンド ライン インターフェイス プログラムです。

centos7+ は chrony 時刻同期設定をサポートしました。centos8 では NTP はサポートされなくなりました。ntp
時刻同期設定と比較して、chrony はよりシンプルかつ効率的です。システムをサーバー時刻と常に同期させることができるオープンソース ソフトウェアです。

2. OSテスト環境の紹介

サーバー側:RedHat8.2-172.20.10.6

クライアント:CentOS8.3-172.20.10.7

このテストは最小限のシステム インストールです。ローカルの yum ソースを構成した後、chrony サービスがインストールされます (次のように、サービスはサーバーとクライアントの両方にインストールされます)。

[root@centos8-3 ~]# yum install -y chrony
Last metadata expiration check: 0:00:05 ago on Tue 15 Jun 2021 08:02:45 PM CST.
Dependencies resolved.
=================================================================================================================================================================
 Package                                Architecture                        Version                                Repository                               Size
=================================================================================================================================================================
Installing:
 chrony                                 x86_64                              3.5-1.el8                              centos8-OS                              271 k
Installing weak dependencies:
 timedatex                              x86_64                              0.5-3.el8                              centos8-OS                               32 k
 
Transaction Summary
=================================================================================================================================================================
Install  2 Packages
 
Total size: 303 k
Installed size: 731 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                         1/1 
  Installing       : timedatex-0.5-3.el8.x86_64                                                                                                              1/2 
  Running scriptlet: timedatex-0.5-3.el8.x86_64                                                                                                              1/2 
  Running scriptlet: chrony-3.5-1.el8.x86_64                                                                                                                 2/2 
  Installing       : chrony-3.5-1.el8.x86_64                                                                                                                 2/2 
  Running scriptlet: chrony-3.5-1.el8.x86_64                                                                                                                 2/2 
  Verifying        : chrony-3.5-1.el8.x86_64                                                                                                                 1/2 
  Verifying        : timedatex-0.5-3.el8.x86_64                                                                                                              2/2 
 
Installed:
  chrony-3.5-1.el8.x86_64                                                       timedatex-0.5-3.el8.x86_64                                                      
 
Complete!

3. サーバーとクライアントの構成

(1) サーバー側の設定と説明(修正3点)

[root@RedHat8-2 ~]# 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).
#pool 2.rhel.pool.ntp.org iburst  --注释这行,外网时间服务器的网址
server 172.20.10.6 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
 
# 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 172.20.10.0/28              -->>允许哪些服务器或客户端到这台时间服务器来同步时间。必须配置
# Serve time even if not synchronized to a time source.
local stratum 10                  -->>该行注释取消掉不然NTP synchronized: 为no 取消掉后变为 NTP synchronized:yes
# 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
 
# Specify directory for log files.
logdir /var/log/chrony
 
# Select which information is logged.
#log measurements statistics tracking

chronyサービスを起動して確認する

启动chronyd
[root@RedHat8-2 ~]# systemctl start chronyd
加入开机自启
[root@RedHat8-2 ~]# systemctl enable chronyd
 
检查
[root@RedHat8-2 ~]# netstat -antulp|grep chronyd
udp        0      0 0.0.0.0:123             0.0.0.0:*                           7745/chronyd        
udp        0      0 127.0.0.1:323           0.0.0.0:*                           7745/chronyd        
udp6       0      0 ::1:323                 :::*                                7745/chronyd        
[root@RedHat8-2 ~]# ss -antulp|grep chronyd
udp     UNCONN   0        0                0.0.0.0:123           0.0.0.0:*       users:(("chronyd",pid=7745,fd=9))                                              
udp     UNCONN   0        0              127.0.0.1:323           0.0.0.0:*       users:(("chronyd",pid=7745,fd=7))                                              
udp     UNCONN   0        0                  [::1]:323              [::]:*       users:(("chronyd",pid=7745,fd=8))                                              

(2) クライアントの設定

[root@centos8-3 ~]# 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).
#pool 2.centos.pool.ntp.org iburst
server 172.20.10.6 iburst   -->>添加该行,表示到这台服务器去同步时间

chronyd を起動し、起動時に自動起動を追加する

[root@centos8-3 ~]# systemctl start chronyd
[root@centos8-3 ~]# systemctl enable chronyd

4. ステータスを確認する

[root@centos8-3 ~]# timedatectl 
               Local time: Tue 2021-06-15 16:45:10 CST
           Universal time: Tue 2021-06-15 08:45:10 UTC
                 RTC time: Tue 2021-06-15 16:45:10
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes   --表示已同步完成
              NTP service: active
          RTC in local TZ: no

5. タイムソース情報の表示

サーバ

[root@RedHat8-2 ~]# 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               
===============================================================================
^? RedHat8-2.localdomain         0   7   377     -     +0ns[   +0ns] +/-    0ns

クライアント

[root@centos8-3 ~]# 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               
===============================================================================
^* 172.20.10.6                  11   6   177    50  +2087ns[  -33us] +/-  227ms

6. テスト

テスト手順。サーバーの時刻を手動で変更して、クライアントが同期されているかどうかを確認します。

(1) クライアントとサーバーの現在時刻を表示する

服务端
[root@RedHat8-2 ~]# date
Tue Jun 15 16:43:22 CST 2021
 
客户端
[root@centos8-3 ~]# date
Tue Jun 15 16:43:22 CST 2021
 

(2) サーバー時刻を手動で変更する

[root@RedHat8-2 ~]# date 
Tue Jun 15 17:14:03 CST 2021
[root@RedHat8-2 ~]# date -s "2021-6-15 18:00:00"
Tue Jun 15 18:00:00 CST 2021

(3) クライアントが自動的に同期するまで待ちます (テスト同期時間は比較的時間がかかります)。

[root@centos8-3 ~]# 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               
===============================================================================
^~ 172.20.10.6                  11   6    37    11   -2716s[ -2716s] +/-  265ms
[root@centos8-3 ~]# date
Tue Jun 15 17:16:44 CST 2021
[root@centos8-3 ~]# timedatectl 
               Local time: Tue 2021-06-15 17:16:53 CST
           Universal time: Tue 2021-06-15 09:16:53 UTC
                 RTC time: Tue 2021-06-15 09:17:19
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
 
[root@centos8-3 ~]# date
Tue Jun 15 18:06:49 CST 2021

テスト中に問題が見つかり、同期時間は比較的遅かったものの、同期は正常に実行されました。

スタートアップの設定とサービスの再起動

systemctl enable chronyd
systemctl restart chronyd

chronyc ソースを使用して時刻同期ステータスを表示する

[root@jingcheng ~]# chronyc sources
  • *chronyd が現在同期しているクロック ソースを示します

画像

  • ? 接続が失われた、またはパケットがすべてのテストに失敗した送信元を示します。また、少なくとも 3 つのサンプルが収集されるまで、起動時にも表示されます。

画像

  • + は、受け入れ可能なソースが選択したソースとグループ化されていることを示します
  • - マージ アルゴリズムによって除外される許容可能なソースを示します
  • x は、chronyd が偽のティッカーとみなす時計を表します (つまり、その時刻は他のほとんどのソースと一致しません)。
  • ~ は時間の変動が大きすぎると思われる情報源を示します。

ntp_servers:chronyc ソースの表示

ntp_servers ステータスの表示: chronyc sourcestats

ntp_servers がオンラインかどうかを確認します: chronyc activity

NTP の詳細を表示: chronyc 追跡

時刻関連コマンド: timedatectl
日付、時刻、タイムゾーン、および NTP ステータスの表示: timedatectl
タイムゾーンリストの表示: timedatectl list-timezones タイム
ゾーンの変更: timedatectl set-timezone + 変更するタイムゾーン
日付と時刻の変更: timedatectl set-time 「変更予定日」

7. chrony 構成の問題の発見と解決

1. クライアント クロック ソースを追加すると表示されますか? (接続が失われたソース、またはパケットがすべてのテストに失敗したソース) 結果としてクロック ソースを同期できなくなります。

解決策: ① ファイアウォールがオフになっているかどうかを確認します。 ② selinux が許可ステータスか無効ステータスかを確認します。 ③ ping を実行してリンクが開いているかどうかを確認します。 ④ ネットワーク構成を確認します。 ⑤ chrony 構成のallow コマンドは、デフォルトでは NTP クライアント アクセスを許可しません。

おすすめ

転載: blog.csdn.net/qq_36306519/article/details/130756674