CentOS7から搭載されたChronyとNTPの違いと時刻同期の設定

Linux

はじめに

ChronyはCentOS7やRed Hat Enterprise Linux7から標準搭載されたNTPに変わる時刻同期のサービスだそうです。
GCPにCentOS7.9を構築した際に、いつも通りに色々な初期設定をしていて、再起動後に何となく各デーモンがちゃんと立ち上がっているか確認したらNTPだけ立ち上がっていなかったので忘れない様にメモしておきます。

Chronyの確認

Chronyのデーモン「chronyd」の状態を確認してみます。


[root@sv01 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2022-04-26 17:07:34 JST; 1min 33s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
 Main PID: 480 (chronyd)
   CGroup: /system.slice/chronyd.service
           mq480 /usr/sbin/chronyd

Chronyの設定

Chronyの設定はchrony.confで編集可能です。
初期状態はこんな感じです。


[root@sv01 ~]# vi /etc/chrony.conf
# These servers were defined in the installation:
server metadata.google.internal iburst
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).

# 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

GCPの公開イメージからデプロイしたので、同期サーバーがGCPの内部サーバー「metadata.google.internal」になっています。
オプション「iburst」を付けると、OS起動時に短い間隔でNTPサーバーに4回同期しに行くみたいです。


# These servers were defined in the installation:
server metadata.google.internal iburst

Chronyを停止

やはり使い慣れたNTPの方がいいので、今回のサーバーではChronyを停止させてNTPを使いたいと思います。


[root@sv01 ~]# systemctl stop chronyd
[root@sv01 ~]# systemctl disable chronyd
Removed symlink /etc/systemd/system/multi-user.target.wants/chronyd.service.
[root@sv01 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:chronyd(8)
           man:chrony.conf(5)

[重要] NTP導入前に必ずChronyを停止させる

今回行き詰ったのがこの点です。
Chronyの存在に気付かず、いつも通りNTPをインストールして設定し、OS再起動したところ、NTPが自動起動しませんでした。

どうやらChronyとNTPの両方が入っている場合、勝手にChronyだけを自動起動して、NTPを起動しないという事になるみたいです。

NTPのインストール

time-ntp
NTPサービスをインストールしてあげます。


[root@sv01 ~]# yum -y install ntp.x86_64

NTPの設定

このサーバーは日本で日本人が使うので、同期させるNTPサーバーもやはり日本在住のNTPサーバーが良いと個人的に思うので、同期先サーバーを「ntp.nict.jp」に変更します。


[root@sv01 ~]# vi /etc/ntp.conf
#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 ntp.nict.jp

NTPのデーモン「ntpd」を起動してあげて、どうやらsyslogも再起動させてあげた方が良いみたいです。


[root@sv01 ~]# systemctl start ntpd
[root@sv01 ~]# systemctl restart rsyslog.service

NTPで時刻同期と確認


[root@sv01 ~]# ntpdate ntp.nict.jp
26 Apr 17:30:08 ntpdate[1478]: adjust time server 61.205.120.130 offset -0.001536 sec
[root@sv01 ~]# timedatectl
      Local time: Tue 2022-04-26 17:31:07 JST
  Universal time: Tue 2022-04-26 08:31:07 UTC
        RTC time: Tue 2022-04-26 08:31:07
       Time zone: Asia/Tokyo (JST, +0900)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

参考ページ

下記ページを参考にさせて頂きました。
https://qiita.com/yunano/items/7883cf295f91f4ef716b

コメント