LoginSignup
11
11

More than 5 years have passed since last update.

さくらのVPSでntpが繋がってなかった件

Last updated at Posted at 2015-12-15

(後日談ありです。結局 IPv6 の通信不良に振り回されていただけでした…。記事の最後を見て貰えれば。)

何気なしに、さくらのVPSでのNTP稼働状況を見て見たら…

$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 2001:e42:0:1:21 .INIT.          16 u    - 1024    0    0.000    0.000   0.000

あれ?同期してないの??

この時の、/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 ignore
restrict -6 default ignore
restrict ntp1.sakura.ad.jp 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 ntp1.sakura.ad.jp iburst

#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

ほぼ 【重要】NTPの脆弱性について | さくらインターネット で紹介されている通りになってます。

「.INIT.」となる理由はNTPサーバに到達できないというところで

  • パケットが遮断されている?
  • そもそも ntp1.sakura.ad.jp には繋がらない?

という辺りを疑い…。

パケットが遮断されている?

まずは、パケットが遮断されているのかも??というところで、iptablesをいじってみた。

-A INPUT -p udp --sport 123 --dport 123 -j ACCEPT

NTPはUDPの123番ポートで通信するので、UDP:123を解放。
ついでに、 service ntpd restart してしばらく待つ…小一時間待つ…

$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 2001:e42:0:1:21 .INIT.          16 u    - 1024    0    0.000    0.000   0.000

変わらん!

そして、ふとremote欄を見て… ntp1.sakura.ad.jp って、IPv6なんだ・・・IPv6!?
もしかしたら、IPv6で上手くいっていないのかも??

- server ntp1.sakura.ad.jp iburst
+ server -4 ntp1.sakura.ad.jp iburst

/etc/ntp.conf で server ntp1.sakura.ad.jp を IPv4 固定で指定してみる。

そして、待つ。沢山待つ。

$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 ntp1.sakura.ad. .INIT.          16 u    -   64    0    0.000    0.000   0.000

変わらん!

そもそも ntp1.sakura.ad.jp には繋がらない?

そもそも、根幹の所で ntp1.sakura.ad.jp に繋がらないんじゃないの??
ということで、 ntpdate コマンドで試してみる。

# ntpdate ntp1.sakura.ad.jp
15 Dec 10:33:18 ntpdate[24953]: the NTP socket is in use, exiting

# service ntpd stop
Shutting down ntpd:                                        [  OK  ]

# ntpdate ntp1.sakura.ad.jp
15 Dec 10:33:34 ntpdate[24972]: adjust time server 210.188.224.14 offset 0.080671 sec

ntpd起動したっままだと、ダメだったので一旦サービスを止めてやってみると・・・
繋がってるね。。。

# vi /etc/sysconfig/iptables
(UDP:123の解放をコメントアウト)

# service iptables restart
iptables: Setting chains to policy ACCEPT: filter nat      [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]

# ntpdate ntp1.sakura.ad.jp
15 Dec 10:34:09 ntpdate[25069]: adjust time server 210.188.224.14 offset 0.065437 sec

じゃ、iptablesで解放したUDP:123を消したらどうなるか?
・・・繋がるね。。

そしたら、やっぱ待てばいいのかな・・・?

# service ntpd start
Starting ntpd:                                             [  OK  ]

待つ・・・

# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 ntp1.sakura.ad. .INIT.          16 u    -   64    0    0.000    0.000   0.000

.INIT. ........

そもそも設定を見直してみよう

/etc/ntp.conf に設定されている有効行だけ抜き出すと

driftfile /var/lib/ntp/drift
restrict default ignore
restrict -6 default ignore
restrict ntp1.sakura.ad.jp kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
server ntp1.sakura.ad.jp iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

こんな感じ。

項目 内容
driftfile /var/lib/ntp/drift ローカルマシンのズレの傾向を記録するファイル
restrict default ignore NTP通信をデフォルトでは拒否(IPv4)
restrict -6 default ignore NTP通信をIPv6においてデフォルトでは拒否
restrict ntp1.sakura.ad.jp kod nomodify notrap nopeer noquery ntp1.sakura.ad.jpとのNTP通信をオプション(kod nomodify notrap nopeer noquery)として許可(ホスト名指定)
restrict 127.0.0.1 127.0.0.1とのNTP通信を全て許可(IPv4)
restrict -6 ::1 ::1(IPv6)とのNTP通信を全て許可
server ntp1.sakura.ad.jp iburst ntp1.sakura.ad.jp NTPサーバと一方的に同期(ホスト名指定 → ntpqで見るとIPv6)
includefile /etc/ntp/crypto/pw /etc/ntp/crypto/pw ファイルをインクルード
keys /etc/ntp/keys キーファイルの指定

日本語で意味を付けて見るとこんな感じかな。
これで見ると、ホスト名で指定するとIPv4とIPv6の扱いが不定になってそう?

server ntp1.sakura.ad.jp iburst の設定では、

