ntpd の設定

cacti の syslog プラグインで全サーバーの syslog を一括管理していたら、サーバー間連携するような処理のログ出力順序がワリとグダグダだったというおはなし……

なにはなくとも、まず emerge です。

# emerge -av ntp

These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild  N     ] net-misc/ntp-4.2.8_p9::gentoo  USE="ipv6 readline ssl threads -caps -debug (-libressl) -openntpd -parse-clocks -samba (-selinux) -snmp -vim-syntax -zeroconf" 7087 KiB

Total: 1 package (1 new), Size of downloads: 7087 KiB

Would you like to merge these packages? [Yes/No] y

USE は特に指定せず、デフォルトのままです。

/etc/ntp.conf を設定していきます。

--- /etc/ntp.conf.orig  2017-04-02 17:21:22.373000000 +0900
+++ /etc/ntp.conf       2017-04-02 17:25:53.977000000 +0900
@@ -11,10 +11,9 @@
 #server pool.ntp.org
 
 # Pools for Gentoo users
-server 0.gentoo.pool.ntp.org
-server 1.gentoo.pool.ntp.org
-server 2.gentoo.pool.ntp.org
-server 3.gentoo.pool.ntp.org
+server -4 ntp.nict.jp
+server -4 ntp.jst.mfeed.ad.jp
+server -4 s2csntp.miz.nao.ac.jp
 
 ##
 # A list of available servers can be found here:

これ、デフォルトのまま使ったことないんですけど、書いてある ntp サーバーって生きてる ntp サーバーなんですかね……w うちの環境ではデフォルトのサーバーを削除して、以下の順番で追加しています。趣味ですね。

  1. ntp.nict.jp 情報通信研究機構 いわゆる NICT
  2. s2csntp.miz.nao.ac.jp 国立天文台 水沢キャンパス 天文保時室 naoj
  3. ntp.jst.mfeed.ad.jp インターネットマルチフィード MFEED

-4 というオプションは “ipv4 を使う” という意味です。

公開するわけではなく、自分自身が時刻あわせをしたいだけなので、こんな感じにします。

@@ -41,9 +40,11 @@ driftfile    /var/lib/ntp/ntp.drift
 # Default configuration:
 # - Allow only time queries, at a limited rate, sending KoD when in excess.
 # - Allow all local queries (IPv4, IPv6)
-restrict default nomodify nopeer noquery limited kod
+restrict default ignore
 restrict 127.0.0.1
-restrict [::1]
+restrict ntp.nict.jp nomodify nopeer noquery notrap limited kod
+restrict ntp.s2csntp.miz.nao.ac.jp nomodify nopeer noquery notrap limited kod
+restrict ntp.jst.mfeed.ad.jp nomodify nopeer noquery notrap limited kod
 
 
 # To allow machines within your network to synchronize

restrict default で、とりあえず全てのアクセスを遮断します。その上で restrict 127.0.0.1 を指定して自身からのアクセスを許可しています。restrict [::1] は……現状まだまだ ipv6 って使いづらいですよね、消しちゃいます。

最後の3行は、自分が server で指定した公開 npt サーバーを使っての時刻同期を許可する設定です。

ちなみに LAN 内のサーバーに時刻同期を許可する場合は、こんな1行を追加すれば OK です。

restrict 192.168.1.0 mask 255.255.255.0 nomodify nopeer noquery notrap limited kod

もちろん、ネットワークアドレスとマスクは環境にあわせて適宜書き換えます。

今までの設定を入れて ntpd を起動します。

# systemctl start ntpd

……とか

# /etc/init.d/ntpd start

……とか。

起動してしばらくしたら ntpq コマンドで状態を確認します。

# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*ntp-b3.nict.go. .NICT.           1 u  121  128  377   10.991   -0.442   0.585
+133.40.41.134   133.40.41.133    2 u  111  128  377   20.191    0.101   0.856
+ntp2.jst.mfeed. 133.243.236.17   2 u  112  128  377    8.533    3.092   0.544

少なくとも、いちばん左側に * が付いた行があれば時刻同期は成功しています。 * は現在同期中のサーバー、+ は今は同期していないけれど、現在同期中のサーバーになにかあればいつでも同期できる状態良好なサーバー……ぐらいの意味です。

そんな時はログ出力してみましょう。

logfile /var/log/ntp/ntpd.log
logconfig =all

こんな風に /etc/ntp.conf に書いてあげます1)。なにかわかるかもしれません。もしくはntpq のインタラクティブモードを使ってデバッグするのもアリだと思います。

時刻同期できることが確認できたら、起動時に勝手に上がってくるように設定します。

systemd ならこんな感じ。

# systemctl enable ntpd

sysVinit ならこんな感じでしょうか。

# rc-update add ntpd default

こんな感じで。


1)
logconfig=all ではなく logconfig =all です。
  • wiki/linux/setup_ntpd
  • 最終更新: 2019/02/17 15:59