本文覆盖Linux两大主流发行版CentOS7+/Ubuntu18.04+的时间同步配置,同步工具优先推荐chrony(性能远优于ntpd),同时补充本地Windows Server/Windows 10/11的轻量NTP源搭建步骤,所有内容可直接复制执行。
先检查系统是否已默认安装chrony:
执行对应命令:
CentOS/Rocky/AlmaLinux 7-9
检查状态
systemctl status chronyd
未安装则执行
yum install -y chrony 7用yum,8+可用dnf install -y chrony
Ubuntu18.04+
检查状态
systemctl status chrony
未安装或需卸载旧ntpd的执行
apt update && apt install -y chrony
旧系统预装ntpd的可先卸载(可选但推荐)
apt remove -y ntp ntpdate
两个发行版的默认配置文件均为/etc/chrony.conf,编辑前先备份:
cp /etc/chrony.conf /etc/chrony.conf.bak
用vi/vim/nano打开文件,替换或修改核心配置项(完整可复制的内容根据需求选):
注释或删除默认的pool/server开头的配置
pool 2.centos.pool.ntp.org iburst
pool ntp.ubuntu.com iburst
国内公共源,阿里云腾讯云优先,教育网备选
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp.tencent.com iburst
server ntp3.aliyun.com iburst
server edu.ntp.org.cn iburst
允许本地局域网其他设备同步本机(可选,搭本地NTP源用)
allow 192.168.1.0/24 替换为你的局域网段
allow 10.0.0.0/8
本地时间偏移备份,防止断电重启后时间丢失
driftfile /var/lib/chrony/drift
开机10秒内即使未同步到源,也先给出本地时间给上层应用
makestep 1.0 3
允许本地RTC(实时时钟)同步
rtcsync
日志配置(可选)
logdir /var/log/chrony
注释或删除默认公共源
替换为你的本地Windows NTP源IP
server 192.168.1.100 iburst 替换为实际Windows IP
其他配置同需求1的driftfile、makestep、rtcsync部分
执行以下命令重启服务并设为开机自启:

CentOS/Rocky/AlmaLinux
systemctl restart chronyd
systemctl enable chronyd
Ubuntu
systemctl restart chrony
systemctl enable chrony
验证同步状态的3个关键命令:
chronyc sources -v(正常显示号标记当前同步源,^号标记未验证的,?号标记不可达的)chronyc tracking(重点看“System time”偏移量,单位秒,正常为1e-6以下级别)chronyc -a makestep无需安装第三方工具,直接通过修改注册表+组策略(或命令行)开启,适用于Windows Server 2012+、Windows 10/11专业版/企业版。
以管理员身份打开“命令提示符”或“PowerShell”,执行以下命令(可直接复制):
启用Windows Time服务并设为自动启动
sc config w32time start= auto
net start w32time
修改注册表配置项,16进制转10进制的说明:
AnnounceFlags=5 → 宣布为可靠时间源
NtpServer=1 → 启用NTP服务器
Type=NTP → 使用NTP协议(不是域控的NT5DS)
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config" /v AnnounceFlags /t REG_DWORD /d 5 /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer" /v Enabled /t REG_DWORD /d 1 /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters" /v Type /t REG_SZ /d NTP /f
重启Windows Time服务使配置生效
net stop w32time && net start w32time
验证本地NTP服务器是否正常响应
w32tm /query /configuration
NTP协议使用UDP 123端口,必须开放局域网访问权限:
管理员PowerShell执行
New-NetFirewallRule -DisplayName "允许局域网NTP同步" -Direction Inbound -Protocol UDP -LocalPort 123 -Action Allow
在局域网内另一台Windows设备(管理员身份)执行验证命令:
替换为你的NTP源IP
w32tm /stripchart /computer:192.168.1.100 /dataonly /samples:3
正常情况下会显示3次时间对比,偏移量在毫秒级别。
ping ntp1.aliyun.com)、防火墙(Linux临时关闭防火墙测试:systemctl stop firewalld,永久放通UDP123端口:firewall-cmd --permanent --add-port=123/udp && firewall-cmd --reload)chronyc -a makestep易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。
Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图