1. はじめに
VirtualBoxにて仮想ホストを構築して検証しているのですが、時刻がずれてエラーログの解析が難しいことがたまにあったので、面倒くさがらずに時刻同期の設定を入れてみました。Linuxサーバ(CentOS8)におけるchronyによる時刻同期の設定方法を記載しているので、ぜひ参考にしてみて下さい。
2. 前提環境
<ホストOS>
・OSはWindows11
・VirtualBox バージョン 7.0.22 r165102をインストール済み
<ゲストOS>
・CentOS 8.5 を3台インストール済み
・NTPサーバ:1台、NTPクライアント:2台
※NTPクライアントはNTPサーバと時刻同期する構成
※NTPサーバ/クライアントはインターネット接続可能
1. タイムゾーン設定(NTPサーバ/NTPクライアント共通)
まずはタイムゾーンをデフォルト設定からJSTに変更します。
[root@masternode ~]# timedatectl set-timezone Asia/Tokyo
[root@masternode ~]# timedatectl
Local time: Sat 2025-01-04 08:17:05 JST
Universal time: Fri 2025-01-03 23:17:05 UTC
RTC time: Fri 2025-01-03 22:57:22
Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
[root@masternode ~]# date
Sat Jan 4 08:17:11 JST 2025
[root@masternode ~]#
2. chrony設定方法(NTPサーバ)
まず、NTPサーバ代表1台の設定をしていきます。下記の通りchrony.confファイルを修正します。
・時刻同期先の指定
複数の選択肢がありますが、今回は日本標準時を時刻配信しているNICT提供のNTPサーバと時刻同期させます。
・時刻同期を受け付けるIPアドレス帯の指定
NTPクライアントが所属するIPアドレス帯を自身の環境に合わせて指定して下さい。
[root@masternode ~]# vi /etc/chrony.conf
[root@masternode ~]# 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 ##左記をコメントアウト##
pool ntp.nict.jp 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
allow 192.168.56.0/24 ##左記を追加(NTPクライアントの所属するIPアドレス帯を指定)##
# 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
# Specify directory for log files.
logdir /var/log/chrony
# Select which information is logged.
#log measurements statistics tracking
続いて設定を反映するためにchronydサービスを再起動します。
[root@masternode ~]# systemctl restart chronyd
[root@masternode ~]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2025-01-04 09:04:44 JST; 14s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 27956 ExecStopPost=/usr/libexec/chrony-helper remove-daemon-state (code=exited, status=0/SUCCESS)
Process: 27964 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
Process: 27961 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 27963 (chronyd)
Tasks: 1 (limit: 11272)
Memory: 1.0M
CGroup: /system.slice/chronyd.service
└─27963 /usr/sbin/chronyd
Jan 04 09:04:42 masternode systemd[1]: Starting NTP client/server...
Jan 04 09:04:42 masternode chronyd[27963]: chronyd version 4.1 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP >
Jan 04 09:04:43 masternode chronyd[27963]: Frequency 16.931 +/- 5.127 ppm read from /var/lib/chrony/drift
Jan 04 09:04:43 masternode chronyd[27963]: Using right/UTC timezone to obtain leap second data
Jan 04 09:04:44 masternode systemd[1]: Started NTP client/server.
Jan 04 09:04:48 masternode chronyd[27963]: Selected source 133.243.238.164 (ntp.nict.jp)
Jan 04 09:04:48 masternode chronyd[27963]: System clock TAI offset set to 37 seconds
Jan 04 09:04:50 masternode chronyd[27963]: Selected source 133.243.238.163 (ntp.nict.jp)
時刻同期の状態を確認しましょう。いずれかの同期先のMSの項目に「^*」がついていれば問題なく時刻同期できています。
[root@masternode ~]# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^- ntp-b2.nict.go.jp 1 6 17 56 +2139us[+1249us] +/- 7819us
^- ntp-b3.nict.go.jp 1 6 17 56 +1871us[ -127us] +/- 7980us
^* ntp-k1.nict.jp 1 6 17 28 -268us[-1491us] +/- 9015us
^- ntp-a3.nict.go.jp 1 6 17 56 +1781us[ -217us] +/- 7744us
[root@masternode ~]#
時刻同期の詳細な説明を知りたい方は、下記のサイトなどを確認してみて下さい。
【Centos7】chronyc sources コマンドの出力結果の見方
3. chrony設定方法(NTPクライアント)
続いてNTPクライアント側の設定です。設定方法はNTPサーバと変わらずで、chrony.confを下記の通り修正します。
・時刻同期先の指定
NTPサーバのホスト名を指定します。
・時刻同期を受け付けるIPアドレス帯の指定
NTPクライアントにおいて本設定は不要なので、コメントアウトしたままでOKです。
[root@workernode2 ~]# vi /etc/chrony.conf
[root@workernode2 ~]# 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 ##左記をコメントアウト##
pool masternode iburst ##NTPサーバのホスト名またはIPアドレスを指定##
# 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
# 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
続いて設定を反映するためにchronydサービスを再起動します。
[root@workernode2 ~]# systemctl restart chronyd
[root@workernode2 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2025-01-04 09:24:47 JST; 5s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 22260 ExecStopPost=/usr/libexec/chrony-helper remove-daemon-state (code=exited, status=0/SUC>
Process: 22269 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
Process: 22265 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 22267 (chronyd)
Tasks: 1 (limit: 11272)
Memory: 960.0K
CGroup: /system.slice/chronyd.service
└─22267 /usr/sbin/chronyd
[root@workernode2 ~]#
時刻同期の状態を確認しましょう。同期先であるNTPサーバのMSの項目に「^*」がついていれば問題なく時刻同期できています。
[root@workernode2 ~]# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* masternode 2 6 17 18 -23us[-9735ns] +/- 5246us
[root@workernode2 ~]#
4. まとめ
CentOS8の時刻同期は以上となります。ntpdからchronyになってもあまり大きな変更はなさそうですね。サクッと設定入れて他の作業に集中してもらえれば幸いです。
参考までに、ntpdとchronyの違いがまっとまったサイトがあったので共有です。chronyになるとサービスが自動停止することはなくなったみたいですね。
Chronyとは | OSSのデージーネット
コメント