計算機の時刻同期 (時刻合わせ) の設定 on Debian 10

Debian 10 (Buster)での計算機の時刻同期の設定について書き残す
(NTPを利用した時刻同期)

当方はntpdを用いた設定は行わない.理由は明快.無駄なサービス/プログラムを常時起動しておきたくないからだ.なので ntpdate コマンドを用い,cron で定期実行することで計算機の時刻同期(時刻合わせ)を実現する.設定はいたって簡単.

  1. ntpdate のインストール
    1
    % sudo apt-get install ntpdate
  1. 設定ファイルの編集
    /etc/default/ntpdate ファイルを編集する
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    % cd /etc/default
    % sudo vi 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

    # 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="ntp3.jst.mfeed.ad.jp ntp.nict.jp time.cloudflare.com"

    # Additional options to pass to ntpdate
    NTPOPTIONS="-sv"
    変更点は3点.
  2. NTPDATE_USE_NTP_CONFはnoにする.
  3. NTPSERVERSは,利用するNTPサーバを指定する.
    参考Webページの[1]を参考に,設定した.
  4. NTPOPTIONSは,コマンドオプション.
    -v はverboseモード,-s は標準出力をsyslogに出力するためのオプションである
  1. 定期実行の設定
    cronを利用して,定期的にntpサーバとの時刻同期を行うよう設定する
1
2
3
4
5
6
7
8
9
10
% su -
# crontab -e
(以降は編集処理)
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 */8 * * * /usr/sbin/ntpdate-debian > /dev/null 2>&1

Debianの場合は,ntpdate-debian というWrapperコマンドがあり(中身はshell script),それを定期的に実行すれば,先ほどの設定ファイルを反映しつつ ntpdateコマンドを実行してくれる.あとは,syslogで実行されていることを確認すれば良い.上のcron設定を見ればわかるが,当方は日に3回同期をするようにしているが,これは管理者のポリシー次第.ただ,あまり頻繁に同期を行うとNTPサーバの負担になるので,そこは配慮するべき.

追記: Ubuntu 18.04 の場合

Ubuntu 18.04は,ntpdateが非推奨になっていて,かわりに systemd-timesyncd という仕組みで行なうらしい.

1
2
3
4
5
6
7
8
9
10
11
12
13
% systemctl -l status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization
Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendo
Active: active (running) since Mon 2020-03-30 13:11:19 JST; 3min 47s ago
Docs: man:systemd-timesyncd.service(8)
Main PID: 26928 (systemd-timesyn)
Status: "Synchronized to time server 133.243.238.243:123 (ntp.nict.jp)."
Tasks: 2 (limit: 4539)
CGroup: /system.slice/systemd-timesyncd.service
└─26928 /lib/systemd/systemd-timesyncd

Mar 30 13:11:19 firenze systemd[1]: Starting Network Time Synchronization...
Mar 30 13:11:19 firenze systemd[1]: Started Network Time Synchronization.

NTPサーバの指定を変更する場合は,/etc/systemd/timesyncd.conf ファイルを書き換える.当方は以下のように設定し直した(16, 17行目)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
% cat timesyncd.conf 
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.

[Time]
NTP=ntp.nict.jp
FallbackNTP=ntp3.jst.mfeed.ad.jp time.cloudflare.com ntp.ubuntu.com
#NTP=
#FallbackNTP=ntp.ubuntu.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

設定を変更したら再起動!

1
% sudo systemctl restart systemd-timesyncd

参考Webページ

  1. 【負荷OK】Public NTPサーバの一覧
    https://qiita.com/halpas/items/5a451ead0b13f6792595
  2. 【Ubuntu 18.04 LTS Server】時刻の同期について
    https://www.yokoweb.net/2018/05/14/ubuntu-18_04-timesyncd/

— ends here

計算機の時刻同期 (時刻合わせ) の設定 on Debian 10

http://the.netaro.info/2020/03/28/2020-0328_ntpdate-debian/

Author

T.T

Posted on

2020-03-28

Updated on

2020-12-12

Licensed under