目次 >> NTP
ここでは、NTP(Network Time Protocol)サーバおよび、ntpdateを使った、時計の合わせ方について解説する。
また、NTPを使わない手動での合わせ方についても解説する。
もし、そのマシンでLinuxしか動かさないのであれば、UTCに設定すればよい。このように設定すると、例えばサマータイムのようにローカルの時刻が変わっても、パソコンの内蔵時計自体は変化せず、LinuxがUTCで設定された時刻から、場所や季節に合わせて、表示する時刻をソフトウェア的に変化させる。
一方もし、Windowsと共存させるのであれば、Windowsはハードウェアクロックに直接ローカルタイムを書き込むので、UTCではなく、Windowsと同じ方法を使って時刻を管理する。そうしないと、Windows、Linuxで立ち上げるごとに時計あわせをする必要が出てしまう。
/etc/default/rcS
内の、
UTC=yes
でUTCを
UTC=no
でローカルタイムを使うようになる。
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
と打って時刻をハードウェアに書き込む。なお、これを行わなくてもハードウェアへの書き込みはシステムのシャットダウン時に自動的に行われる。
WindowsはNTPサーバに定期的に問い合わせて、時刻を補正している。初期値では7日ごとに時計合わせが行われている。この間隔を変えるには、レジストリで、
HKEY_LOCAL_MACHINE¥SYSTEM¥CurrentControlSet¥Services¥W32Time¥TimeProviders¥NtpClient
内の、
の値を変更する。単位は秒。
初期値は604800(十進値)である。
例えば1時間ごとに時計合わせをするならこの値を、3600に変更する。
NTPサーバを立てると、NTPサーバはほかのサーバを参照して時刻を正確に保ちつつ、ほかのパソコンに対して、時刻の同期を取るための、NTPサービスを提供することが出来るようになる。
ここでは、上位サーバに時刻を問い合わせて、時刻を同期させかつ、LAN内のパソコンに対して、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 extra packages will be installed: libopts25 Suggested packages: ntp-doc The following NEW packages will be installed: libopts25 ntp 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded. Need to get 646 kB of archives. After this operation, 1,637 kB of additional disk space will be used. Do you want to continue [Y/n]? y Get:1 http://us.archive.ubuntu.com/ubuntu/ precise/main libopts25 amd64 1:5.12-0.1ubuntu1 [59.9 kB] Get:2 http://us.archive.ubuntu.com/ubuntu/ precise/main ntp amd64 1:4.2.6.p3+dfsg-1ubuntu3 [586 kB] Fetched 646 kB in 2s (303 kB/s) Selecting previously unselected package libopts25. (Reading database ... 177224 files and directories currently installed.) Unpacking libopts25 (from .../libopts25_1%3a5.12-0.1ubuntu1_amd64.deb) ... Selecting previously unselected package ntp. Unpacking ntp (from .../ntp_1%3a4.2.6.p3+dfsg-1ubuntu3_amd64.deb) ... Processing triggers for ureadahead ... Processing triggers for man-db ... Setting up libopts25 (1:5.12-0.1ubuntu1) ... Setting up ntp (1:4.2.6.p3+dfsg-1ubuntu3) ... * Starting NTP server ntpd ...done. Processing triggers for libc-bin ... ldconfig deferred processing now taking place
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サーバを持っている場合は、そちらを利用するようにする。
CentOS7では
# systemctl restart ntpd
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
で再起動する。
ntpdが正常に動いているかは、ntpq -pと打つとわかる。
$ ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== ntp2.Housing.Be 128.32.206.54 2 u 11 64 3 5.675 -2.212 0.427 68.68.18.78.cus 192.43.244.18 2 u 13 64 3 65.347 -2.711 0.436 dione.cbane.org 69.25.96.13 2 u 13 64 3 5.790 -1.819 0.501 199.249.223.123 129.7.1.66 2 u 13 64 3 67.372 -16.592 0.718 europium.canoni 193.79.237.14 2 u 45 64 3 142.370 -2.722 0.534
パソコン起動時に自動的に起動するようにするには
CentOS7ではsystemctl enable ntpdで設定する
# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
確認はsystemctl is-enabled ntpdでできる。
# systemctl is-enabled ntpd
enabled
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.nict.jp ntp.jst.mfeed.ad.jp ntp.ring.gr.jp
アメリカ東海岸ボストン付近の場合は、
tick.mit.edu
アメリカ西海岸ロサンゼルス付近の場合は
time.ucla.edu
もしタイムゾーンがあっていないようであれば、/etc/localtime
を書き換えて、設定する。もととなるファイルが、/usr/share/zoneinfo/
にあるので、そこからコピーすればよい。
例えば、日本標準時(JST)に設定するには、
# cp -p /usr/share/zoneinfo/Japan /etc/localtime
とする。
アメリカなど夏時間のある地域の場合は、少し気をつける必要がある。
# cp -p /usr/share/zoneinfo/EST /etc/localtime
では東部時間だが、夏時間が反映されないので、下記のように設定する。
# cp -p /usr/share/zoneinfo/EST5EDT /etc/localtime
/etc/localtime
を書き換えてシステム全体のタイムゾーンを設定するが、各国にユーザーがいる場合などで、ユーザーごとに設定したい場合もある。
その場合は、各ユーザーの.bashrcなどで下記のように設定する。この例では、アメリカ太平洋時間に設定。
export TZ="/usr/share/zoneinfo/US/Pacific"
ネットワークにつながっていないなど、何らかの理由でNTPサーバに接続できない場合は、dateコマンドで日付を手動で設定する。
時計を合わせたら、最後に
# clock -w
と打って時刻をハードウェアに書き込む。
最終更新日