備忘録

Windows,Linux,Mac,AWS,VMware,ネットワークなどの検証

Raspberry Pi NTPサーバ(時刻配信用)を作る

ラズパイでNTPサーバ(時刻配信用)を作り、LAN内のPCに時刻を配信します。

参考
blog.inaho.space

使用機種:Raspberry Pi 4 Model B 4GB

ntpパッケージがインストールされていないので、インストールします。

※NTPクライアント用に最初からインストールされている「systemd-timesyncd」には、時刻配信の機能はないようです(インターネット上のNTPサーバから時刻を取得するのみ)
wiki.archlinux.jp

またsystemd-timesyncd は ntpパッケージと競合するようなので、ntpパッケージのインストール時に削除されます。

$ sudo apt install ntp
$ sudo apt install ntp
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています... 完了
状態情報を読み取っています... 完了
以下の追加パッケージがインストールされます:
  libevent-core-2.1-7 libevent-pthreads-2.1-7 libopts25 sntp
提案パッケージ:
  ntp-doc
以下のパッケージは「削除」されます:
  systemd-timesyncd
以下のパッケージが新たにインストールされます:
  libevent-core-2.1-7 libevent-pthreads-2.1-7 libopts25 ntp sntp
アップグレード: 0 個、新規インストール: 5 個、削除: 1 個、保留: 0 個。
1,131 kB のアーカイブを取得する必要があります。
この操作後に追加で 2,569 kB のディスク容量が消費されます。
続行しますか? [Y/n] Y

npt.confを編集します。

$ sudo nano /etc/ntp.conf

すでに書き込まれているpool行を#コメントアウトし、日本のNTPサーバを追加します。

#pool 0.debian.pool.ntp.org iburst
#pool 1.debian.pool.ntp.org iburst
#pool 2.debian.pool.ntp.org iburst
#pool 3.debian.pool.ntp.org iburst
pool ntp.nict.jp iburst
pool ntp.jst.mfeed.ad.jp iburs

このNTPサーバへのアクセスを許可するネットワーク範囲を追記します。

#restrict 192.168.123.0 mask 255.255.255.0 notrust
restrict 192.168.100.0 mask 255.255.255.0 kod nomodify nopeer notrap

NTPサービスを有効化

$ sudo systemctl enable ntp.service

NTPサービスを起動

$ sudo systemctl start ntp.service

NTPサービス起動状態を確認

※ntp.service; enabled になっている。
※Active: active になっている。

$ sudo systemctl status  ntp.service
$ sudo systemctl status  ntp.service
● ntp.service - Network Time Service
     Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-03-20 10:53:34 JST; 4min 18s ago
       Docs: man:ntpd(8)
    Process: 1845 ExecStart=/usr/lib/ntp/ntp-systemd-wrapper (code=exited, status=0/SUCCESS)
   Main PID: 1851 (ntpd)
      Tasks: 2 (limit: 4915)
        CPU: 149ms
     CGroup: /system.slice/ntp.service
             mq1851 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 110:114

 3月 20 10:53:34 raspberrypi systemd[1]: Started Network Time Service.
 3月 20 10:53:35 raspberrypi ntpd[1851]: Soliciting pool server 61.205.120.130
 3月 20 10:53:36 raspberrypi ntpd[1851]: Soliciting pool server 133.243.238.244
 3月 20 10:53:36 raspberrypi ntpd[1851]: Soliciting pool server 210.173.160.57
 3月 20 10:53:37 raspberrypi ntpd[1851]: Soliciting pool server 210.173.160.87
 3月 20 10:53:37 raspberrypi ntpd[1851]: Soliciting pool server 133.243.238.163
 3月 20 10:53:38 raspberrypi ntpd[1851]: Soliciting pool server 210.173.160.27
 3月 20 10:53:38 raspberrypi ntpd[1851]: Soliciting pool server 133.243.238.164
 3月 20 10:53:39 raspberrypi ntpd[1851]: Soliciting pool server 133.243.238.243
 3月 20 10:53:40 raspberrypi ntpd[1851]: Soliciting pool server 2001:ce8:78::2

NTP同期状態を確認

参考
iotdiyclub.net

$ sudo ntpq -p
$ sudo ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
  ntp.nict.jp     .POOL.          16 p    -   64    0    0.000   +0.000   0.001
  ntp.jst.mfeed.a .POOL.          16 p    -   64    0    0.000   +0.000   0.001
 *ntp-k1.nict.jp  .nict.           1 u   31   64   77   16.269   -3.278   1.481
 -ntp-a3.nict.go. .NICT.           1 u   34   64   77   24.230   -1.527   1.291
 -ntp2.jst.mfeed. 133.243.236.18   2 u   39   64   77   22.225   -2.147   4.894
 +ntp-b2.nict.go. .NICT.           1 u   29   64   77   19.540   -3.357   2.492
 -ntp3.jst.mfeed. 133.243.236.19   2 u   35   64   77   36.522   -1.583   2.270
 -ntp1.jst.mfeed. 133.243.236.17   2 u   33   64   77   24.113   -2.324   2.883
 +ntp-b3.nict.go. .NICT.           1 u   25   64   77   27.172   +0.788   1.992
 -ntp-a2.nict.go. .NICT.           1 u   28   64   77   24.417   -2.561   1.996

 #「*」は、参照同期中のNTPサーバ。
 #「+」は、クロック誤り検査に合格したNTPサーバ
 #「  」は、サーバを参照していないことを表します。

Windows PCと同期してみる

タスクトレイの"日時"を右クリックし、「日時を調整する」をクリック。
f:id:tksfj17:20220320111953j:plain
「その他の時計」をクリック。
f:id:tksfj17:20220320112000j:plain
「インターネット時刻」タブをクリックし、「設定の変更」をクリック。
f:id:tksfj17:20220320112032j:plain
「サーバー」欄にラズパイのIPアドレスを入力して「今すぐ更新」をクリック。
「〇〇と同期しています。お待ちください」と表示されます。
f:id:tksfj17:20220320112048j:plain
うまく設定できていれば数秒で「時計は正常に 〇〇(日時)に〇〇(NTPサーバ名)と同期しました。」と表示されます。
f:id:tksfj17:20220320112105j:plain