操作系统系统时钟会不准确,这个时候就需要一个时钟服务器进行同步、自动调整。如果内网环境无法访问公网,或是受限的网络访问,就需要搭建一个专门用于时钟同步的服务器。下面记录搭建的过程。
ntpdate
已经被chrony
所取代,所以优先选择chrony
来搭建时钟服务器。
使用的操作系统为 CentOS 7。这台系统需要保证时钟准确,比如通过配置同步其他的时钟服务器。
CentOS 7 默认安装了 chrony
软件包。可以通过 yum info chrony
进行确认。
默认 chrony
处于运行状态,作为客户端从 NTP 服务器同步系统时钟,不会用做 NTP
服务器使用。默认配置文件 allow
指令处于注释状态,放开 allow
后将作为 NTP
服务器使用,且监听标准 NTP
协议端口 123
。一个基于默认(CentOS 7)配置(/etc/chrony.conf
)的修改示例如下:
# Use public ##servers from the ##pool.ntp.org project.
# Please consider joining the ##pool (http://www.pool.ntp.org/join.html).
###pool 2.centos.pool.ntp.org iburst
# server 通常需要修改,可以使用国内的 NTP 服务器
# 比如阿里云,参考:https://help.aliyun.com/zh/ecs/user-guide/alibaba-cloud-ntp-server
# server ntp.aliyun.com iburst
# server ntp1.aliyun.com iburst
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
# 公有云上的服务器一般会自动设置指向自己的 NTP 服务器,比如如下为华为云华为云
# server ntp.myhuaweicloud.com minpoll 4 maxpoll 10 iburst
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift
# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync
# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *
# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2
# Allow NTP client access from local network.
# 指定主机、子网或网络来允许 NTP 连接,可以指定多个 allow 指令允许多个主机、子网或网络
#allow 192.168/16
# 0.0.0.0/0 表示允许所有 IP
allow 0.0.0.0/0
# Listen for commands only on localhost.
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
# Serve time even if not synchronized to a time source.
# 如果 NTP 服务器没有同步成功时钟,则也可以用本地时间返回 NTP client,比较危险的设置,通常不放开
#local stratum 10
# Specify file containing keys for NTP authentication.
# 只是为了匹配 NTP Client server 设置中的 key ${id},精确的匹配客户端的访问
# 如果不设置 key ${id} 也不会影响同步
keyfile /etc/chrony.keys
# Get TAI-UTC offset and leap seconds from the system tz database.
#leapsectz right/UTC
# Specify directory for log files.
logdir /var/log/chrony
# Select which information is logged.
#log measurements statistics tracking
客户端也需要运行 chronyd
进程。通常只需要修改 /etc/chrony.conf
中 server
指令指向目标 NTP 服务器,并确保没有开启 allow
指令,其他字段可以留作默认。一个简单示例如下:
# 假设 chronyd NTP 服务器 IP 为 10.5.13.127
# chrony 支持设置端口,ntpd 不支持,如果不是 123 时需要指定,这种场景比如 chronyd 在代理后面
# key ${id} 用于精确指定客户端匹配,指定且匹配才能正确同步,但不设置也能正确同步。设置的意义:防止计算机因发送给它们的恶意数据包而扰乱其系统时间(引用)。
server 10.5.13.127 port 19000 iburst key 1
driftfile /var/lib/chrony/drift
logdir /var/log/chrony
keyfile /etc/chrony.keys
# 确保注释掉,或直接删除
# allow 192.0.2.123
场景:内网的几台服务器无法访问公共 NTP 服务,且内网没有可用的 NTP 服务。但是几台内网服务器可以访问特定公网的特定端口。为了使得内网的时钟准确,且同步。特意在可访问的公网及端口上暴露 NTP 服务。
将 chronyd
作为 NTP 服务器时,会监听 123
端口,NTP 为 UDP 协议。如果 123
端口并不是实际暴露在公网上的端口,需要通过代理来暴露服务。选用 Nginx
时作为代理时的设置方式示例:
stream {
server {
# 可以重用 19000 正常监听 HTTP(TCP) 协议
# 监听 udp 协议,转发给 chronyd 监听的 123 端口
listen 19000 udp;
proxy_pass 10.5.13.127:123;
}
}
问题:这种设置有没有什么问题?
可以在 chronyd
NTP 服务器上、chronyd
NTP Client 上通过 tcpdump
查看流量情况。
# NTP 服务器
tcpdump -i eth0 udp port 123
# NTP Client
tcpdump -i eth0 udp port 123
# 或者查看访问的代理端口,比如 19000
tcpdump -i eth0 udp port 19000
工作正常时,会有流量的往返。如下示例。
# `server` 指令中未设置 key ${id}
20:39:29.528401 IP VM-16-16-centos.43044 > 169.254.0.80.ntp: NTPv4, Client, length 48
20:39:29.530848 IP 169.254.0.80.ntp > VM-16-16-centos.43044: NTPv4, Server, length 48
20:39:30.895430 IP VM-16-16-centos.41198 > 169.254.0.82.ntp: NTPv4, Client, length 48
20:39:30.897496 IP 169.254.0.82.ntp > VM-16-16-centos.41198: NTPv4, Server, length 48
# `server` 指令中设置 key ${id}
21:44:54.949446 IP 153.35.170.70.35091 > VM-16-16-centos.ntp: NTPv4, Client, length 72
21:44:54.949557 IP VM-16-16-centos.ntp > 153.35.170.70.35091: NTPv4, Server, length 72
21:44:56.971855 IP 153.35.170.70.44884 > VM-16-16-centos.ntp: NTPv4, Client, length 72
21:44:56.971947 IP VM-16-16-centos.ntp > 153.35.170.70.44884: NTPv4, Server, length 72
当
server
指令中使用了keyfile /etc/chrony.keys
中id
时,NTP 包为 72 字节,未使用时为 48 字节。因为数据包中包含了校验和。
AGAIN: 把 NTPD
放在代理后面有没有潜在问题?
参考:
评论 / 共 0 条