10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ubuntu 16.04にntpとntpdateを両方インストールするとOS起動時にntp.serviceが停止している

Posted at

現象

Ubuntu 16.04でntp.serviceはntpインストール時にenableになっているのに、OS起動時にntp.serviceが停止しているという現象が発生した。

$ ntpq -p
ntpq: read: Connection refused
$ systemctl status ntp.service
● ntp.service - LSB: Start NTP daemon
   Loaded: loaded (/etc/init.d/ntp; bad; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:systemd-sysv-generator(8)

Apr 24 22:04:05 ubuntu1604 systemd[1]: Stopped LSB: Start NTP daemon.

このメッセージだと、一旦正常に起動してから停止したようだ。

OS起動後に手動でsudo systemctl start ntp.serviceを実行すると問題なく起動する。

$ sudo systemctl start ntp.service
$ systemctl status ntp.service
● ntp.service - LSB: Start NTP daemon
   Loaded: loaded (/etc/init.d/ntp; bad; vendor preset: enabled)
   Active: active (running) since Sun 2016-04-24 22:08:20 JST; 18s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1421 ExecStart=/etc/init.d/ntp start (code=exited, status=0/SUCCESS)
    Tasks: 2 (limit: 512)
   Memory: 1.4M
      CPU: 40ms
   CGroup: /system.slice/ntp.service
           └─1433 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 111:117

Apr 24 22:08:23 ubuntu1604 ntpd[1433]: Soliciting pool server 157.7.152.213
Apr 24 22:08:23 ubuntu1604 ntpd[1433]: Soliciting pool server 104.41.167.60
Apr 24 22:08:24 ubuntu1604 ntpd[1433]: Soliciting pool server 106.185.48.114
Apr 24 22:08:24 ubuntu1604 ntpd[1433]: Soliciting pool server 106.187.50.84
Apr 24 22:08:24 ubuntu1604 ntpd[1433]: Soliciting pool server 157.7.203.102
Apr 24 22:08:24 ubuntu1604 ntpd[1433]: Soliciting pool server 116.58.172.182
Apr 24 22:08:25 ubuntu1604 ntpd[1433]: Soliciting pool server 157.7.154.29
Apr 24 22:08:25 ubuntu1604 ntpd[1433]: Soliciting pool server 157.7.64.173
Apr 24 22:08:25 ubuntu1604 ntpd[1433]: Soliciting pool server 36.55.235.15
Apr 24 22:08:25 ubuntu1604 ntpd[1433]: Soliciting pool server 91.189.89.199

誰が殺したntp.service。
この前CentOS 7でchronyとntpdateが両方インストールされているとofflineの参照先がOS起動時にonlineにならないという問題にぶつかったため、今回もntpdateのせいではないかと適当にアンインストールしてみるとやっぱり当たり。

ntpdateが犯人

何が悪いのか。
もう一度ntpdateパッケージをインストールし、とりあえずntpdateパッケージに含まれるファイルを確認。

$ dpkg -L ntpdate
/.
/etc
/etc/network
/etc/network/if-up.d
/etc/network/if-up.d/ntpdate
/etc/dhcp
/etc/dhcp/dhclient-exit-hooks.d
/etc/dhcp/dhclient-exit-hooks.d/ntpdate
/etc/logcheck
/etc/logcheck/ignore.d.server
/etc/logcheck/ignore.d.server/ntpdate
/etc/default
/etc/default/ntpdate
/var
/var/lib
/var/lib/ntpdate
/usr
/usr/sbin
/usr/sbin/ntpdate-debian
/usr/sbin/ntpdate
/usr/share
/usr/share/doc
/usr/share/doc/ntpdate
/usr/share/doc/ntpdate/copyright
/usr/share/doc/ntpdate/changelog.Debian.gz
/usr/share/doc/ntpdate/README.Debian
/usr/share/doc/ntpdate/NEWS.Debian.gz
/usr/share/man
/usr/share/man/man8
/usr/share/man/man8/ntpdate.8.gz
/usr/share/man/man8/ntpdate-debian.8.gz

/etc/network/if-up.d/ntpdate はネットワークに接続された時に実行されるものだ。怪しいのでこれを見てみる。

$ cat /etc/network/if-up.d/ntpdate
#!/bin/sh

set -e

PATH=/sbin:/bin:/usr/sbin:/usr/bin

# This is a heuristic:  The idea is that if a static interface is brought
# up, that is a major event, and we can put in some extra effort to fix
# the system time.  Feel free to change this, especially if you regularly
# bring up new network interfaces.
if [ "$METHOD" = static ]; then
        OPTS="-b"
fi

if [ "$METHOD" = loopback ]; then
        exit 0
fi

# Check whether ntpdate was removed but not purged; it's useless to wait for
# it in that case.
if [ ! -x /usr/sbin/ntpdate-debian ] && [ -d /usr/sbin ]; then
        exit 0
fi

(

# This is for the case that /usr will be mounted later.
if [ -r /lib/udev/hotplug.functions ]; then
        . /lib/udev/hotplug.functions
        wait_for_file /usr/sbin/ntpdate-debian
fi

if [ -e /usr/sbin/openntpd ]; then
    service='openntpd'
else
    service='ntp'
fi

invoke-rc.d --quiet $service stop >/dev/null 2>&1 || true

# Avoid running more than one at a time
flock -n /run/lock/ntpdate /usr/sbin/ntpdate-debian -s $OPTS 2>/dev/null || :

invoke-rc.d --quiet $service start >/dev/null 2>&1 || true

) &

invoke-rc.d --quiet $service stop >/dev/null 2>&1 || trueはntp.serviceを停止するものだ。その後にinvoke-rc.d --quiet $service start >/dev/null 2>&1 || trueでntp.serviceが起動されるが、多分そこに辿り着けていないのだろうと当たりを付けると、その間にあるflock -n /run/lock/ntpdate /usr/sbin/ntpdate-debian -s $OPTS 2>/dev/null || :が問題だろう。

というわけで今度は/usr/sbin/ntpdate-debianを見る。

$ cat /usr/sbin/ntpdate-debian
#!/bin/sh

set -e

if [ -r /etc/default/ntpdate ]; then
        . /etc/default/ntpdate
fi

if [ "$NTPDATE_USE_NTP_CONF" = yes ]; then
        for f in /var/lib/ntp/ntp.conf.dhcp /etc/ntp.conf /etc/openntpd/ntpd.conf; do
                if [ -r "$f" ]; then
                        file=$f
                        break
                fi
        done
        if [ -n "$file" ]; then
                NTPSERVERS=$(sed -rne 's/^(servers?|peer)[[:space:]]+(-[46][[:space:]]+)?([-_.:[:alnum:]]+).*$/\3/p' "$file" | grep -v '^127\.127\.') || [ $? -le 1 ]
        fi
elif [ -r /var/lib/ntpdate/default.dhcp ]; then
        . /var/lib/ntpdate/default.dhcp
fi

exec /usr/sbin/ntpdate $NTPOPTIONS "$@" $NTPSERVERS

つまり、/etc/default/ntpdate のNTPDATE_USE_NTP_CONFがyesの場合、/etc/ntp.confからserver, servers, peerディレクティブから問い合わせ先を取得してntpdateで問い合わせるという処理のようだ。

そして/etc/ntp.confはデフォルトでserverもpeerもなくpoolで指定されている(serversはOpenNTPD用)。

$ cat /etc/ntp.conf
(略)

# Specify one or more NTP servers.

# 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.
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst

# Use Ubuntu's ntp server as a fallback.
pool ntp.ubuntu.com

(略)

これが原因か。

解決

それでは/usr/sbin/ntpdate-debianを書き換えてpoolからも問い合わせ先を取得できるようにしてからOS再起動。

$ sudo sed -i 's/servers?|peer/servers?|pool|peer/' /usr/sbin/ntpdate-debian
$ sudo reboot

問題ないね。

$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 0.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 1.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 2.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 3.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 ntp.ubuntu.com  .POOL.          16 p    -   64    0    0.000    0.000   0.000
-wktk-sub.tk     210.173.160.87   3 u   42   64    3   20.318    4.537   5.074
*sv01.azsx.net   103.1.106.69     2 u   39   64    3    6.241    1.019   3.372
+extendwings.com 133.243.238.164  2 u   41   64    3    4.961    1.002   3.494
+balthasar.gimas 181.170.187.161  3 u   42   64    3    6.345    2.090   3.292
+gw1.kohaaloha.c 133.243.238.244  2 u   43   64    3    5.550    0.794   2.574
-x.ns.gin.ntt.ne 249.224.99.213   2 u   38   64    3    3.688   -1.114   2.492
-sv2.localdomain 133.243.238.243  2 u   39   64    3    7.291    1.075   2.855
-ec2-54-64-6-78. 133.243.238.164  2 u   43   64    3    6.745    0.293   2.680
+jp.linode.oxoox 103.1.106.69     2 u   41   64    3    7.543    0.731   2.318
#y.ns.gin.ntt.ne 249.224.99.213   2 u   39   64    3    6.635   -4.997   2.338
 122x215x240x51. 223.255.185.2    2 u   39   64    3    4.166    0.412   2.646
#next.kkyy.me    133.243.238.244  2 u   40   64    3    5.200    0.705   3.188
-golem.canonical 192.150.70.56    2 u   40   64    3  238.342    8.668  10.597

$ systemctl status ntp.service
● ntp.service - LSB: Start NTP daemon
   Loaded: loaded (/etc/init.d/ntp; bad; vendor preset: enabled)
   Active: active (running) since Sun 2016-04-24 22:31:25 JST; 2min 15s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1162 ExecStart=/etc/init.d/ntp start (code=exited, status=0/SUCCESS)
    Tasks: 2 (limit: 512)
   Memory: 1.7M
      CPU: 57ms
   CGroup: /system.slice/ntp.service
           └─1174 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 111:117

Apr 24 22:31:28 ubuntu1604 ntpd[1174]: Soliciting pool server 106.187.50.84
Apr 24 22:31:29 ubuntu1604 ntpd[1174]: Soliciting pool server 129.250.35.250
Apr 24 22:31:29 ubuntu1604 ntpd[1174]: Soliciting pool server 106.187.100.179
Apr 24 22:31:29 ubuntu1604 ntpd[1174]: Soliciting pool server 157.7.236.66
Apr 24 22:31:29 ubuntu1604 ntpd[1174]: Soliciting pool server 54.64.6.78
Apr 24 22:31:30 ubuntu1604 ntpd[1174]: Soliciting pool server 129.250.35.251
Apr 24 22:31:30 ubuntu1604 ntpd[1174]: Soliciting pool server 60.56.214.78
Apr 24 22:31:30 ubuntu1604 ntpd[1174]: Soliciting pool server 122.215.240.51
Apr 24 22:31:30 ubuntu1604 ntpd[1174]: Soliciting pool server 133.130.120.204
Apr 24 22:31:31 ubuntu1604 ntpd[1174]: Soliciting pool server 91.189.89.199

なお、/etc/network/if-up.d/ntpdateの中のflock -n /run/lock/ntpdate /usr/sbin/ntpdate-debian -s $OPTS 2>/dev/null || :行がUbuntu 15.10の時点では/usr/sbin/ntpdate-debian -s $OPTS 2>/dev/null || :になっていた。
ここが変わったのが問題発生の契機のようだ。

まあそのうち直りそうなものだ。

10
11
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
10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?