Open26
NTPサーバーを立ててみる。

NTPサーバーを立てて、サーバー間での時間を合わせて見たいと思います。

参考記事はこちら

今回はCTで立てて見ます。
概要

ntpインストールします
apt -y install ntp

設定の変更を行います。
vi /etc/ntp.conf

内容
# 21行目 : デフォルト設定はコメントにして自身のタイムゾーンの NTP サーバーを追記
#pool 0.ubuntu.pool.ntp.org iburst
#pool 1.ubuntu.pool.ntp.org iburst
#pool 2.ubuntu.pool.ntp.org iburst
#pool 3.ubuntu.pool.ntp.org iburst
# Use Ubuntu's ntp server as a fallback.
#pool ntp.ubuntu.com
pool ntp.nict.jp iburst
# 51行目 : 時刻同期を許可する範囲を追記
restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap

ntpを再起動させます
systemctl restart ntp

動作確認
root@ntp:~# 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.000
*ntp-a2.nict.go. .NICT. 1 u 44 64 3 10.003 -0.017 0.128
+ntp-a3.nict.go. .NICT. 1 u 44 64 3 10.049 -0.024 0.115
-ntp-k1.nict.jp .NICT. 1 u 41 64 3 9.243 -0.039 0.116
-ntp-b2.nict.go. .NICT. 1 u 41 64 3 9.940 -0.135 0.012
+ntp-b3.nict.go. .NICT. 1 u 39 64 3 9.848 -0.147 0.020

Chronyで構築するらしい。
やり直しですね。
悲しい

ntpをアンインストールします。
apt remove ntp

これを見ながら実装していきます。
Chronyをインストールします。
apt -y install chrony

設定ファイルを書き換えます。
vim /etc/chrony/chrony.conf
# 20行目 : デフォルト設定はコメントにして自身のタイムゾーンの NTP サーバーを追記
#pool ntp.ubuntu.com iburst maxsources 4
#pool 0.ubuntu.pool.ntp.org iburst maxsources 1
#pool 1.ubuntu.pool.ntp.org iburst maxsources 1
#pool 2.ubuntu.pool.ntp.org iburst maxsources 2
pool ntp.nict.jp iburst
# 最終行に追記 (時刻同期を許可する範囲)
allow 10.0.0.0/24

再起動させるお
root@ntp:~# systemctl restart chrony

動作確認
root@ntp:~# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^+ ntp-a2.nict.go.jp 1 6 17 10 -106us[ -122us] +/- 5230us
^- ntp-b3.nict.go.jp 1 6 17 9 +61us[ +61us] +/- 5121us
^+ ntp-b2.nict.go.jp 1 6 17 10 -1103ns[ -17us] +/- 5204us
^* ntp-k1.nict.jp 1 6 17 10 +47us[ +30us] +/- 4669us

次はクライアントにntpサーバーを登録させます。

デフォルトでntpサーバーを読み込むやつがあるらしい。
とりあえずステータス確認。
root@ntp:~# systemctl status systemd-timesyncd
* systemd-timesyncd.service
Loaded: masked (Reason: Unit systemd-timesyncd.service is masked.)
Active: inactive (dead)
Condition: start condition failed at Wed 2022-11-02 11:08:14 UTC; 39min ago
Nov 02 11:08:14 ntp systemd[1]: Condition check resulted in Network Time Synchronization being skipped.
こけてる

とりあえずsystemctlでstartして見ます。
root@ntp:~# systemctl start
Too few arguments.
こけてますね
root@ntp:~# systemctl status systemd-timesyncd
* systemd-timesyncd.service
Loaded: masked (Reason: Unit systemd-timesyncd.service is masked.)
Active: inactive (dead)
Condition: start condition failed at Wed 2022-11-02 11:08:14 UTC; 39min ago
Nov 02 11:08:14 ntp systemd[1]: Condition check resulted in Network Time Synchronization being skipped.

一旦
インストールし直して見ます。
sudo apt install systemd-timesyncd
timedatectl set-ntp true
sudo systemctl restart systemd-timesyncd.service

こけました。

aptをupdateして見ます
sudo apt update
sudo apt upgrade

とりあえず、utcになっているものをjstに変えます。
sudo timedatectl set-timezone Asia/Tokyo
変わりました
root@ntp:~# date
Wed Nov 2 21:30:27 JST 2022

多分、提供する側のサーバーは完成したので、マイルド7のサーバーにntpを登録して見ます。

おそらくこけていた原因は、提供する側のサーバーにntpの受信の設定をしたため?

とりあえずいつもの
apt install chrony
apt update
apt upgrade
apt install vim

設定をしていきます。
vim /etc/chrony/chrony.conf
# Welcome to the chrony configuration file. See chrony.conf(5) for more
# information about usable directives.
# Include configuration files found in /etc/chrony/conf.d.
confdir /etc/chrony/conf.d
# Use Debian vendor zone.
pool 172.16.12.220 iburst
# Use time sources from DHCP.
sourcedir /run/chrony-dhcp
# Use NTP sources found in /etc/chrony/sources.d.
sourcedir /etc/chrony/sources.d
# This directive specify the location of the file containing ID/key pairs for
# NTP authentication.
keyfile /etc/chrony/chrony.keys
# This directive specify the file into which chronyd will store the rate
# information.
driftfile /var/lib/chrony/chrony.drift
# Save NTS keys and cookies.
ntsdumpdir /var/lib/chrony
# Uncomment the following line to turn logging on.
#log tracking measurements statistics
# Log files location.
logdir /var/log/chrony
# Stop bad estimates upsetting machine clock.
maxupdateskew 100.0
# This directive enables kernel synchronisation (every 11 minutes) of the
# real-time clock. Note that it can’t be used along with the 'rtcfile' directive.
rtcsync
# Step the system clock instead of slewing it if the adjustment is larger than
# one second, but only in the first three clock updates.
makestep 1 3
# Get TAI-UTC offset and leap seconds from the system tz database.
# This directive must be commented out when using time sources serving
# leap-smeared time.
leapsectz right/UTC
8行目を自分たちの作成したntpサーバーにしました。
pool 172.16.12.220 iburst

サービスの再起動
root@mildseven:~# systemctl restart chrony
動作確認
root@mildseven:~# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 172.16.12.220 2 6 17 4 -3837ns[ -36us] +/- 4729us
ちゃんと動いていそう。
よかった