サーバー構築

サーバーの時計を自動で合せるNTPクライアントを導入

サーバーの時計を自動で合せるNTPクライアントを導入
目次
  1. NTP(時刻合わせ for client)をインストール
  2. ntpdateを編集
  3. 手動で時刻合わせる
  4. Cronに登録(自動実行)
  5. CMOS情報の時間をOSの時間と合わせる
  6. 時間を確認する

サーバーの時計が狂わないように自動で同期を取るように設定します。サーバーの時計が狂えばすべてのプログラム内で使われる日時が狂ってしまいます。その前に時刻合わせはやっておきたいですね。

2026年11月時点の補足:今の Debian では ntpdate を入れなくても時刻が合います

Debian 12 のリリースノートによると、Debian 12 から時刻合わせの標準は systemd-timesyncd になり、ntp パッケージは ntpsec に置き換わりました。Debian 13 には ntpdate というパッケージが無く(代わりは ntpsec-ntpdate)、記事の apt-get install ntpdate は失敗します(Debian のパッケージ一覧(madison)・2026年11月5日に確認)。

# timedatectl                        ← 「System clock synchronized: yes」なら合っている
# vi /etc/systemd/timesyncd.conf
[Time]
NTP=ntp.jst.mfeed.ad.jp
# systemctl restart systemd-timesyncd
# timedatectl timesync-status        ← 同期先のサーバーとずれを表示
項目この記事(2013年)Debian 13(2026年11月時点)
時刻合わせの仕組みntpdate を cron で毎日実行systemd-timesyncd(最初から動いている)
同期先の指定/etc/default/ntpdate の NTPSERVERS/etc/systemd/timesyncd.conf の NTP=
手動で合わせるntpdate -u ntp.jst.mfeed.ad.jpntpsec-ntpdate を入れて ntpdate を使う(普段は不要)
NTP サーバーを自分で立てるntpchrony 4.6.1 か ntpsec 1.2.3

記事の cron での毎日の実行は、timesyncd が動いていれば要りません。ずれを常に小さく保つ仕組みなので、1日1回まとめて合わせる方法より時刻が安定します。

NTP(時刻合わせ for client)をインストール

// インストール
# apt-get install ntpdate

ntpdateを編集

# vi /etc/default/ntpdate を修正します。

# The settings in this file are used by the program ntpdate-debian, but not
# by the upstream program ntpdate.

# Set to "yes" to take the server list from /etc/ntp.conf, from package ntp,
# so you only have to keep it in one place.
-NTPDATE_USE_NTP_CONF=yes
+NTPDATE_USE_NTP_CONF=no ← 修正:/etc/ntp.conf を利用しない。

# List of NTP servers to use  (Separate multiple servers with spaces.)
# Not used if NTPDATE_USE_NTP_CONF is yes.
-NTPSERVERS="0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org"
+NTPSERVERS="ntp.jst.mfeed.ad.jp" ← 修正:時計を合わせるNTPサーバーを変更。

# Additional options to pass to ntpdate
-NTPOPTIONS=""
+NTPOPTIONS="-u" ← 修正:-u オプションは、Firewall 内にあるマシンに対して有効なオプションです。ポート123が利用できない場合。

手動で時刻合わせる

# ntpdate -u ntp.jst.mfeed.ad.jp

Cronに登録(自動実行)

// 新規作成
# vi /etc/cron.daily/ntpdate

// 内容(コピペ)
#!/bin/sh
ntpdate-debian

// アクセス権変更
# chmod 755 /etc/cron.daily/ntpdate

CMOS情報の時間をOSの時間と合わせる

# /sbin/hwclock -w

時間を確認する

# date
Sun May 18 21:27:00 JST 2013