LoginSignup
30
26

More than 5 years have passed since last update.

Linuxサーバー時刻を(現在時刻と)合わせる

Last updated at Posted at 2018-09-19

初期設定だとLinuxサーバーの時刻設定がめちゃくちゃになっているので
正しい時間帯で使用する場合、自分で設定しないといけない

※ 詳しいことはこちらの記事で
Linux/UNIXサーバは立てたらまず時刻同期の設定

現在の設定時刻を確認

システムクロック
date
ハードウェアクロック
hwclock -r

設定作業

ntpサーバのインストール

RHEL(CentOSとか)系
yum install ntp
Debian系
apt-get install ntp

自動起動設定

# centos6 & etc
chkconfig ntpd on

# centos7
systemctl enable ntpd

ntpdの設定ファイルを編集

時刻の同期設定するサーバ(日本国内)を記述する

/etc/ntp.conf
# 以下の各行を編集する

#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 -4 ntp.nict.jp iburst
server -4 ntp1.jst.mfeed.ad.jp iburst
server -4 ntp2.jst.mfeed.ad.jp iburst
server -4 ntp3.jst.mfeed.ad.jp iburst

#  iburst は環境によっては記述しない

システムクロックの時刻を合わせる

ntpdate ntp.nict.jp

ntpdサービスを起動

# centos6 & etc
service ntpd start

# centos7
systemctl start ntpd.service

ハードウェアクロックの時刻を設定

システムクロックの時刻に合わせる

hwclock -w

設定された時刻を再確認

システムクロック確認
date
ハードウェアクロック確認
hwclock -r

同期状況の確認

ntpdの起動後に少し時間を置いてから以下のコマンドで同期状況を確認できる

ntpq -p
コマンド実行が早すぎた場合(まだ同期時刻取得中)
***Request timed out
時刻同期の実行結果
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*ntp-a2.nict.go. .NICT.           1 u   30   64  377    4.766   15.553   7.141
+ntp1.jst.mfeed. 133.243.236.17   2 u   36   64  377    3.187   16.979   6.375
-ntp2.jst.mfeed. 133.243.236.17   2 u   37   64  377    3.226   11.573   5.880
+ntp3.jst.mfeed. 133.243.236.17   2 u   33   64  377    3.408    3.691  11.861

各サーバ名の左側の記号について

記号 意味
同期中
同期完了(参照可能なサーバー)

ハードウェアクロックへ定期的に同期する設定

以下は任意で設定。

/etc/crontab
#毎朝5時にソフトウェアクロックをハードウェアクロックに反映
0 5 * * 0 root /sbin/hwclock -w > /dev/null 2>&1

参考

Linux/UNIXサーバは立てたらまず時刻同期の設定
ntpq -pの見方 – 猫型iPS細胞研究所

30
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
26