使用NTP将一组linux服务器同步到一个公共时间源

我有20左右的Linux服务器,我想同步所有的时钟到一个NTP服务器,这是在同一个机架和交换机作为我的服务器。 没有什么是虚拟化的。

我们的pipe理员在各台机器上同步时间比大约500毫秒更难。 我会猜测, 这个post意味着我们应该能够得到同步到源和对方的2毫秒内的Linux机器。

我对NTP的期望是不合理的吗? 任何提示pipe理员应该做什么/检查?

我拥有一家托pipe公司,我们正是这样做的。 这是我们如何做到这一点。

首先,您需要一个NTP主源。 所以你的一个Linux服务器将成为主人。 我将创build一个名为time.example.com的DNS Alogging(假设example.com是域名)。 这样,如果你的主人移动你不需要更新其他19台服务器。

在主服务器上,您需要有一个适当configuration的ntp.conf文件。

这是我们的一个主/etc/ntp.conf文件。 请注意,这是一个使用172.17.xx的私有地址空间(RFC1918)的数据中心,因此您需要进行相应的调整。 如果您需要多个主服务器,请创build多个DNS Alogging,每个IPlogging具有不同的IP,以便在需要时获得一定的容错能力。

server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 server 0.north-america.pool.ntp.org server 1.north-america.pool.ntp.org server 2.north-america.pool.ntp.org server 3.north-america.pool.ntp.org # Logging & Stats statistics loopstats statsdir /var/log/ntp/ filegen peerstats file peers type day link enable filegen loopstats file loops type day link enable # Drift file. Put this in a directory which the daemon can write to. # No symbolic links allowed, either, since the daemon updates the file # by creating a temporary in the same directory and then rename()'ing # it to the file. # driftfile /etc/ntp/drift broadcastdelay 0.008 restrict default noquery nomodify restrict 0.north-america.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery restrict 1.north-america.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery restrict 2.north-america.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery restrict 3.north-america.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery # Allow LAN to query us restrict 172.17.0.0 mask 255.255.0.0 nomodify notrap # Trust ourselves. :-) restrict 127.0.0.1 

现在在每个客户端上,我们都有一个如下所示的/etc/ntp.conf文件:

 server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 server time.example.com # Drift file. Put this in a directory which the daemon can write to. # No symbolic links allowed, either, since the daemon updates the file # by creating a temporary in the same directory and then rename()'ing # it to the file. driftfile /etc/ntp/drift multicastclient # listen on default 224.0.1.1 broadcastdelay 0.008 # Don't serve time or stats to anyone else by default (more secure) restrict default noquery nomodify restrict time.example.com mask 255.255.255.255 nomodify notrap noquery # Allow LAN to query us restrict 172.17.0.0 mask 255.255.0.0 nomodify notrap # Trust ourselves. :-) restrict 127.0.0.1 

使用ntpq命令查看与之同步的服务器。 它为您提供了configuration的时间服务器列表以及您的服务器遇到的延迟,偏移和抖动。 对于正确的同步,延迟和偏移值应该是非零的,抖动值应该在100以下。

同样在我们的客户端节点上,我们有一个rc脚本(/etc/rc.d/rc.local),在启动NTPD守护进程之前同步时钟。 这里是重要的部分…他们是顺序依赖的。

将客户端的时钟与主时间源/ usr / sbin / ntpdate -b time.example.com同步

启动NTPD守护进程,以便在启动过程中进行大量的时间调整。 / usr / sbin / ntpd -g -x

最后,根据您的设置,您需要打出防火墙规则以允许您的time.example.com主站通过UDP端口访问公共Internet。 这是一个典型的,适当放置的IPTables规则

iptables -t nat -A POSTROUTING -o $ PUB_IF -p udp –dport 123 -j MASQUERADE

其中PUB_IF是公共接口(eth0,eth1,无论什么)

正确configurationNTP将在几ms内实现同步。 我总是确保每个NTP客户端与至less三个NTP服务器进行通信。

使用ntpq -p来监视状态 – 它应该指出为什么你没有得到更好的同步。

我不确定是否可以实现如此less的时间同步,但正确的ntp服务器configuration将使服务器同步我的服务器上已经完成的10-20毫秒。 最大限度地减less漂移时间。 这并非不可能,但是在设置NTP服务器并将所有服务器指向该NTP服务器之后,手动首次同步时间将减less时间差异服务器。