Google Bookmarksに追加 Twitterに投稿 はてなブックマークに追加 Yahoo!ブックマークに追加

目次 >> NTP

時計の合わせ方

ここでは、NTP(Network Time Protocol)サーバおよび、ntpdateを使った、時計の合わせ方について解説する。
また、NTPを使わない手動での合わせ方についても解説する。

パソコンの内蔵時計について

もし、そのマシンでLinuxしか動かさないのであれば、UTCに設定すればよい。このように設定すると、例えばサマータイムのようにローカルの時刻が変わっても、パソコンの内蔵時計自体は変化せず、LinuxがUTCで設定された時刻から、場所や季節に合わせて、表示する時刻をソフトウェア的に変化させる。

一方もし、Windowsと共存させるのであれば、Windowsはハードウェアクロックに直接ローカルタイムを書き込むので、UTCではなく、Windowsと同じ方法を使って時刻を管理する。そうしないと、Windows、Linuxで立ち上げるごとに時計あわせをする必要が出てしまう。

ubuntuの場合

/etc/default/rcS

内の、

UTC=yes

でUTCを

UTC=no

でローカルタイムを使うようになる。

ntpdateを使ってあわせる

ntpdateを使っての時計の合わせ方の基本は、rootで、

# ntpdate NTPサーバ

である。例えば、NTPサーバにntp.nict.jpを指定する場合は、

# ntpdate ntp.nict.jp

となる。

もしこれで

# ntpdate ntp.nict.jp
21 Mar 21:52:40 ntpdate[2800]: the NTP socket is in use, exiting

このようなエラーがでるようであれば、NTPサーバがそのパソコンで作動している。NTPサーバを切るか、ntpdateではなく、NTPサーバで時計あわせをするようにする。(もしくは手動で設定するという手もある。)

ntpdateのその他のオプションとしては、-s、-qなどがある。

# ntpdate -s ntp.nict.jp

-sはログに結果を出力する。

時計を合わせたら、最後に

# clock -w

と打って時刻をハードウェアに書き込む。なお、これを行わなくてもハードウェアへの書き込みはシステムのシャットダウン時に自動的に行われる。

NTPサーバを立てて設定する

NTPサーバを立てると、NTPサーバはほかのサーバを参照して時刻を正確に保ちつつ、ほかのパソコンに対して、時刻の同期を取るための、NTPサービスを提供することが出来るようになる。

ここでは、上位サーバに時刻を問い合わせて、時刻を同期させかつ、LAN内のパソコンに対して、NTPサービスを提供する方法について記述する。

NTPサーバのインストール

Fedoraの場合はyumでSuseの場合はyastで、ubuntuの場合はapt-get install ntpでインストール。

$ sudo apt-get install ntp
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-2.6.32-21-generic linux-headers-2.6.32-21
Use 'apt-get autoremove' to remove them.
Suggested packages:
ntp-doc
The following NEW packages will be installed:
ntp
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 559kB of archives.
After this operation, 1,450kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu/ lucid/main ntp 1:4.2.4p8+dfsg-1ubuntu2 [559kB]
Fetched 559kB in 5s (111kB/s)
Selecting previously deselected package ntp.
(Reading database ... 148652 files and directories currently installed.)
Unpacking ntp (from .../ntp_1%3a4.2.4p8+dfsg-1ubuntu2_amd64.deb) ...
Processing triggers for man-db ...
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot
Setting up ntp (1:4.2.4p8+dfsg-1ubuntu2) ...
* Starting NTP server ntpd
...done.

NTPサーバの設定

NTPサーバを動かす場合は、/etc/ntp.confを書き換えて設定する。
基本的にはserverの項目を自分にあった上位サーバに書き換えればよい。
FC5のデフォルトでは下記のようになっている。(FC4でもほぼ同様)

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.

restrict default nomodify notrap 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 


# -- CLIENT NETWORK -------
# Permit systems on this network to synchronize with this
# time service.  Do not permit those systems to modify the
# configuration of this service.  Also, do not use those
# systems as peers for synchronization.
# restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap


# --- OUR TIMESERVERS ----- 
server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org


# --- NTP MULTICASTCLIENT ---
#multicastclient                        # listen on default 224.0.1.1
# restrict 224.0.1.1 mask 255.255.255.255 nomodify notrap
# restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap



# --- GENERAL CONFIGURATION ---
#
# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available. The
# default stratum is usually 3, but in this case we elect to use stratum
# 0. Since the server line does not have the prefer keyword, this driver
# is never used for synchronization, unless no other other
# synchronization source is available. In case the local host is
# controlled by some external source, such as an external oscillator or
# another protocol, the prefer keyword would cause the local host to
# disregard all other synchronization sources, unless the kernel
# modifications are in use and declare an unsynchronized condition.
#
server 127.127.1.0
fudge   127.127.1.0 stratum 10  

#
# Drift file.  Put this in a directory which the daemon can write to.
# No symbolic links allowed, either, since the daemon updates the file
# by creating a temporary in the same directory and then rename()'ing
# it to the file.
#
driftfile /var/lib/ntp/drift
broadcastdelay  0.008

