我在 EC2 服务器上进行的一些测试开始失败。深入研究后发现,原因是我的服务器之间存在严重的时间不匹配:
Sun Jun 22 14:43:43 UTC 2014 # Laptop
Sun Jun 22 14:46:36 UTC 2014 # Server #1
Sun Jun 22 07:50:19 UTC 2014 # Server #2
差异很明显——一台服务器上需要 3 分钟,另一台服务器上则需要大约 7 个小时。
因此,我编写了一个小脚本来计算一台服务器和其他一些服务器之间的时间差:
$ $ ./time_diff.py
+--------+---------------+----------------------+----------------------+-----------------------+
| Number | ip | difference (seconds) | local time (seconds) | remote time (seconds) |
+--------+---------------+----------------------+----------------------+-----------------------+
| 1 | 172.31.45.17 | -142.73 | 1403448641.8 | 1403448499.07 |
| 2 | 172.31.35.69 | -24977.82 | 1403448642.7 | 1403423664.87 |
| 3 | 172.31.47.152 | -141.79 | 1403448643.51 | 1403448501.72 |
| 4 | 172.31.47.151 | -119.05 | 1403448644.35 | 1403448525.31 |
| 5 | 172.31.32.31 | -124.29 | 1403448645.2 | 1403448520.91 |
| 6 | 172.31.26.134 | -122.59 | 1403448646.43 | 1403448523.85 |
| 7 | 172.31.27.224 | -161.85 | 1403448647.72 | 1403448485.87 |
| 8 | 172.31.17.112 | -156.70 | 1403448649.03 | 1403448492.32 |
+--------+---------------+----------------------+----------------------+-----------------------+
这非常令人惊讶:我以为虚拟机从底层物理机器获取时间,并且物理机器时间由 Amazon NTP 服务器妥善保存。
更新-ntpdate 我尝试使用 ntpdate,并且相关的 UDP 端口也打开了,但我无法让它修复漂移:
$ sudo ntpdate -dv 0.ubuntu.pool.ntp.org
22 Jun 15:20:56 ntpdate[11889]: ntpdate [email protected] Wed Oct 9 19:08:07 UTC 2013 (1)
Looking for host 0.ubuntu.pool.ntp.org and service ntp
host found : hydrogen.constant.com
transmit(108.61.73.243)
transmit(108.61.73.244)
transmit(173.255.118.107)
transmit(208.87.104.40)
transmit(108.61.73.243)
transmit(108.61.73.244)
transmit(173.255.118.107)
transmit(208.87.104.40)
transmit(108.61.73.243)
transmit(108.61.73.244)
transmit(173.255.118.107)
transmit(208.87.104.40)
transmit(108.61.73.243)
transmit(108.61.73.244)
transmit(173.255.118.107)
transmit(208.87.104.40)
transmit(108.61.73.243)
transmit(108.61.73.244)
transmit(173.255.118.107)
transmit(208.87.104.40)
108.61.73.243: Server dropped: no data
108.61.73.244: Server dropped: no data
173.255.118.107: Server dropped: no data
208.87.104.40: Server dropped: no data
server 108.61.73.243, port 123
stratum 0, precision 0, leap 00, trust 000
refid [108.61.73.243], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time: 00000000.00000000 Mon, Jan 1 1900 0:00:00.000
originate timestamp: 00000000.00000000 Mon, Jan 1 1900 0:00:00.000
transmit timestamp: d75172df.1d895ce9 Sun, Jun 22 2014 15:21:03.115
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 108.61.73.244, port 123
stratum 0, precision 0, leap 00, trust 000
refid [108.61.73.244], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time: 00000000.00000000 Mon, Jan 1 1900 0:00:00.000
originate timestamp: 00000000.00000000 Mon, Jan 1 1900 0:00:00.000
transmit timestamp: d75172df.50bc6bd2 Sun, Jun 22 2014 15:21:03.315
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 173.255.118.107, port 123
stratum 0, precision 0, leap 00, trust 000
refid [173.255.118.107], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time: 00000000.00000000 Mon, Jan 1 1900 0:00:00.000
originate timestamp: 00000000.00000000 Mon, Jan 1 1900 0:00:00.000
transmit timestamp: d75172df.83eeeb52 Sun, Jun 22 2014 15:21:03.515
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 208.87.104.40, port 123
stratum 0, precision 0, leap 00, trust 000
refid [208.87.104.40], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time: 00000000.00000000 Mon, Jan 1 1900 0:00:00.000
originate timestamp: 00000000.00000000 Mon, Jan 1 1900 0:00:00.000
transmit timestamp: d75172df.b722b2ea Sun, Jun 22 2014 15:21:03.715
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
22 Jun 15:21:05 ntpdate[11889]: no server suitable for synchronization found
如何同步 EC2 上 Ubuntu 服务器的时间?
答案1
要与 ntp 服务器保持时间同步,只需安装软件包ntp
并重新启动服务即可
/etc/init.d/ntp restart
答案2
解决使用ntpdate -u
。
看来,如果没有这个选项,EC2 机器就无法连接到其可用区域之外的 NTP 服务器。