IPv? ntp1.sakura.ad.jp 適用ルール アクセス
IPv4 ntp1.sakura.ad.jp restrict ntp1.sakura.ad.jp kod nomodify notrap nopeer noquery 許可
IPv6 ntp1.sakura.ad.jp restrict -6 default ignore 拒否

となりそうだけど、そうでもない反応。

IPv4かIPv6か明示しちゃおう

上記をふまえて、IPv4なのかIPv6なのか明示してみる。

driftfile /var/lib/ntp/drift
restrict default ignore
restrict -6 default ignore
- restrict ntp1.sakura.ad.jp kod nomodify notrap nopeer noquery
+ restrict -4 ntp1.sakura.ad.jp kod nomodify notrap nopeer noquery
+ restrict -6 ntp1.sakura.ad.jp kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
- server ntp1.sakura.ad.jp iburst
+ server -4 ntp1.sakura.ad.jp iburst
+ server -6 ntp1.sakura.ad.jp iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

そして、再起動して確認

# service ntpd restart
Shutting down ntpd: 
                                                           [  OK  ]
Starting ntpd: 
                                                           [  OK  ]

# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*ntp1.sakura.ad. 10.84.87.146     2 u    -   64    1   20.635  -27.538   0.178
 2001:e42:0:1:21 .INIT.          16 u    -   64    0    0.000    0.000   0.000

IPv4側繋がってる-。
IPv6側はダメか…

# ntpdate -6d ntp1.sakura.ad.jp
15 Dec 11:39:13 ntpdate[1649]: ntpdate 4.2.6p5@1.2349-o Mon Oct 26 15:32:20 UTC 2015 (1)
Looking for host ntp1.sakura.ad.jp and service ntp
host found : 2001:e42:0:1:210:188:224:14
transmit(2001:e42:0:1:210:188:224:14)
transmit(2001:e42:0:1:210:188:224:14)
transmit(2001:e42:0:1:210:188:224:14)
transmit(2001:e42:0:1:210:188:224:14)
transmit(2001:e42:0:1:210:188:224:14)
2001:e42:0:1:210:188:224:14: Server dropped: no data
server 2001:e42:0:1:210:188:224:14, port 123
stratum 0, precision 0, leap 00, trust 000
refid [2001:e42:0:1:210:188:224:14], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time:    00000000.00000000  Mon, Jan  1 1900  9:00:00.000
originate timestamp: 00000000.00000000  Mon, Jan  1 1900  9:00:00.000
transmit timestamp:  da19fbd4.e349de85  Tue, Dec 15 2015 11:39:16.887
filter delay:  0.00000  0.00000  0.00000  0.00000
         0.00000  0.00000  0.00000  0.00000
filter offset: 0.000000 0.000000 0.000000 0.000000
         0.000000 0.000000 0.000000 0.000000
delay 0.00000, dispersion 64.00000
offset 0.000000

15 Dec 11:39:17 ntpdate[1649]: no server suitable for synchronization found

どうもできないな…
この辺は後で調べよう。。。

まとめ

とりあえず、IPv4で同期出来るようになった。
今の /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 ignore
restrict -6 default ignore
restrict -4 ntp1.sakura.ad.jp kod nomodify notrap nopeer noquery
#restrict -6 ntp1.sakura.ad.jp 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 -4 ntp1.sakura.ad.jp iburst
#server -6 ntp1.sakura.ad.jp iburst

#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

IPv6での接続はコメントアウト中。
解決したら復活させよう。

(後日談)IPv6...

NTPとは関係なくIPv6を調べていたら、こっちもそもそも通信できていない事が発覚。。。
eth0にはIPv6のアドレスが割り当てられているし、/etc/sysconfig/networkとか/etc/sysconfig/network-script/ifcfg-eth0のファイルを見直しても、特に問題は無さそう…。
でも、ping6コマンドでどこにも抜けられない。
小一時間 ip6tables と iptables を、あーでもない、こーでもないと弄ってみたけど改善せず。。
むしろド坪ってしまったので、VPS自体をシャットダウン→起動としてみたら・・・




繋がった。 Σ( ̄□ ̄;
理由は不明だけど、VPSをシャットダウン→起動するとネットワークもろとも再構成して良い具合になるって事かな??

というわけで、

- #restrict -6 ntp1.sakura.ad.jp kod nomodify notrap nopeer noquery
+ restrict -6 ntp1.sakura.ad.jp kod nomodify notrap nopeer noquery
- #server -6 ntp1.sakura.ad.jp iburst
+ server -6 ntp1.sakura.ad.jp iburst

と、IPv6の設定をコメントアウトから解放してみたら

# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+ntp1.sakura.ad. 10.84.87.146     2 u   64   64   77   20.258    0.344   2.209
*2001:e42:0:1:21 10.84.87.146     2 u   64   64   77   17.579   -1.362   1.028

繋がりましたとさ。

改めてまとめ…

  • IPv6絡みでおかしそうなら、少し足掻いて見て、ダメならとりあえずVPS毎シャットダウン→起動
  • ntp.conf は結局元のままでよかった

ということですね。。

11
11
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11