Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

CentOS7の時刻同期 chronyについて

 

こんにちは、LinuCエバンジェリストこと、鯨井貴博@opensourcetechです。

 

CentOS7の時刻同期プログラムとして動作する、chronyに関するメモです。

CentOS6ではntpd/ntpdateが使われていたので、ちょっとその差分を知りたくて。 



公式ページやその他マニュアルなどのリンク

https://chrony.tuxfamily.org/

https://chrony.tuxfamily.org/manual.html

https://linux.die.net/man/1/chronyc

https://linux.die.net/man/8/chronyd

第18章 chrony スイートを使用した NTP 設定 Red Hat Enterprise Linux 7 | Red Hat Customer Portal

18.3. chrony の使用 Red Hat Enterprise Linux 7 | Red Hat Customer Portal

 

上記などを参照すると、

・時刻の精度向上

・ハードウェアクロックとの連携

などの特徴があります。

 

なお、NTPバージョン3となるので、NTPバージョン4を使いたい場合は、ntpdを使えばよいとのことです。

 

 

 

設定ファイル(/etc/chrony.conf)

 設定ファイルは /etc/chrony.confとなります。

https://chrony.tuxfamily.org/doc/3.4/chrony.conf.html

[root@db01 ~]# 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.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

# 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

 

server 0.centos.pool.ntp.org iburs・・・時刻参照するNTPサーバ。
driftfile /var/lib/chrony/drift・・・RTCがどれくらいずれるかを記録し、補正するための情報を書き込むファイル。
makestep 1.0 3・・・1.0秒間のずれがあった場合、3回のstep調整を行う。
rtcsync・・・カーネルに時刻更新を伝え、11分毎にRTC(リアルタイムクロック)を更新する。
#hwtimestamp *・・・一部のNICでサポートされているハードウェアタイムスタンプを利用する。「*」は全NICを意味する。
#minsources 2・・・時刻同期の際に考慮されるNTPサーバの数。
#allow 192.168.0.0/16・・・NTPクライアントの接続を許可するネットワーク。逆のdenyもある。
#local stratum 10・・・同期先のNTPサーバから情報が取得できない場合、自身のストラタムをいくつにするかの設定。通常だと、16となりNTPクライアントからみると利用不可となる。
#keyfile /etc/chrony.keys・・・認証用のキーファイル。
logdir /var/log/chrony・・・ログ格納ディレクトリ。
#log measurements statistics tracking・・・ログに記録する情報指定。

  ※slew調整とは、取得した時刻に徐々に合わせていく動作

  ※step調整とは、取得した時刻に即合わせる動作

 

 

chronyプロセスの起動(start)・停止(stop)・状態確認(status)など

chronyd(chronyデーモン)に対して、stystemctlコマンドを使います。 

[root@db01 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since 木 2020-05-21 19:58:10 JST; 11min ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 17162 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
Process: 17158 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
CGroup: /system.slice/chronyd.service
mq17160 /usr/sbin/chronyd

5月 21 19:58:10 db01 systemd[1]: Stopped NTP client/server.
5月 21 19:58:10 db01 systemd[1]: Starting NTP client/server...
5月 21 19:58:10 db01 chronyd[17160]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PR...BUG)
5月 21 19:58:10 db01 chronyd[17160]: Frequency -2.183 +/- 0.944 ppm read from /var/lib/chrony/drift
5月 21 19:58:10 db01 systemd[1]: Permission denied while opening PID file or unsafe symlink chain...d.pid
5月 21 19:58:10 db01 systemd[1]: Started NTP client/server.
5月 21 19:58:16 db01 chronyd[17160]: Selected source 172.105.231.23
5月 21 20:00:36 db01 chronyd[17160]: Source 2606:4700:f1::123 replaced with 91.189.182.32
Hint: Some lines were ellipsized, use -l to show in full.

 

 

同期の状態確認など

 chronycコマンドにオプションをつけて状態確認を行います。

 

chronyc makestep・・・NTPサーバと手動同期

[root@db01 ~]# chronyc makestep
200 OK

 ※その他、chronyデーモンが停止している場合は「506 Cannot talk to daemon」となるなど

 

chronyc tracking・・・chronyのトラッキング確認

[root@db01 ~]# chronyc trachking
Unrecognized command
[root@db01 ~]# chronyc tracking
Reference ID : 85F3EEA3 (ntp-b2.nict.go.jp)
Stratum : 2
Ref time (UTC) : Thu May 21 14:03:24 2020
System time : 0.000045390 seconds slow of NTP time
Last offset : +0.000000412 seconds
RMS offset : 0.000684728 seconds
Frequency : 1.919 ppm slow
Residual freq : -2.164 ppm
Skew : 0.906 ppm
Root delay : 0.008469617 seconds
Root dispersion : 0.000855708 seconds
Update interval : 64.3 seconds
Leap status : Normal

 

chronyc sources・・・同期先NTPサーバの情報確認

[root@db01 ~]# chronyc sources
210 Number of sources = 4
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* ntp-b2.nict.go.jp 1 6 37 51 +9769ns[ +374us] +/- 5351us
^? a80-127-119-186.adsl.xs4> 0 6 0 - +0ns[ +0ns] +/- 0ns
^? 2001:bc8:182c:120d::1 0 7 0 - +0ns[ +0ns] +/- 0ns
^- time.cloudflare.com 3 6 37 52 +4548us[+4912us] +/- 65ms

※^はNTPサーバ、=はピア、#はローカル接続を表す。

※*は現在同期中、+は同期アルゴリズムに利用可能、-は同期アルゴリズムから除外、?や×は利用に適さないものを表す。

 

 chronyc sourcestat・・・同期先NTPサーバの統計情報確認

[root@db01 ~]# chronyc sourcestats
210 Number of sources = 4
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
ntp-b2.nict.go.jp 4 3 6 -23.642 3080.554 -1512us 423us
time.cloudflare.com 4 3 6 +180.156 86601.500 +18ms 6815us
2001:bc8:182c:120d::1 0 0 0 +0.000 2000.000 +0ns 4000ms
time.cloudflare.com 5 5 70 -0.551 122.993 +4906us 503us

 

 

 

 

アフィリエイトのアクセストレード

 

 

 

 

 

 

www.slideshare.net

github.com

www.facebook.com

twitter.com

www.instagram.com

 

 

にほんブログ村 IT技術ブログ Linuxへ
Linux

にほんブログ村 IT技術ブログ オープンソースへ
オープンソース

 

 

Opensourcetech by Takahiro Kujirai