一、背景原因
因为某个项目环境为内网,不能与互联网时间保持一致,为了保证时间一致性,尤其是对数据库的影响很大,且Doris数据库要求时间误差在5s内。
整体思路为:
选择一台Web服务器,手动将其时间设置与互联网时间相似,此时这台服务器为A
在A机器上部署NTP服务器
在其余服务器上配置NTP客户端,并指向A服务器IP
二、部署NTP服务器
NTP服务端安装包:ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
NTP客户端安装包:ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm
1、上传安装包并安装NTP服务
rpm -ivh /home/ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
*我这里需要先安装依赖

依赖安装包名称:
autogen-libopts-5.18-5.el7.x86_64.rpm
ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm
同样上传至服务器安装

再次安装NTP
2、配置NTP服务端
编辑/etc/ntp.conf,添加本地时钟源并允许内网访问:
server 127.127.1.0 # 使用本地硬件时钟作为时间源
fudge 127.127.1.0 stratum 10 # 设置层级为10(内网主时钟)
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 允许内网设备同步[3](@ref)完整如下
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 10.0.12.0 mask 255.255.255.0 nomodify notrap # 允许内网设备同步[3](@ref)
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 127.127.1.0
fudge 127.127.1.0 stratum 10 # 设置层级为10(内网主时钟)
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor
3、启动NTP服务并设置开机自启
sudo systemctl start ntpd && sudo systemctl enable ntpd
若需放行防火墙需额外执行:
sudo firewall-cmd --add-service=ntp --permanent && sudo firewall-cmd --reload
三、配置客户端
1、安装NTP客户端
与安装服务器端保持一致
2、配置NTP客户端指向主NTP服务器IP
编辑/etc/ntp.conf,删除默认服务器并添加:
server 10.0.12.27 iburst # 指定内网主服务器
3、重启服务并验证
# NTP客户端
sudo systemctl restart ntpd
ntpq -pn # 查看同步状态,若显示10.0.12.27且偏移量(offset)接近0则成功
# 设置开机自启动
sudo systemctl enable ntpd四、附加优化措施
1、时区统一
在所有服务器上执行:
timedatectl set-timezone Asia/Shanghai # 设置为上海时区(或其他统一时区)[1,6](@ref)
2、硬件时间同步
防止重启后时间偏移:
hwclock --systohc # 将系统时间写入硬件时钟[1](@ref)
*注意:防火墙问题,确保UDP 123端口开放