
【後編】仮想マシンでサーバを簡単に建てる方法~CentOS7インストール&実装編~
目次
こんにちは、いっしーです。
CentOS6まで標準実装されていたNTPクライアント/サーバ「ntpd」ですが、CentOS7からは「ntpd」に代わり、「chronyd」が標準実装となりました。
今回は、「chronyd」による時刻同期の設定方法についてご紹介します。
まずは、知っておくべきこととして、ハードウェアクロックとシステムクロックについて簡単にまとめました。
NTP(Network Time Protocol)とは、ネットワークから正確な時間を取得・同期するための通信方法です。
通信による僅かな遅延もうまいこと補正してくれるすごいやつです(^o^)
そして、NTPを使いサーバの時計を正しい時刻に合わせてくれるのがNTPクライアント/サーバ「chronyd」です。
なぜNTPが必要なのかといいますと、サーバの時刻「ハードウェアウェアクロック」、「システムクロック」を手動で合わせたとしても、少しずつずれていきます。
もし、サーバの時間が正しくなかったら、プログラムの定期実行、各種ログ、メールなど…至るところで不具合が生じます。
そのため、NTPクライアント/サーバで、常に正確な時間を同期する必要があるということですね。
※イメージ図
CentOS7では、デフォルトでchronydがインストールされていますが、もしインストールされてない場合はインストールします。
chronydがインストールされているか確認してみましょう。
インストールされてない場合は、「Unit chronyd.service could not be found.」が返って来ます。
1 2 |
[root@j-cloud ~]# systemctl status chronyd.service Unit chronyd.service could not be found. |
▼chronydのインストール
1 |
[root@j-cloud ~]# yum -y install chrony |
設定ファイルは「/etc/chrony.conf」です。
そのままでも問題ないですが、今回は国内のNTPサーバを利用するように変更します。
海外NTPサーバのコメント化
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
国内NTPサーバを追記
server ntp1.jst.mfeed.ad.jp iburst
server ntp2.jst.mfeed.ad.jp iburst
server ntp3.jst.mfeed.ad.jp iburst
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
[root@j-cloud ~]# vi /etc/chrony.conf # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). #server 0.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst server ntp1.jst.mfeed.ad.jp iburst server ntp2.jst.mfeed.ad.jp iburst server ntp3.jst.mfeed.ad.jp iburst # Record the rate at which the system clock gains/losses time. driftfile /var/lib/chrony/drift # Allow the system clock to be stepped in the first three updates # if its offset is larger than 1 second. makestep 1.0 3 # Enable kernel synchronization of the real-time clock (RTC). rtcsync # Enable hardware timestamping on all interfaces that support it. #hwtimestamp * # Increase the minimum number of selectable sources required to adjust # the system clock. #minsources 2 # Allow NTP client access from local network. #allow 192.168.0.0/16 # Serve time even if not synchronized to a time source. #local stratum 10 # Specify file containing keys for NTP authentication. #keyfile /etc/chrony.keys # Specify directory for log files. logdir /var/log/chrony # Select which information is logged. #log measurements statistics tracking |
▼chronydの起動・自動起動有効化
1 2 |
[root@j-cloud ~]# systemctl start chronyd.service [root@j-cloud ~]# systemctl enable chronyd.service |
「chronyc sources」コマンドで時刻が同期されているかを確認します。
「M」カラムの「^」はサーバ、「S」カラムの「*」は現在同期中、「+」は同期可能の表示。
つまり、ntpサーバの左に「*」が表示されていれば、正常に同期されていることを意味します。
1 2 3 4 5 6 7 |
[root@j-cloud ~]# chronyc sources 210 Number of sources = 3 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* ntp1.jst.mfeed.ad.jp 2 6 377 62 -337us[ -158us] +/- 79ms ^+ ntp2.jst.mfeed.ad.jp 2 6 377 60 -649us[ -649us] +/- 92ms ^+ ntp3.jst.mfeed.ad.jp 2 6 377 61 +238us[ +238us] +/- 126ms |
オプション「-v」をつけることによってカラムの説明文が表示されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@j-cloud ~]# chronyc sources -v .-- Source mode '^' = server, '=' = peer, '#' = local clock. / .- Source state '*' = current synced, '+' = combined , '-' = not combined, | / '?' = unreachable, 'x' = time may be in error, '~' = time too variable. || .- xxxx [ yyyy ] +/- zzzz || Reachability register (octal) -. | xxxx = adjusted offset, || Log2(Polling interval) --. | | yyyy = measured offset, || \ | | zzzz = estimated error. || | | \ MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* ntp1.jst.mfeed.ad.jp 2 8 377 302 +666us[ +702us] +/- 88ms ^+ ntp2.jst.mfeed.ad.jp 2 8 377 167 +1295us[+1295us] +/- 72ms ^+ ntp3.jst.mfeed.ad.jp 2 8 377 425 +1640us[+1675us] +/- 122ms |
時刻同期の設定ができましたでしょうか?
それではまた次回!
▼ジョイントメディアのホスティングサービス『J CLOUD』のWEBサイトはこちら▼
安心・安全のクラウドサーバー
【J CLOUD】