马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
时间不同步在集群里是个隐性问题:日志 顺序错乱、证书校验失败、定时任务错开、Kerberos 门禁全都会受影响。NTP 就是解决这件事的。这篇记录在 CentOS 7 上从装包、换源、开端口、改配置到客户端验证的完整流程。
一、NTP 是什么
网络时间协议(Network Time Protocol)用来让计算机时间与其他时间源同步,精度从毫秒到微秒级。稳妥做法是内网自建一台时间源统一对外提供,而不是每台机器各自去公网对时。
二、安装
rpm -qa | grep ntp
没有输出就是没装。CentOS 7 上:
yum install -y ntp
如果报 Cannot find a valid baseurl for repo: base/7/x86_64
按三步排查:
- 先确认能不能上网:
ping www.baidu.com,通就说明网络没问题。
- 换镜像源:编辑
/etc/yum.repos.d/CentOS-Base.repo,把 mirrorlist 注释掉、baseurl 指向可用镜像(例如阿里云镜像站的 /centos/$releasever/os/$basearch/),保存后重试安装。
- 还不行就查 DNS:改
/etc/resolv.conf,加稳定的 DNS:
nameserver 8.8.8.8
nameserver 8.8.4.4
# iptables
sudo iptables -A INPUT -p udp --dport 123 -j ACCEPT
sudo iptables -A OUTPUT -p udp --sport 123 -j ACCEPT
# firewalld
sudo firewall-cmd --permanent --add-port=123/udp
sudo firewall-cmd --reload
更好做法是内网时间服务一般不对外暴露,只用 firewalld 限制来源网段即可,公网一律不开放 123。
四、配置 /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
# 允许本机回环向本机同步
restrict 127.0.0.1
# 允许整个内网使用本服务同步时间
restrict default ignore
# 上游时间源,prefer 表示优先
server pool.ntp.org prefer
server ntp.aliyun.com
server cn.ntp.org.cn
keys /etc/ntp/keys
disable monitor
几个关键字:
server ... prefer:优先同步的上游。
restrict <网段> mask <掩码> nomodify notrap:限定谁能来同步(原文注释里的写法可按需放开到指定网段)。
restrict default ignore:默认不响应外部查询,是较安全的基线。
改完重启:
systemctl restart ntpd
systemctl enable ntpd
ntpq -p # 看同步情况
五、Linux 客户端
客户端也要装 ntp,把 /etc/ntp.conf 里的 server 指向内网时间服务器:
server 192.168.1.78 prefer
重启 ntpd 后稍等几分钟,用下面几条确认:
ntpq -p
date
timedatectl
六、Windows 客户端
控制面板 → 时钟和地区 → 日期和时间 → Internet 时间 → 更改设置,填入内网 NTP 服务器域名或 IP,点立即更新验证。
七、验证与排错
ntpq -p 能看到 +(已选中的时间源)和 reach、delay 列,说明同步链路正常。
- 刚启动几分钟内
offset 偏大是正常的,NTP 需要逐步收敛。
- 若一直
reach=0:查防火墙 123 端口、上游源是否可达、DNS 是否解析正常。
- chrony 也是常见替代(CentOS 8+/Rocky/Alma 多用它),配置思路一致:
chrony.conf 里写 server ... iburst。
qidao123.com企服评测-应用市场 编辑提示:本文为 Linux 搭建 NTP 服务的过程记录,时间服务影响认证与日志 ,生产环境请先在一台机器灰度验证。 |