wiki:TipAndDoc/network/ntp

Version 13 (modified by mitty, 13 years ago) (diff)

--

ntpdate

ntpd

/etc/ntp.conf

  • SakuraのVPSで運用中
    # /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
    
    driftfile /var/lib/ntp/ntp.drift
    
    
    # Enable this if you want statistics to be logged.
    #statsdir /var/log/ntpstats/
    
    statistics loopstats peerstats clockstats
    filegen loopstats file loopstats type day enable
    filegen peerstats file peerstats type day enable
    filegen clockstats file clockstats type day enable
    
    
    # You do need to talk to an NTP server or two (or three).
    #server ntp.ubuntu.com
    server -4 ntp1.sakura.ad.jp        # Sakura => 210.188.224.14
    server ntp1.sakura.ad.jp           # Sakura => 2001:e40:100:208::123
    server -4 ntp.nict.jp              # NICT => 133.243.238.163
    server -4 ntp.nict.jp              # NICT => 133.243.238.164
    server -4 ntp.nict.jp              # NICT => 133.243.238.243
    server -4 ntp.nict.jp              # NICT => 133.243.238.244
    server ntp.nict.jp                 # NICT => 2001:2f8:29:100::fff3
    server ntp.nict.jp                 # NICT => 2001:2f8:29:100::fff4
    server ntp.jst.mfeed.ad.jp         # MFEED => 210.173.160.
    server ntp.netlab.is.tsukuba.ac.jp # => 130.158.83.97
    
    server 127.127.1.0                 # local system clock (not IP address)
    
    # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
    # details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
    # might also be helpful.
    #
    # Note that "restrict" applies to both servers and clients, so a configuration
    # that might be intended to block requests from certain clients could also end
    # up blocking replies from your own upstream servers.
    
    # By default, exchange time with everybody, but don't allow configuration.
    #restrict -4 default kod notrap nomodify nopeer noquery
    #restrict -6 default kod notrap nomodify nopeer noquery
    restrict -4 default ignore
    restrict -6 default ignore
    
    # Local users may interrogate the ntp server more closely.
    restrict 127.0.0.1
    restrict ::1
    
    # Clients from this (example!) subnet have unlimited access, but only if
    # cryptographically authenticated.
    #restrict 192.168.123.0 mask 255.255.255.0 notrust
    
    
    restrict 210.188.224.14  mask 255.255.255.255 nomodify notrap noquery
    restrict 133.243.238.163 mask 255.255.255.255 nomodify notrap noquery
    restrict 133.243.238.164 mask 255.255.255.255 nomodify notrap noquery
    restrict 133.243.238.243 mask 255.255.255.255 nomodify notrap noquery
    restrict 133.243.238.244 mask 255.255.255.255 nomodify notrap noquery
    restrict 210.173.160.27  mask 255.255.255.255 nomodify notrap noquery
    restrict 210.173.160.57  mask 255.255.255.255 nomodify notrap noquery
    restrict 210.173.160.87  mask 255.255.255.255 nomodify notrap noquery
    restrict 130.158.83.97   mask 255.255.255.255 nomodify notrap noquery
    restrict 2001:e40:100:208::123 mask ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff nomodify notrap noquery
    restrict 2001:2f8:29:100::fff3 mask ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff nomodify notrap noquery
    restrict 2001:2f8:29:100::fff4 mask ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff nomodify notrap noquery
    
    
    # If you want to provide time to your local subnet, change the next line.
    # (Again, the address is an example only.)
    #broadcast 192.168.123.255
    
    # If you want to listen to time broadcasts on your local subnet, de-comment the
    # next lines.  Please do this only if you trust everybody on the network!
    #disable auth
    #broadcastclient
    
  • $ ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
    -ntp1.sakura.ad. .GPS.            1 u    5   64  377    0.695   -0.011   0.888
    +2001:e40:100:20 .GPS.            1 u   10   64  377    1.434   -1.154   0.829
    *ntp-a3.nict.go. .NICT.           1 u   13   64  377    1.411   -0.539   0.481
    +ntp-a2.nict.go. .NICT.           1 u   62   64  377    1.357   -1.173   0.486
    +ntp-b3.nict.go. .NICT.           1 u    1   64  377    3.049   -1.105   0.676
    +ntp-a3.nict.go. .NICT.           1 u   61   64  377    2.029   -1.144   0.675
    -ntp-a2.nict.go. .NICT.           1 u    2   64  377    2.069    0.197   0.737
    -ntp3.jst.mfeed. 210.173.160.86   2 u    -   64  377    1.292   -0.154   0.824
    -phoenix-gw.netl 210.173.160.87   3 u   61   64  377    8.147    1.432   0.591
     LOCAL(0)        .LOCL.           5 l   63   64  377    0.000    0.000   0.001
    

fource IPv4

  1. /etc/default/ntp (Ubuntu)
    NTPD_OPTS='-g -4'
    
    • ntpq -pでpeerに接続出来なくなる
    • $ ntpq -p
      localhost: timed out, nothing received
      ***Request timed out
      
    • ntpq -p4 とすることで回避出来る
           remote           refid      st t when poll reach   delay   offset  jitter
      ==============================================================================
       ntp-b3.nict.go. .NICT.           1 u   29   64    3   11.385   -0.560   0.497
       ntp-b2.nict.go. .NICT.           1 u   26   64    3    8.957   -0.115   0.567
       ntp-a3.nict.go. .NICT.           1 u   30   64    3    8.522   -0.643   0.521
       ntp-a2.nict.go. .NICT.           1 u   28   64    3    7.413   -0.117   0.603
       ntp2.jst.mfeed. 210.173.160.86   2 u   27   64    3   18.359    0.371   1.393
       phoenix-gw.netl 210.173.160.27   3 u   26   64    3   12.690    1.117   0.165
       LOCAL(0)        .LOCL.           5 l   24   64    3    0.000    0.000   0.001
      
  2. 「server -4 ntp.nict.jp」とする

