NTPサーバで時刻を合わせる
Tips
時刻補正には ntpdateコマンド が適さない場合がある
例えば、Dovecotというメールサーバーは6秒以上、時刻が巻き戻ると、サービスが落ちる可能性がある。
以下の公式Wikiサイトにntpdateを使うことは良いアイデアじゃないとの記載がある。
TimeMovedBackwards - Dovecot Wiki
ハードウェアクロック(リアルタイムクロック、BIOSクロック)
電源を切っていても動作し続ける。
精度は高くなく、適宜、修正する必要がある。
秒単位の粗い時刻情報しか保持しない。
システムクロック
起動時にハードウェアクロックを参照して取得した現在時刻を、Linuxカーネルが保持・管理する時刻。
ハードウェアクロックより、単位の細かい時刻情報(ミリ秒〜ナノ秒単位)を保持。
ハードウェアクロックを表示する
$ sudo hwclock --debug hwclock from util-linux 2.20.1 Using /dev interface to clock. Assuming hardware clock is kept in UTC time. Waiting for clock tick... ...got clock tick Time read from Hardware Clock: 2014/09/20 13:05:44 Hw clock time : 2014/09/20 13:05:44 = 1411218344 seconds since 1969 2014年09月20日 22時05分44秒 -0.313228 seconds $
このマイナス表記の時間は、「コマンド実行時の時刻と、画面に表示した際の時間差」
以下でも同じ結果となる
$ hwclock -r hwclock: Cannot access the Hardware Clock via any known method. hwclock: Use the --debug option to see the details of our search for an access method. $ sudo hwclock -r --debuhwclock from util-linux 2.20.1 Using /dev interface to clock. Assuming hardware clock is kept in UTC time. Waiting for clock tick... ...got clock tick Time read from Hardware Clock: 2014/09/20 23:46:37 Hw clock time : 2014/09/20 23:46:37 = 1411256797 seconds since 1969 2014年09月21日 08時46分37秒 -0.161288 seconds $ $ sudo hwclock --show --debug hwclock from util-linux 2.20.1 Using /dev interface to clock. Assuming hardware clock is kept in UTC time. Waiting for clock tick... ...got clock tick Time read from Hardware Clock: 2014/09/20 23:47:00 Hw clock time : 2014/09/20 23:47:00 = 1411256820 seconds since 1969 2014年09月21日 08時47分00秒 -0.500714 seconds $
ハードウェアクロックの時刻をシステムクロックに合わせる
$ sudo hwclock -w
システムクロックの時刻をハードウェアクロックに合わせる
$ sudo hwclock -s
dateコマンドの使い方
オプション無しだと、現在時刻が表示される
オプションをつけると、システムクロックの日付や時刻を設定できる
コマンド書式
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
月日・時分・西暦 の順で指定する点に注意。
時刻を公開NTPサーバと同期する方法①
ntpdateコマンドを使う
オプション無しで、NTPサーバを指定すると、瞬時に時刻同期する。
サーバー構築後にntpdateコマンドを使う場合は、「-B」オプションを使うか、ntpdというサービスを使い、徐々に時刻同期すると良い。
ntpdateコマンドで、公開NTPサーバと時刻同期する
$ sudo ntpdate jp.pool.ntp.org 21 Sep 11:09:08 ntpdate[3369]: adjust time server 157.7.154.134 offset 0.024619 sec $
pool.ntp.org というドメインは、NTPサーバーの仮想クラスター。
このような公開NTPサーバには、「DNSラウンドロビン」という負荷分散の仕組みが備わっていて、1台のNTPサーバーに負荷が集中しないようになっている。
※上記と同様のコマンドを実行しても、2回目は違うIPアドレスが表示された。
一般ユーザーが利用できる公開NTPサーバーについては、以下のサイトに情報が乗っている。
NTP/推奨公開サーバ - wiki@nothing
NTPサーバーを構築する
構築の流れ
・ntpパッケージの導入
・上位サーバーやサービス提供先などの設定
・ntpサービスの起動
・ファイアウォールでudp/123ポートの開放
$ apt-get install ntp
上記サーバは /etc/ntp.conf に記述する。
通常はデフォルトのままで良い。
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board # on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for # more information. server 0.ubuntu.pool.ntp.org server 1.ubuntu.pool.ntp.org server 2.ubuntu.pool.ntp.org server 3.ubuntu.pool.ntp.org
・NTPサーバを192.168.10.0/24のネットワーク内のサーバーやクライアントから利用可能にするには、ntp.confで以下のように設定する
restrict 192.168.10.0 mask 255.255.255.0 notrap
ntpdサービスの起動
$ service ntp start * Starting NTP server ntpd [ OK ] $
ntpサービスの自動起動
$ sudo update-rc.d ntp enable 2 3 4 5 update-rc.d: warning: start runlevel arguments (none) do not match ntp Default-Start values (2 3 4 5) update-rc.d: warning: stop runlevel arguments (none) do not match ntp Default-Stop values (1) Enabling system startup links for /etc/init.d/ntp ... Removing any system startup links for /etc/init.d/ntp ... /etc/rc1.d/K77ntp /etc/rc2.d/K77ntp /etc/rc3.d/K77ntp /etc/rc4.d/K77ntp /etc/rc5.d/K77ntp Adding system startup for /etc/init.d/ntp ... /etc/rc1.d/K77ntp -> ../init.d/ntp /etc/rc2.d/S23ntp -> ../init.d/ntp /etc/rc3.d/S23ntp -> ../init.d/ntp /etc/rc4.d/S23ntp -> ../init.d/ntp /etc/rc5.d/S23ntp -> ../init.d/ntp $ ls -l /etc/rc*.d/*ntp lrwxrwxrwx 1 root root 13 9月 21 12:10 /etc/rc1.d/K77ntp -> ../init.d/ntp lrwxrwxrwx 1 root root 13 9月 21 12:10 /etc/rc2.d/S23ntp -> ../init.d/ntp lrwxrwxrwx 1 root root 13 9月 21 12:10 /etc/rc3.d/S23ntp -> ../init.d/ntp lrwxrwxrwx 1 root root 13 9月 21 12:10 /etc/rc4.d/S23ntp -> ../init.d/ntp lrwxrwxrwx 1 root root 13 9月 21 12:10 /etc/rc5.d/S23ntp -> ../init.d/ntp $
ファイアウォール(firewalld)のインストール
ubuntuでは、デフォルトではファイアウォールは有効になっていない。
そこで、最近、iptablesサービスの代わりに、導入されているfirewalldをインストールする。(fedoraではfirewalldが標準)
$ sudo apt-get install firewalld
iptableからの違いとしては、firewalldでは、ポリシー設定が動的に反映されるようになったほか、ゾーンと呼ばれる概念が導入された。
firewall-cmdを使ったファイアウォールの設定
# デフォルトのゾーンを確認 $ firewall-cmd --get-default-zone public # publicゾーンで123/udpポートを許可 $ sudo firewall-cmd --add-port=123/udp --zone=public success $ # publicゾーンで常に123/udpポートを許可 $ sudo firewall-cmd --permanent --add-port=123/udp --zone=public success $