Windows 10 PC を NTP サーバ にする方法

Windows 10 PC を NTPサーバにする方法について述べます。

まず、NTPサーバのサービス自体は Windows 10 に最初から入っていますが、デフォルトでは使えないようになっています。

レジストリを変更して使えるようにしないといけません。

使えるようにしてもサーバとして使いたいわけですからサービスを有効しする必要がありますが、デフォルトでは有効になっていません。

サービスを有効にしても、ファイヤーウォールによって外部からのアクセスが遮断されるため、使えません。

なので、NTPサーバを使えるようにし、サービスを有効にし、外部からアクセスできるようにする方法について述べます。

NTPサーバを有効にする

Windows に入っているNTPサーバを有効にするにはレジストリを2箇所書き換える必要があります。

Windowsキー + rキーを押すと「ファイル名を指定して実行」というアプリケーションが起動しますので、 regedit と入力しOKを押します。

タイトル

すると、レジストリエディタが起動しますので、上部のURLのところに以下を入力しEnterを押下します。

コンピューター\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer

タイトル

すると、以下に移動しますので、その中からEnabledを見つけダブルクリックします。

タイトル

ダイアログが表示されますので、値を0から1に変更します。

タイトル

これで一か所目は終了です。

次は、 コンピューター\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config
に移動し、AnnounceFlags をダブルクリックします。

タイトル

するとダイアログが表示されますので、 値を a から 5 に変更します。

タイトル

これでレジストリの変更は終了です。

管理者権限でコマンドプロンプトを開き、以下のコマンドでNTPサーバソフト(w32time)を再起動するとNTPサーバが有効になります。

C:\Windows\system32>net stop w32time

Windows Time サービスを停止中です.
Windows Time サービスは正常に停止されました。

C:\Windows\system32>net start w32time

Windows Time サービスを開始します.
Windows Time サービスは正常に開始されました。

この時、コマンドプロンプトを「管理者として実行」で実行しないと、

システム エラー 5 が発生しました。
アクセスが拒否されました。

と システム エラー 5 が発生するので注意しましょう。

ポートに穴を開けて外部からアクセスできるようにする

せっかくNTPサーバをたててもファイヤーウォールがあるため外部からアクセスできません。
よって、NTPサーバのポート番号である UDP の 123 番 を開ける設定をします。

コントロールパネルを開かないといけないのですが、意外に面倒で、私は検索窓に"コントロール パネル"と入力してでてきた「コントロール パネル」に進みます。
その後、システムとセキュリティ -> Windows Defender ファイアウォール と進みます。

または、検索窓にファイヤーウォールと入力したら「Windows Defender ファイアウォール」がサジェストで出てくるのでそれで一気に進んでも問題ないです。

出てきたら詳細設定を選び、
タイトル

受信の規則の新しい規則を選びます。

タイトル

あとは以下の通り、ポート、UDP、123、接続を許可する、と進み、
タイトル タイトル タイトル

プロファイルはデフォルトのままで、
タイトル

最後に任意の名前を付けます。
タイトル

以上でUDP の 123 は正しく開きました。

NTPサーバを自動起動するようにする

このままだとパソコンを再起動したらNTPサーバが落ちたままになります。

ので、サービス設定をする必要があります。

検索窓に「サービス」と入力するとサービスが表示されるのでクリックし、その中から、 Windows Time を選択します。
タイトル

スタートアップの種類を 自動(遅延開始) にします。
タイトル

これでOKです。
再起動して起動することを確認しましょう。

ちなみにですが、遅延起動、すごい待たされました。
起動する前にNTPサーバにアクセスしても当然タイムアウトするので注意しましょう。

動作確認(別のWindowsPCから)

動作確認といっても、別のWindowsPCからこのPCのアドレスを設定すれば良いだけです。

コントロールパネルから 時計と地域 -> 日付と時刻の設定 に進みます。
(時刻を右クリックしたら出てくる日付と時刻の調整、じゃないので注意が必要です。日付と時刻、の画面です)
タイトル

設定の変更から、サーバを変更すればよいだけです。
例えばNTPサーバの動いているPCのアドレスが 192.168.1.2ならそれを入れる、ということです。
タイトル

で、NTPサーバ側の時刻を勝手に変えてから同期したらこっちのPCの時間も変わる、というのを確認しましょう。

動作確認(Linuxから)

WindowsはさっきのようにGUIでやりましたが、Ubuntuなどの場合は以下のように設定ファイルを書き換えれば可能です。