refer local clock

server に 127.127.1.0 を指定すると、NTPサーバが時間を合わせる基準として 自分自身のローカルクロック(システム時刻)を使用します。 127.127.1.0は、時間を取得するデバイスを指定するためのNTP独自の表現で 127.127.1.0を指定するとシステム時刻から時間を取得するようになります。 IPアドレスと同じ形式のため紛らわしいですが、このままの数字を記入して下さい。

  • ntp.conf
    server 127.127.1.0
    
  • ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
     LOCAL(0)        .LOCL.           5 l    -   64    0    0.000    0.000   0.001
    
  • fudge文で、stratumを詐称(?)出来る模様。(CentOS:/usr/share/doc/ntp-*にドキュメントがある模様(未読))
    • ntp.conf
      server 127.127.1.0
      fudge   127.127.1.0 stratum 10
      
           remote           refid      st t when poll reach   delay   offset  jitter
       LOCAL(0)        .LOCL.          10 l   15   64    1    0.000    0.000   0.001
      

hardware clockのsyncについて

CentOS 5

起動時

  • /etc/sysconfig/ntpd
    # Set to 'yes' to sync hw clock after successful ntpdate
    SYNC_HWCLOCK=yes
    
  • /etc/ntp/step-tickers
    • このファイルにホストが羅列されている場合、ntpd起動前にntpdateでupdateする
  • /etc/init.d/ntpd
    • boot時に実行されるスクリプトでは、以下のような流れになっている
      echo -n $"$prog: Synchronizing with time server: "
      /usr/sbin/ntpdate $dropstr -s -b $NTPDATE_OPTIONS $tickers &>/dev/null
      
      [ "$SYNC_HWCLOCK" = "yes" ] && sync_hwclock
      
    • sync_hwclock() は以下のようになっている
      action $"Syncing hardware clock to system time" /sbin/hwclock $CLOCKFLAGS
      
    • $CLOCKFLAGSは「--systohc」の他、環境に合わせてUTCなどを指定する引数がセットされている。
ログ
  • 以下のようなログが画面に表示される
    ntpd: Synchronizing with time server:                      [  OK  ]
    Syncing hardware clock to system time                      [  OK  ]
    Starting ntpd:                                             [  OK  ]
    
  • attachment:CentOS5-boot.png

終了時

  • shutdown/rebootともに、以下のスクリプトで同期
    • /etc/init.d/halt
      [ -x /sbin/hwclock ] && action $"Syncing hardware clock to system time" /sbin/hwclock $CLOCKFLAGS
      
    • $CLOCKFLAGSは「/etc/init.d/ntpd」と同じく、「--systohc」の他、環境に合わせてUTCなどを指定する引数がセットされている。

Ubuntu

起動時

  • 以下の二つのスクリプトがまず呼ばれる
    • /etc/rcS.d/S08hwclockfirst.sh
    • /etc/rcS.d/S11hwclock.sh
    • 中身は以下を除いてほぼ同一
      FIRST=yes	# debian/rules sets this to 'yes' when creating hwclockfirst.sh
      
    • boot時は、以下のコマンドにより「H/W clockからSystem clockへ」時刻がコピーされる
      if /sbin/hwclock --hctosys $GMT $HWCLOCKPARS $BADYEAR $NOADJ; then
      
  • その後、「/etc/rcS.d/S40networking」ででeth0などが起動されるが、この際以下のスクリプトが実行される。
    • /etc/network/if-up.d/ntpdate
      invoke-rc.d --quiet ntp stop || true
      /usr/sbin/ntpdate-debian -s $OPTS 2>/dev/null
      invoke-rc.d --quiet ntp start || true
      
    • このため、rcSののちにランレベル毎のスクリプトである「/etc/rc3.d/S23ntp」とは別にntpdがstart stopされる。このスクリプトはntpdパッケージとは別になっている。
      $ dpkg -S /etc/network/if-up.d/ntpdate
      ntpdate: /etc/network/if-up.d/ntpdate
      
    • また、「/etc/rc3.d/S23ntp」ではntpdが起動される以上のことは行われない
ログ
  • 以下のようなログが画面に表示される
     * Setting the system clock
    
    ...
    
     * Configuring network interfaces...
    
     * Stopping NTP server ntpd
     * Starting NTP server ntpd
    
    ...
    
     * Starting NTP server ntpd
    

終了時

  • shutdown/reboot時は、/etc/rc0.d/K25hwclock.sh(あるいは/rc6.d/)によって、H/W clockがSystem clockによって上書きされる。
    if /sbin/hwclock --systohc $GMT $HWCLOCKPARS $BADYEAR; then
    

misc

  • restrict default ignore と設定すると、ntpdが別のntpサーバへ同期するクライアントとしての動作も当然阻害される。従って、配布をせず時刻合わせのクライアントとしてのみ使う場合でも、defaultはignoreしつつ以下のように特定のサーバとは同期は出来るようにしておくこと。
    server 192.168.100.254
    restrict 192.168.100.254 mask 255.255.255.255 nomodify notrap noquery
    
    • これを忘れると、いつまで経っても同期先サーバのstratumが16と表示されて同期されない。

Attachments (5)

Download all attachments as: .zip