Raspberry Pi OS NTP 設定

2023/12/14 13:58 OS別::Linuxその他::技術情報
Raspberry Pi を利用したサーバの、初期に行うべき手順で NTP を設定します。
対象は、Raspbian GNU/Linux 12 (bookworm) になります。

パッケージ

インストール

パッケージのインストール状況を確認して、必要であればインストールします。
$ sudo apt list ntp ntp-doc ntpdate dnsutils
$ sudo apt install ntp ntp-doc ntpdate dnsutils

名前解決確認

名前解決ができることを確認します。
$ dig ntp.nict.jp +short

NTP 設定

設定差分の読み替え設定

$ . /etc/os-release
$ pNtpConfDir=""
$ case ${VERSION_ID} in
>   12 ) pNtpConf="/etc/ntpsec/ntp.conf" ;;
>    * ) pNtpConf="/etc/ntp.conf" ;;
> esac

設定ファイル修正

$ sudo cp -p ${pNtpConf}{,$(date +%Y%m%d)}
$ sudo diff ${pNtpConf}{,$(date +%Y%m%d)}
$ sudo vi ${pNtpConf}
---------------------------------------------------------
(pool をコメントアウトして、serverに ntp.nict.jp を指定する)
---------------------------------------------------------
$ sudo diff ${pNtpConf}{,$(date +%Y%m%d)}
23,27c23,27
< #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
< server ntp.jimba.jp iburst
---
> 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
>
39c39
< #restrict -6 default kod notrap nomodify nopeer noquery limited
---
> restrict -6 default kod notrap nomodify nopeer noquery limited
43c43
< #restrict ::1
---
> restrict ::1

設定の反映

$ sudo systemctl status ntp
$ sudo systemctl stop ntp
$ sudo ntpdate ntp.nict.jp
$ sudo systemctl start ntp
$ sudo systemctl status ntp
もし status 確認で次のようなエラーが出力されていたら、ディレクトリを作成する。
11月 05 11:01:02 rasp012 ntpd[1385]: statistics directory /var/log/ntpsec/ does not exist or is unwriteable, error No such fi>
$ grep ntp /etc/passwd
ntpsec:x:112:122::/nonexistent:/usr/sbin/nologin
$ sudo mkdir /var/log/ntpsec
$ sudo chown 112:122 /var/log/ntpsec
$ ls -ld /var/log/ntpsec
drwxr-xr-x 2 ntpsec ntpsec 4096 11月  5 11:02 /var/log/ntpsec
$ sudo systemctl status ntp
$ sudo systemctl restart ntp
$ sudo systemctl status ntp
$ ls -l /var/log/ntpsec

設定反映確認

$ ntpq -p
     remote                                   refid      st t when poll reach   delay   offset   jitter
=======================================================================================================
+ntp-a3.nict.go.jp                       .NICT.           1 u   42   64  377   7.1691 9051.364   1.0737
ntp は、ホスト名に * が付いているサーバに同期している。
また、IPアドレスで確認したい場合は、オプションに -n を追加して ntpq -np 等とする。

その他

NTP サーバとして利用するなら追加設定が必要なので、別途 NTP サーバとしての設定を行う。
例えば、次のような認証を行う。
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap