CentOS7で時刻設定

CentOS7から、ntpdでなくchronyを利用するのですね。今後も設定するはずので、その時のためのメモ。ntpclientとして、chronydを利用する方法です。

こちらのサイトを参考にさせて頂きました。

CentOS7

CentOS 7 NTP クライアント時刻同期設定 - eTuts+ Server Tutorial

公式はこちらです。

chrony – Introduction

ntpの設定をする前に、とりあえず、タイムゾーンをTokyoに変更。

$ sudo timedatectl set-timezone Asia/Tokyo
$ sudo timedatectl
Local time: 木 2018-01-18 06:38:50 JST
Universal time: 水 2018-01-17 21:38:50 UTC
  RTC time: 木 2018-01-18 15:24:19
 Time zone: Asia/Tokyo (JST, +0900)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a

ntpdと競合しているとまずいので、まずntpdを停止させておきます。

$ sudo systemctl stop ntpd.service
$ sudo systemctl status ntpd.service
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
$ sudo systemctl disable ntpd.service
$ sudo systemctl list-unit-files  -t service | grep ntpd.service
ntpd.service                                  disabled

続いて、chronyをインストール。

$ sudo yum install chrony
$ ps -ax | grep chronyd | grep -e grep
 72909 pts/1    S+     0:00 grep --color=auto chronyd

設定ファイルとなる"chrony.conf"を編集。nictのntpサーバと同期させます。

$ sudo vi /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
server ntp.nict.jp iburst
port 0

chrony.confのリファレンスはこちら。

chrony – chrony.conf(5)

iburst:

If this option is set, the interval between the first four polls will be 2 seconds instead of minpoll. This is useful to quickly get the first update of the clock after chronyd is started.

port:

This option allows the UDP port on which the server understands NTP requests to be specified. For normal servers this option should not be required (the default is 123, the standard NTP port).

ntp clientとして動作させる場合は、portオプションを付与して、ポートを閉じておくといいらしいです。

あとは起動設定をしてあげて。

$ sudo systemctl restart chronyd.service
$ sudo systemctl status chronyd.service
● chronyd.service - NTP client/server
  Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
  Active: active (running) since 金 2018-01-19 00:38:54 JST; 8s ago
    Docs: man:chronyd(8)
          man:chrony.conf(5)
 Process: 72998 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
 Process: 72991 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 72994 (chronyd)
  CGroup: /system.slice/chronyd.service
          mq72994 /usr/sbin/chronyd

$ sudo systemctl enable chronyd.service
$ sudo systemctl list-unit-files --type service | grep chronyd
chronyd.service                               enabled

同期されているかの確認。「*」がついていれば、同期されています。

$ chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* ntp-b2.nict.go.jp             1   6   377    70    +62us[ +868us] +/- 6552us