NTP

提供: Wikinote
2010年7月28日 (水) 00:17時点におけるHagio (トーク | 投稿記録)による版 (参考文献)

移動: 案内検索

Network Time Protocol

やってることは単に時刻を合わせて、時計の進み方を正確にするだけなのに、異様に奥が深い NTP について。 ちなみに、ntp 付属文書の 日本語訳 やってます。いつまでかかることやら。 これ Expert Network Time Protocol: An Experience in Time with NTP (Expert) 誰か翻訳して…。

参考文献

うるう秒関連

メモ

  • ntpd はサーバでもクライアントでも UDP 123 番ポートを用いる。

ツール等

ntpq コマンド

NTP の情報を得るのに最もポピュラーなコマンド。

$ ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
-202.224.32.4    210.171.225.76   2 u  975  256  370    5.759   -1.264   0.162
-133.243.232.68  210.171.226.40   2 u   30  256  377    9.018   -1.209   0.386
+210.173.160.27  210.173.160.86   2 u  209  256  377    5.452    0.021   0.123
+210.173.160.57  133.243.236.18   2 u  215  256  377    5.741    0.272   0.111
*210.173.160.87  210.173.176.4    2 u   38  256  377    5.817   -0.100   0.222
$ ntpq -c as

ind assID status  conf reach auth condition  last_event cnt
===========================================================
  1 59668  9314   yes   yes  none   outlyer   reachable  1
  2 59669  9314   yes   yes  none   outlyer   reachable  1
  3 59670  9414   yes   yes  none  candidat   reachable  1
  4 59671  9414   yes   yes  none  candidat   reachable  1
  5 59672  9614   yes   yes  none  sys.peer   reachable  1
$ ntpq -c rl
assID=0 status=06c4 leap_none, sync_ntp, 12 events, event_peer/strat_chg,
version="ntpd 4.2.2p1@1.1570-o Tue Jun 10 00:07:18 UTC 2008 (1)",
processor="i686", system="Linux/2.6.18-194.el5PAE", leap=00, stratum=3,
precision=-20, rootdelay=6.168, rootdispersion=25.945, peer=59672,
refid=210.173.160.87,
reftime=cff96bee.64899eac  Tue, Jul 27 2010 23:30:06.392, poll=8,
clock=cff96dc6.30334e8f  Tue, Jul 27 2010 23:37:58.188, state=4,
offset=0.057, frequency=44.983, jitter=0.321, noise=0.254,
stability=0.060, tai=0

ntptime コマンド

カーネルの時計関連の情報を表示・設定することができる。

$ ntptime
ntp_gettime() returns code 0 (OK)
  time cd8dcff1.8d374000  Mon, Apr 13 2009 23:51:29.551, (.551624),
  maximum error 20085 us, estimated error 0 us
ntp_adjtime() returns code 0 (OK)
  modes 0x0 (),
  offset 0.000 us, frequency 77.335 ppm, interval 1 s,
  maximum error 20085 us, estimated error 0 us,
  status 0x1 (PLL),
  time constant 6, precision 1.000 us, tolerance 512 ppm,

それぞれの値の意味は…難しいんだこれが。誰か解説本書いてくれないかなあ。

ntp_adjtime() から返っている status 値は、/usr/include/linux/timex.h で定義されている以下の値を取る。

/*     
 * Status codes (timex.status) 
 */    
#define STA_PLL                0x0001  /* enable PLL updates (rw) */
#define STA_PPSFREQ    0x0002  /* enable PPS freq discipline (rw) */
#define STA_PPSTIME    0x0004  /* enable PPS time discipline (rw) */
#define STA_FLL                0x0008  /* select frequency-lock mode (rw) */

#define STA_INS                0x0010  /* insert leap (rw) */
#define STA_DEL                0x0020  /* delete leap (rw) */
#define STA_UNSYNC     0x0040  /* clock unsynchronized (rw) */ ★いわゆる 11 分モードのとき、0 になる
#define STA_FREQHOLD   0x0080  /* hold frequency (rw) */

#define STA_PPSSIGNAL  0x0100  /* PPS signal present (ro) */
#define STA_PPSJITTER  0x0200  /* PPS signal jitter exceeded (ro) */
#define STA_PPSWANDER  0x0400  /* PPS signal wander exceeded (ro) */
#define STA_PPSERROR   0x0800  /* PPS signal calibration error (ro) */

#define STA_CLOCKERR   0x1000  /* clock hardware fault (ro) */

#define STA_RONLY (STA_PPSSIGNAL | STA_PPSJITTER | STA_PPSWANDER | \
    STA_PPSERROR | STA_CLOCKERR) /* read-only bits */

ntpdc コマンド

ちょっとマイナーだけど様々な情報が得られる。

$ ntpdc -pn
     remote           local      st poll reach  delay   offset    disp