#
# Keys file.  If you want to diddle your server at run time, make a
# keys file (mode 600 for sure) and define the key number to be
# used for making requests.
#
# PLEASE DO NOT USE THE DEFAULT VALUES HERE. Pick your own, or remote
# systems might be able to reset your clock at will. Note also that
# ntpd is started with a -A flag, disabling authentication, that
# will have to be removed as well.
#
keys            /etc/ntp/keys
restrict 0.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
restrict 1.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
restrict 2.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery

まず、restrictであるが、これで、どのクライアントからの問い合わせに応答するかを決定する。複数ある場合は、後の方の行が優先される。上記の設定では、まずデフォルト値として非常に制限した設定をし(上位のサーバに問い合わせて、自分自身の時計を合わせることを許可している)、次に、自分自身(127.0.0.1)からの問い合わせにはすべて応答するようにしている。
また、外部からの問い合わせに対しては特に制限を行っていないので、どのパソコンからでも、NTPサービスを利用できる。
通常は、特にいじる必要はない。LAN外からの問い合わせを不可能にするには、ファイアウォールなどで、ポートをふさいでおけばよい。

もし、よりセキュアな設定を行いたい場合は、下記のようにするとよい。1行目はまずデフォルトですべての問い合わせに応じないようにしている。3行目はLAN内からの問い合わせには応答するようにしている。4行目で、上位のNTPサーバとの通信を許可している。

restrict default ignore
restrict 127.0.0.1
restrict 192.168.0.0 mask 255.255.255.0 noquery nomodify nopeer notrust notrap
restrict ntp.nict.jp noquery nomodify

次にserverであるが、ここに上位サーバを書き込む。通常は、この設定を書き換えるのみでよい。日本国内では、下記の設定を推奨する。

server ntp.nict.jp

もしくは、

server ntp1.jst.mfeed.ad.jp
server ntp2.jst.mfeed.ad.jp
server ntp3.jst.mfeed.ad.jp

もちろん、自分のプロバイダーが独自にNTPサーバを持っている場合や、大学内で大学がNTPサーバを持っている場合は、そちらを利用するようにする。

起動、停止、再起動

FC5の場合は、

# /etc/rc.d/ntpd restart

Fedora10の場合は

# /etc/init.d/ntpd restart

で再起動する

OpenSUSE 10.0の場合

# /etc/rc.d/ntp restart

で再起動する

ubuntuの場合、

$ sudo /etc/init.d/ntp restart

で再起動する。

パソコン起動時に自動的に起動する

パソコン起動時に自動的に起動するようにするには

FC4、FC5では、

# chkconfig ntpd on

OpenSUSE 10.0、10.1、10.2、10.3では、

# chkconfig ntp on

とする。

ubuntuでは、sysv-rc-confを使って対話的に設定する。

なお、

/etc/ntp/step-tickers

にNTPサーバを記述しておくと、NTPサーバ起動前にntpdateを記述されたサーバを使用して実行してくれるので、同期がより早くできるようになる。/etc/ntp/step-tickersファイルはテキストファイルで下記のように記述すればよい。

ntp.nict.jp

主な公開NTPサーバ

日本の場合は下記がお薦め

ntp.nict.jp
ntp.jst.mfeed.ad.jp
ntp.ring.gr.jp

アメリカ東海岸ボストン付近の場合は、

tick.mit.edu

アメリカ西海岸ロサンゼルス付近の場合は

time.ucla.edu

タイムゾーンの設定

もしタイムゾーンがあっていないようであれば、/etc/localtimeを書き換えて、設定する。もととなるファイルが、/usr/share/zoneinfo/にあるので、そこからコピーすればよい。

アメリカなど夏時間のある地域の場合は、少し気をつける必要がある。

# cp -p /usr/share/zoneinfo/EST /etc/localtime

では東部時間だが、夏時間が反映されないので、下記のように設定する。

# cp -p /usr/share/zoneinfo/EST5EDT /etc/localtime

もし、NTPサーバが使えない場合は?

ネットワークにつながっていないなど、何らかの理由でNTPサーバに接続できない場合は、dateコマンドで日付を手動で設定する。

時計を合わせたら、最後に

# clock -w

と打って時刻をハードウェアに書き込む。

リンク


Google Bookmarksに追加 Twitterに投稿 はてなブックマークに追加 Yahoo!ブックマークに追加
本文中のFC4はFedora ProjectのFedora Core 4を、FC5はFedora Core 5を、FC6はFedora Core 6をopenSUSEはNovellのSUSE Linux OSSを表します。Fedora7以降は、単にFedora7、Fedora8、Fedora9、Fedora10、Fedora11と表示しています。Ubuntuは、必要に応じて9.04、10.04のようにバージョン番号をつけて区別しています。MandrivaはMandriva Linuxを表します。

ここに登場するドメイン名やIPアドレスなどはフィクションです。実在の人物・団体等とは一切関係がありません。
実際に使用する際は、各自の環境に合わせて書き換えてください。
もし何か間違いなどありましたらこちらからご連絡ください
リンクに許可は不要です。
Copyright (C) 2010 Chikuma Engineering Co., Ltd. All Rights Reserved.