Network Time Protocol daemon (日本語)

From ArchWiki
Jump to: navigation, search

この記事では NTPd (Network Time Protocol daemon) を設定・起動する方法を説明しています。GNU/Linux のソフトウェアクロックNetwork Time Protocol を使ってインターネット上の時刻サーバーと同期するのに最も一般的な方法です。正しく設定すれば、NTPd を使ってあなたのコンピュータを時刻サーバーとして動かすこともできます。

Contents

インストール

公式リポジトリにある ntpインストールしてください。

設定

Tip: ntp パッケージはデフォルトの /etc/ntp.conf と一緒にインストールされるので、カスタム設定を行わなくても NTPd は動作します。

NTP サーバーの接続の設定

まず /etc/ntp.conf に設定することは、マシンが同期するサーバの設定です。

NTP サーバは階層的なシステムによって strata と呼ばれるたくさんのレベルにクラス分けされています。その定義は、独立したタイムソースとされるデバイスは stratum 0 ソース、stratum 0 のデバイスに直接接続しているサーバーを stratum 1 ソース、stratum 1 ソースに接続しているサーバーを stratum 2 ソース、と続きます。

サーバーの stratum は正確性や信頼性を表しているわけではないことを理解する必要があります。一般的に、stratum 2 のサーバーが時刻同期に使われます: どのサーバーに接続するのかまだ決めてない場合、pool.ntp.org サーバー (alternate link) を使って一番地理的に近いサーバープールを選んで下さい。

サンプル::

/etc/ntp.conf
server 0.jp.pool.ntp.org iburst
server 1.jp.pool.ntp.org iburst
server 2.jp.pool.ntp.org iburst
server 3.jp.pool.ntp.org iburst

iburst オプションは推奨です。最初に試行した時に接続できなかったときだけパケットのバーストを送信するようになります。burst オプションは常時バーストを送信するので、許可を得ているときにだけ使って下さい。さもないとブラックリストに入れられてしまう可能性があります。

あなた自身の NTP サーバーの設定

NTP サーバーを設定する場合、サーバーとして local clock を追加する必要があります。これによりインターネットアクセスを失った時でも、ネットワークへの時刻の提供を続行することができます。(fudge コマンドを使って) stratum 10 サーバーとして local clock を追加すればインターネットアクセスが失われない限り使用されません:

server 127.127.1.0
fudge  127.127.1.0 stratum 10

次に、restrict コマンドを使ってあなたのサービスに接続できるクライアント (localhost もクライアントとして見なされます) を制限するルールを定義します。あなたのファイルには次のような行がすでに存在するはずです:

restrict default nomodify nopeer noquery

これで誰も何も変更できなくなり時刻サーバーの状態の問い合わせも制止されます: nomodify は ntpd (と ntpq または ntpdc) の再設定を無視し、noquery は ntpd (と ntpq または ntpdc) の状態データのダンプを防ぎます。

また、他のオプションを加えることも可能です:

restrict default kod nomodify notrap nopeer noquery
Note: This still allows other people to query your time server. You need to add noserve to stop serving time. It will also block time synchronization since it blocks all packets except ntpq and ntpdc queries.

"restrict" オプションの完全なドキュメントは man ntp_acc にあります。詳しい説明は https://support.ntp.org/bin/view/Support/AccessRestrictions を参照してください。

次の行のように、サーバーへの接続を許可する IP を ntpd に伝える必要があります; NTP サーバーを設定しない場合はこのままで問題ありません:

restrict 127.0.0.1

DNS 解決を IPv6 ネームスペースに強制したい時は、IP アドレスやホストネームの前に -6 を書いて下さい (-4 は IPv4 を強制します)。例:

restrict -6 default kod nomodify notrap nopeer noquery
restrict -6 ::1    # ::1 is the IPv6 equivalent for 127.0.0.1

最後に、ドリフトファイル (時計の時刻のズレを記録します) と任意でログファイルの場所を指定してください:

driftfile /var/lib/ntp/ntp.drift
logfile /var/log/ntp.log

基本的な設定ファイルは以下のようになります (整理のためにコメントは全て省略しています):

/etc/ntp.conf
server 0.pool.ntp.org iburst
server 1.pool.ntp.org iburst
server 2.pool.ntp.org iburst
server 3.pool.ntp.org iburst

restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

restrict 127.0.0.1
restrict -6 ::1  

driftfile /var/lib/ntp/ntp.drift
logfile /var/log/ntp.log
Note: Defining the log file is not mandatory, but it is always a good idea to have feedback for ntpd operations.

