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

Ted*_*ham 7 linux ntp ntpd

我有 20 台左右的 linux 服务器,我想将它们的所有时钟同步到单个 NTP 服务器,该服务器与我的服务器位于同一机架和交换机中。没有什么是虚拟化的。

我们的管理员很难将各种机器上的时钟同步到大约 500 毫秒以内。我已经猜到了,这篇文章暗示我们应该能够将 linux 盒子同步到源和彼此的 2 毫秒内。

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

Kil*_*ilo 12

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

首先,您需要一个 NTP 主源。因此,您的其中一台 Linux 服务器将成为主服务器。我会创建一个名为 time.example.com 的 DNS A 记录(假设 example.com 是域)。这样,如果您的主服务器移动,您就不需要更新其他 19 个服务器。

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

这是我们的主 /etc/ntp.conf 文件之一的样子。请注意,这是一个使用 172.17.xx 的具有私有地址空间 (RFC1918) 的数据中心,因此您需要进行相应调整。如果您想要多个主服务器,请创建多个 DNS A 记录,每个记录具有不同的 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
Run Code Online (Sandbox Code Playgroud)

现在在每个客户端上,我们都有一个 /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
Run Code Online (Sandbox Code Playgroud)

使用 ntpq 命令查看与您同步的服务器。它为您提供了一个已配置的时间服务器列表以及您的服务器遇到的延迟、偏移和抖动。为了正确同步,延迟和偏移值应为非零值且抖动值应低于 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 等)


Ram*_*mar 0

我不确定您是否可以实现如此少的时间同步,但正确配置 ntp 服务器将使服务器同步几乎 10-20 毫秒,就像我在我的服务器上所做的那样。尽量减少漂移时间。这并非不可能,但在设置 NTP 服务器并将所有服务器指向该 NTP 服务器后,第一次手动同步时间将减少黑白服务器的时间差异。