NTP サーバ構築
| Table of Contents |
インストール環境
- OS
- CentOS 5.5 (kernel 2.6.18-194.11.1.el5)
- パッケージ
- ntp-4.2.2p1-9.el5.centos.2.1
NTPのインストール
- ntpをインストールします
# yum install ntp
ハードウェアクロックの調整
- NTPが起動していれば停止します。
# /etc/init.d/ntpd stop
- NTPサーバを利用してシステムクロックを調整します。
# /usr/sbin/ntpdate ntp.nict.jp
- ハードウェアクロック(マザーボード上の時計)を調整します。
# hwclock --systohc
NTPの設定
ntp.confの編集
- ntpの設定ファイル「/etc/ntp.conf」を編集します。
# vi /etc/ntp.conf #NTPサーバとローカルマシンの時刻のずれを管理するファイル driftfile /var/lib/ntp/drift #全てのNTP通信を拒否 restrict default kod nomodify notrap nopeer noquery restrict -6 default kod nomodify notrap nopeer noquery #ローカルホストからのNTP通信を許可する restrict 127.0.0.1 restrict -6 ::1 # LANのNTP通信を許可する restrict 192.168.11.0 mask 255.255.255.0 nomodify notrap # 上位NTPサーバとの通信を許可する 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 #NTPサーバの指定(stratum:優先順位はサーバで決まっています) server 133.243.238.163 #ntp.nict.jp (stratum1) server 133.243.238.164 #ntp.nict.jp (stratum1) #NTPサーバとの同期に失敗した場合はローカルクロックと同期させる server 127.127.1.0 # local clock #ローカルクロックの優先度は10とする fudge 127.127.1.0 stratum 10 #NTP認証で利用するキー(今回は利用しない) keys /etc/ntp/keys #ログファイルの指定 logfile /var/log/ntp.log
設定ファイルの主要項目については以下の通り設定項目 説明 restrict アクセス制御を行います。 server 上位のNTPサーバを指定します。 fudge 優先順位の指定を行います。 driftfile NTPサーバとローカルマシンの時刻のずれを管理するファイルを指定します。 keys NTP認証で利用するKeyファイルを指定します。 logfile ログファイルを指定します。
自動起動の設定
- chkconfigコマンドで自動起動をONにします
# chkconfig --level 345 ntpd on
起動
- 時刻のずれが大きいとntpでの時刻同期に失敗するため、ntpdateコマンドで時刻を合わせます。
# ntpdate ntp.nict.jp 25 May 02:36:23 ntpdate[1520]: adjust time server 133.243.238.243 offset -0.124722 sec
- ntpを起動します。
# /etc/init.d/ntpd start
動作確認
- ntpqコマンドで同期状態を確認します。
起動直後は時刻同期が出来てません。(時刻同期が完了するまで5分ぐらいかかります)# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== ntp-b2.nict.go. .NICT. 1 u 2 64 1 12.238 -0.346 0.001 ntp-b3.nict.go. .NICT. 1 u 1 64 1 10.813 0.710 0.001 LOCAL(0) .LOCL. 10 l - 64 1 0.000 0.000 0.001
- 5分後ぐらいに再び同期状態を確認します。左端に「*」が付けば同期が出来ています。
# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== +ntp-b2.nict.go. .NICT. 1 u 30 64 377 10.542 0.835 0.810 *ntp-b3.nict.go. .NICT. 1 u 29 64 377 10.367 -0.020 0.469 LOCAL(0) .LOCL. 10 l 37 64 377 0.000 0.000 0.001 - ntpqコマンドでの表示内容は以下の通りです。
設定項目 説明 * 現在同期中のサーバ + 接続可能で時刻同期可能なサーバ remote NTPサーバ名 refid NTPサーバの識別子 st 優先順位 t 通信手段(local,unicast,munticast,broardcast) when 最後にパケットを受信した時間(秒) poll ポーリング間隔(秒) reach 到達可能性を表すレジスタデータ delay 遅延(ミリ秒) offset システムクロックとの差(ミリ秒) jitter 時刻のばらつき(ミリ秒)。値が少ないほどよい
FAQ
「set_rtc_mmss: can't update from * to **」のようなエラーが表示される
A システムクロックとハードウェアクロックのずれが大きいと表示されるため、 ハードウェアクロックの時間を合わせて下さい。