用的是CloudCone的VPS, 发现一件很诡异的事情. 系统从没有动过, 安装的ntp服务一直也在运行, 时区也是正确的, 偏偏系统时间是错误的. 即使卸载再重新安装ntp服务也是一样.
问题排除
$ ntpdate -ud ntp.ubuntu.com
17 Sep 14:05:04 ntpdate[9117]: ntpdate [email protected] (1)
Looking for host ntp.ubuntu.com and service ntp
91.189.89.198 reversed to chilipepper.canonical.com
host found : chilipepper.canonical.com
transmit(91.189.89.198)
transmit(91.189.89.199)
transmit(91.189.94.4)
transmit(91.189.91.157)
transmit(91.189.89.198)
transmit(91.189.89.199)
transmit(91.189.94.4)
transmit(91.189.91.157)
transmit(91.189.89.198)
transmit(91.189.89.199)
transmit(91.189.94.4)
transmit(91.189.91.157)
transmit(91.189.89.198)
transmit(91.189.89.199)
transmit(91.189.94.4)
transmit(91.189.91.157)
transmit(91.189.89.198)
transmit(91.189.89.199)
transmit(91.189.94.4)
transmit(91.189.91.157)
91.189.89.198: Server dropped: no data
91.189.89.199: Server dropped: no data
91.189.94.4: Server dropped: no data
91.189.91.157: Server dropped: no data
server 91.189.89.198, port 123
stratum 0, precision 0, leap 00, trust 000
refid [91.189.89.198], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time: 00000000.00000000 Thu, Feb 7 2036 14:28:16.000
originate timestamp: 00000000.00000000 Thu, Feb 7 2036 14:28:16.000
transmit timestamp: df49c296.3d0a1d7b Mon, Sep 17 2018 14:05:10.238
filter delay: 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000
filter offset: 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000
delay 0.00000, dispersion 64.00000
offset 0.000000
server 91.189.89.199, port 123
stratum 0, precision 0, leap 00, trust 000
refid [91.189.89.199], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time: 00000000.00000000 Thu, Feb 7 2036 14:28:16.000
originate timestamp: 00000000.00000000 Thu, Feb 7 2036 14:28:16.000
transmit timestamp: df49c296.703703de Mon, Sep 17 2018 14:05:10.438
filter delay: 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000
filter offset: 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000
delay 0.00000, dispersion 64.00000
offset 0.000000
server 91.189.94.4, port 123
stratum 0, precision 0, leap 00, trust 000
refid [91.189.94.4], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time: 00000000.00000000 Thu, Feb 7 2036 14:28:16.000
originate timestamp: 00000000.00000000 Thu, Feb 7 2036 14:28:16.000
transmit timestamp: df49c296.a3685167 Mon, Sep 17 2018 14:05:10.638
filter delay: 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000
filter offset: 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000
delay 0.00000, dispersion 64.00000
offset 0.000000
server 91.189.91.157, port 123
stratum 0, precision 0, leap 00, trust 000
refid [91.189.91.157], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time: 00000000.00000000 Thu, Feb 7 2036 14:28:16.000
originate timestamp: 00000000.00000000 Thu, Feb 7 2036 14:28:16.000
transmit timestamp: df49c296.d69f3de6 Mon, Sep 17 2018 14:05:10.838
filter delay: 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000
filter offset: 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000
delay 0.00000, dispersion 64.00000
offset 0.000000
17 Sep 14:05:12 ntpdate[9117]: no server suitable for synchronization found
参数提示:
-d表示debugging mode, 使用-d选项时ntpdate会使用非特权端口去同步, 所以理论上-u参数可以省略
-u表示告诉ntpdate使用非特权端口进行同步(而不是默认的123端口)
可以看到, 出现了”Server dropped: no data”和”ntpdate: no server suitable for synchronization found”. 上网查了一下, 出现这种提示提示的话, 基本说明是firewall拦截了ntp的数据包(UDP的123端口). 其实想想也是, CloudCone这种自带较强防御的商家来说(据说自带20G防御?), block ntp的端口也在情理之中.
解决办法
安装htpdate, 通过http协议同步时间
$ apt-get install htpdate
$ systemctl status htpdate.service #此时应该是默认进入运行状态且系统时间已经同步正确了
$ grep -v ^# /etc/default/htpdate #查看其配置
HTP_SERVERS="www.pool.ntp.org www.ntp.br www.wikipedia.org"
HTP_OPTIONS="-D -s"