=======================================================================
*210.173.160.87  118.243.85.16    2  256  377 0.00552 -0.000084 0.07770
=202.224.32.4    118.243.85.16    2  256  360 0.00575 -0.001264 0.45509
=133.243.232.68  118.243.85.16    2  256  377 0.00900 -0.001209 0.08156
=210.173.160.27  118.243.85.16    2  256  377 0.00545  0.000021 0.12018
=210.173.160.57  118.243.85.16    2  256  377 0.00574  0.000272 0.09195
$ ntpdc -l
client    ntp3.jst.mfeed.ad.jp
client    orion.asahi-net.or.jp
client    udon.nict.go.jp
client    ntp1.jst.mfeed.ad.jp
client    ntp2.jst.mfeed.ad.jp
$ ntpdc -c kerninfo
pll offset:           5.2e-05 s
pll frequency:        44.985 ppm
maximum error:        0.138157 s
estimated error:      0.000237 s
status:               0001  pll
pll time constant:    4
precision:            1e-06 s
frequency tolerance:  512 ppm
$ ntpdc -c loopinfo
offset:               0.000065 s
frequency:            44.985 ppm
poll adjust:          0
watchdog timer:       235 s
$ ntpdc -c sysinfo
system peer:          ntp3.jst.mfeed.ad.jp
system peer mode:     client
leap indicator:       00
stratum:              3
precision:            -20
root distance:        0.00620 s
root dispersion:      0.02278 s
reference ID:         [210.173.160.87]
reference time:       cff96dec.641c9286  Tue, Jul 27 2010 23:38:36.391
system flags:         auth monitor ntp kernel stats 
jitter:               0.000275 s
stability:            0.000 ppm
broadcastdelay:       0.003998 s
authdelay:            0.000000 s
$ ntpdc -c sysstats
time since restart:     6980
time since reset:       6980
packets received:       1415
packets processed:      319
current version:        532
previous version:       0
bad version:            0
access denied:          0
bad length or format:   0
bad authentication:     0
rate exceeded:          0
$ ntpdc -c memstats
time since reset:     7007
total peer memory:    15
free peer memory:     10
calls to findpeer:    533
new peer allocations: 0
peer demobilizations: 0
hash table counts:      0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        2   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   1   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   1   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   1   0   0   0   0   0
                        0   0   0   0   0   0   0   0
$ ntpdc -c iostats
time since reset:     7116
receive buffers:      10
free receive buffers: 9
used receive buffers: 0
low water refills:    1
dropped packets:      0
ignored packets:      0
received packets:     1432
packets sent:         2151
packets not sent:     0
interrupts handled:   1414
received by int:      1414
$ ntpdc -c timerstats
time since reset:  7133
alarms handled:    0
alarm overruns:    0
calls to transmit: 0
$ ntpdc -c reslist
   address          mask            count        flags
=====================================================================
0.0.0.0         0.0.0.0               320  noquery, nomodify, nopeer, notrap, kod
y085016.dynamic 255.255.255.255         0  ntpport, interface, ignore
localhost.local 255.255.255.255       911  none
localhost.local 255.255.255.255         0  ntpport, interface, ignore
192.168.1.0     255.255.255.0          44  nomodify, notrap
lab.hagio.org   255.255.255.255         0  ntpport, interface, ignore
192.168.8.0     255.255.255.0         167  nomodify, notrap
192.168.8.1     255.255.255.255         0  ntpport, interface, ignore
192.168.10.1    255.255.255.255         0  ntpport, interface, ignore
::              ::                      0  none
localhost6.loca ffff:ffff:ffff:         0  ntpport, interface, ignore
fe80::20a:79ff: ffff:ffff:ffff:         0  ntpport, interface, ignore
fe80::21f:d0ff: ffff:ffff:ffff:         0  ntpport, interface, ignore
fe80::250:56ff: ffff:ffff:ffff:         0  ntpport, interface, ignore
fe80::250:56ff: ffff:ffff:ffff:         0  ntpport, interface, ignore
$ ntpdc -c monlist
remote address          port local address      count m ver code avgint  lstint
===============================================================================
localhost.localdomain  40435 127.0.0.1            923 7 2      0      6       0
192.168.8.60             123 192.168.8.1           22 3 4    180    290      32
192.168.8.47             123 192.168.8.1          146 3 4    180     96     111
192.168.1.10             123 192.168.1.1           44 3 4    180    230     170
orion.asahi-net.or.jp    123 118.243.85.16         62 4 4    5d0    317     284
ntp1.jst.mfeed.ad.jp     123 118.243.85.16         65 4 4    5d0    225     293
ntp2.jst.mfeed.ad.jp     123 118.243.85.16         64 4 4    5d0    227     299
udon.nict.go.jp          123 118.243.85.16         65 4 4    5d0    225     371
ntp3.jst.mfeed.ad.jp     123 118.243.85.16         64 4 4    5d0    225     380

その他

  • 最近の携帯電話に搭載されている自動時刻合わせ機能は、基地局からの制御情報に含まれる時刻情報を利用している。したがって、i-mode などを用いた NTP でもなければ、標準電波を利用した、いわゆる電波時計でもない。電波っちゃ電波だけど。