CentOS6.9のntp.confのデフォルトの設定です。しかし、CentOS6のマイナーバージョンもついに9になりましたね。
そして、CentOS6.9でntp.confのrestrictのコメントアウトの組み合わせの時刻同期動作確認をしてみました。というのがネットで、コメントアウトについて色々と書かれていて、どれが正解なのかということで、実際に設定を変更して動作確認しました。
まずは、ntp.confファイルのデフォルト設定の抜粋です。
-----
[root@ntp etc]# cat /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
server 192.168.9.137
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
----
設定の動作確認やっていきたいと思います。まず、デフォルトの状態では、ntpdは起動していないので、以下のコマンドでサービスを起動します。
#service ntpd start
サーバ再起動時に、ntpdを自動起動させる設定は以下です。
#chkconfig ntpd on
続いて、デフォルトの状態で時刻同期を確認。「ntp.ix.ru」と時刻同期されていますね。
[root@ntp etc]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
+ns1.blazing.de 213.172.96.14 2 u 119 64 356 292.833 2239.70 1242.75
+SunSITE.icm.edu 194.146.251.100 2 u 47 64 377 317.113 2500.82 1048.46
*ntp.ix.ru .PPS. 1 u 13 64 377 285.344 2584.88 1045.65
+routerida1.sopr 133.243.238.244 2 u 17 64 377 54.319 2590.69 1042.86
続いて、ネットで見つけた以下の2行をコメントアウト。
#restrict default kod nomodify notrap nopeer noquery
#restrict -6 default kod nomodify notrap nopeer noquery
ntp000000
そして、忘れずサービス再起動し、再び確認。
[root@ntp etc]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*bwntp2.bluewin. 195.186.150.242 2 u 3 64 1 289.978 -5.255 23.898
+5751b502.skybro 85.199.214.98 2 u 6 64 1 288.736 26.838 15.055
-time-b.as43289. 162.23.41.56 2 u 5 64 1 353.976 -75.293 14.495
+ntp2.ds.network 213.172.105.106 3 u 4 64 1 278.897 25.559 18.518
問題なく時刻同期できました。続いて、ipv6の行をコメントアウトしてみました。
#restrict -6 default kod nomodify notrap nopeer noquery
#restrict -6 ::1
ntp000001
結果は、ローカルシステムがサービスにアクセスできずに、タイムアウト。
[root@ntp etc]# ntpq -p
localhost: timed out, nothing received
***Request timed out
ntp000002
#restrict default kod nomodify notrap nopeer noquery
#restrict 127.0.0.1
こちらは問題なく時刻同期できました。
[root@ntp etc]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
-122.50.87.109.t 193.27.209.1 3 u 26 64 1 355.743 25.110 14.620
+dedibox.demonge 17.253.34.253 2 u 23 64 1 313.787 55.791 19.180
+ntp10.icmp.dk 173.34.166.174 2 u 25 64 1 348.258 37.048 18.505
*stratum2-1.NTP. 129.70.130.71 2 u 22 64 1 331.126 49.313 12.937
昔の設定例だとrestrict default ignoreとしてるものが多いそうですが、この設定が意外と理解が難しい。
色々と調べていて分かりやすく説明されていた内容としては、restrictが通信を制限をする行になるわけですが、
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
上の2つの restrict で外部からの問い合わせを制限していて、下の2行で、自分自身からの問い合わせには応じるよう設定しているという内容で、最初の行がIPv4、次の行がIPv6です。
今回の動作確認では、IPv6を利用していないというのが前提。つまり、デフォルトの設定では、外部からの問い合わせは拒否、そして、内部からの問い合わせは応じるという設定になっていることになります。仮に、コメントアウトをすると、その設定が無効になりますので、制限がなくなるという考え方になります。
上の確認でもれていましたが、すべてコメントアウトした場合は、問題なく時刻同期できました。つまり、制限がない状態です。
では、最初にIPv6をコメントアウトして通信に失敗した理由は???
調査したところ、hostsファイルのipv6が無効になっていないのが原因でした。hostsファイルのipv6を無効にすれば、問題なく時刻同期できるようになりました。
[root@ntp etc]# cat hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
#::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
動作確認はやっぱり重要ですね。