NTP の設定資料

結論を言うと、man ページを忘れないで下さい: man ntp.conf であなたの疑問の全ては解決するでしょう (関連する man ページも参照してください: man {ntpd|ntp_auth|ntp_mon|ntp_acc|ntp_clock|ntp_misc})。

デーモンを使わない

システム時刻を一度だけ同期するには、NTP デーモンを起動する代わりに、次を実行してください:

# ntpd -q
Note: 上のコマンドは現在は廃止されている ntpdate コマンドと同じ効果があります。

システム時刻を更新した後、再起動した時も時刻が反映されるようにハードウェアクロックに時刻を保存してください:

# hwclock -w

起動毎に同期する

Warning: サーバーや、数日間電源を落とさないようなマシンではこの方法は非推奨です。

oneshotsystemd ユニットを記述します:

/etc/systemd/system/ntp-once.service
[Unit]
Description=Network Time Service (once)
After=network.target nss-lookup.target 

[Service]
Type=oneshot
ExecStart=/usr/bin/ntpd -g -u ntp:ntp ; /usr/bin/hwclock -w

[Install]
WantedBy=multi-user.target
そしてユニットを有効にしてください:
# systemctl enable ntp-once

oneshot タイプの systemd ユニットは一度しか実行しません。従って ntpd -q オプションを使うのはこの場合ふさわしくありません。

デーモンとして実行する

ntpd を起動する:

# systemctl start ntpd

ntpd を起動時に有効にする:

# systemctl enable ntpd

もしくは次のコマンドを使って下さい:

# timedatectl set-ntp 1

デーモンが正しく同期を実行しているか確認する

ntpq を使えるようにするには pacman で libedit パッケージをインストールする必要があります。ntpq を使って設定されたピアの一覧を見て下さい:

$ ntpq -np

delay, offset, jitter のカラムがゼロではないはずです。ntpd が同期しているサーバーにはアスタリスクが前に付けられます。ntpd がサーバーを選んでいない時は同期に数分かかるかもしれません。17分 (1024秒) 後に確認してください。

Netctl

Netctl によるネットワーク接続にあわせてシステム時刻を同期するには、次の行をあなたの netctl プロファイルに追加してください:

ExecUpPost='/usr/bin/ntpd -q || true'

NetworkManager

Note: ntpd should still be running when the network is down if the hwclock daemon is disabled, so you should not use this.

NetworkManager の dispatcher スクリプトを使ってネットワーク接続にあわせて ntpd を立ち上げたり終了することができます。公式リポジトリ から networkmanager-dispatcher-ntpd をインストールしてください。

chroot で実行する

Note: ntpd should be run as non-root before attempting to jail it in a chroot (default in the vanilla Arch Linux package), since chroots are relatively useless at securing processes running as root.

/etc/conf.d/ntpd.conf を編集して

NTPD_ARGS="-g -u ntp:ntp"

NTPD_ARGS="-g -i /var/lib/ntp -u ntp:ntp"

に変更してください。そして、/etc/ntp.conf を編集して driftfile のパスを実際の root ではなく chroot ディレクトリの相対パスに変えて下さい:

driftfile       /var/lib/ntp/ntp.drift

driftfile       /ntp.drift

に変えて下さい。適切な chroot 環境を作成してください。getaddrinfo() を動作させるために適切なディレクトリとファイルを (root で) 作成し:

# mkdir /var/lib/ntp/etc /var/lib/ntp/lib /var/lib/ntp/proc
# touch /var/lib/ntp/etc/resolv.conf /var/lib/ntp/etc/services

そして上記のファイルをバインドマウントしてください:

/etc/fstab
...
#ntpd chroot mounts
/etc/resolv.conf  /var/lib/ntp/etc/resolv.conf none bind 0 0
/etc/services	  /var/lib/ntp/etc/services none bind 0 0
/lib		  /var/lib/ntp/lib none bind 0 0
/proc		  /var/lib/ntp/proc none bind 0 0
# mount -a

最後に、デーモンを再起動してください:

# systemctl restart ntpd

It is relatively difficult to be sure that your driftfile configuration is actually working without waiting a while, as ntpd does not read or write it very often. If you get it wrong, it will log an error; if you get it right, it will update the timestamp. If you do not see any errors about it after a full day of running, and the timestamp is updated, you should be confident of success.

代替

Chrony は NTPd の代替です、ダイアルアップフレンドリで、常時オンラインではない環境用に設計されています。

関連項目

外部リンク