設定ファイルにNTPサーバを指定して、サービスをリスタートしましょう。
ここではNTPサーバのIPが192.168.1.2だとします。

$ sudo vi /etc/systemd/timesyncd.conf
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.

[Time]
NTP=192.168.1.2
#FallbackNTP=ntp.ubuntu.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

で、

$ sudo systemctl restart systemd-timesyncd.service

これでうまくいっていたら、

$ sudo systemctl status systemd-timesyncd.service
● systemd-timesyncd.service - Network Time Synchronization
     Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2023-02-02 13:10:30 JST; 2s ago
       Docs: man:systemd-timesyncd.service(8)
   Main PID: 352137 (systemd-timesyn)
     Status: "Initial synchronization to time server 192.168.1.2:123 (192.168.1.2)."
      Tasks: 2 (limit: 18928)
     Memory: 1.8M
     CGroup: /system.slice/systemd-timesyncd.service
             └─352137 /lib/systemd/systemd-timesyncd

 2月 02 13:10:30 ntp-pc systemd[1]: Starting Network Time Synchronization...
 2月 02 13:10:30 ntp-pc systemd[1]: Started Network Time Synchronization.
 2月 02 13:10:30 ntp-pc systemd-timesyncd[352137]: Initial synchronization to time server 192.168.1.2:123 (192.168.1.2).

と表示されます。

NTPサーバ(Windows側のほう)の時計を変更するなどして、Linux側の時刻が変わることを確認しましょう。

Server has too large root distance. Disconnecting.

なんか、 Server has too large root distance. Disconnecting. とかいうエラーが出るんですけど。

$ sudo systemctl status systemd-timesyncd.service
● systemd-timesyncd.service - Network Time Synchronization
     Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2023-02-02 13:06:48 JST; 2s ago
       Docs: man:systemd-timesyncd.service(8)
   Main PID: 352051 (systemd-timesyn)
     Status: "Idle."
      Tasks: 2 (limit: 18928)
     Memory: 1.5M
     CGroup: /system.slice/systemd-timesyncd.service
             └─352051 /lib/systemd/systemd-timesyncd

 2月 02 13:06:48 ntp-pc systemd[1]: Starting Network Time Synchronization...
 2月 02 13:06:48 ntp-pc systemd[1]: Started Network Time Synchronization.
 2月 02 13:06:48 ntp-pc systemd-timesyncd[352051]: Server has too large root distance. Disconnecting.

解決方法がわからなかったのですが、とりあえず、 timesyncd.conf の RootDistanceMaxSec のコメントを外し、値を 5から30にして、

$ sudo vi /etc/systemd/timesyncd.conf
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.

[Time]
NTP=192.168.1.2
#FallbackNTP=ntp.ubuntu.com
RootDistanceMaxSec=30
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

NTPクライアントサービスを再起動すると、

$ sudo systemctl restart systemd-timesyncd.service

正しく動くようになった。

$ sudo systemctl status systemd-timesyncd.service
● systemd-timesyncd.service - Network Time Synchronization
     Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2023-02-02 13:10:30 JST; 2s ago
       Docs: man:systemd-timesyncd.service(8)
   Main PID: 352137 (systemd-timesyn)
     Status: "Initial synchronization to time server 192.168.1.2:123 (192.168.1.2)."
      Tasks: 2 (limit: 18928)
     Memory: 1.8M
     CGroup: /system.slice/systemd-timesyncd.service
             └─352137 /lib/systemd/systemd-timesyncd

 2月 02 13:10:30 ntp-pc systemd[1]: Starting Network Time Synchronization...
 2月 02 13:10:30 ntp-pc systemd[1]: Started Network Time Synchronization.
 2月 02 13:10:30 ntp-pc systemd-timesyncd[352137]: Initial synchronization to time server 192.168.1.2:123 (192.168.1.2).

時刻を自動的に設定する、をいじらない

せっかくサービスで自動起動するようにしたのですが、以下のスイッチを切り替えるとなぜか勝手に「手動」に切り替えられてしまします。

OFFにしたときに手動になるのは仕方ないとして、ONに戻したら自動起動にしてほしいものです。

いつの間にか手動になってしまって、Windows PCを再起動したときにNTPサーバが立ち上がってない、という問題が起きてしまい悲しい思いをします。

ので、サービス設定後、このスイッチは触らない、または触ったらサービスも再度設定しなおすようにしましょう。

オフにして...
タイトル

オンすると...

タイトル

?????手動に戻ってる!?

タイトル

という謎問題が起きましたのでやめましょう。

以上。