Ubuntu 搭建 NTP 服务器 + Windows 成功同步时间
Linux 安装与配置 NTP 服务(Ubuntu/Debian)
适用于:Ubuntu 18.04 / 20.04 / 22.04、Debian 10/11 等
目标:安装ntp服务,使其作为时间服务器供内网设备(如 Windows)同步IP为xxx.xxx.xxx.xxx
一、安装 NTP 服务
1. 更新软件包列表
sudo apt update
2. 安装 ntp 软件包
sudo apt install ntp -y
✅ 安装后会自动启动
ntp服务并设置为开机自启。
3. 验证服务状态
systemctl status ntp
正常应显示:
● ntp.service - Network Time Service
Loaded: loaded (/lib/systemd/system/ntp.service; enabled; preset: enabled)
Active: active (running) since ...
二、(可选)停止并禁用 systemd-timesyncd(避免冲突)
现代 Ubuntu 默认启用轻量级时间同步服务 systemd-timesyncd,它与 ntp 功能重叠,建议关闭:
sudo timedatectl set-ntp off # 关闭自动时间同步
sudo systemctl stop systemd-timesyncd
sudo systemctl disable systemd-timesyncd
⚠️ 如果不关闭,可能导致时间被两个服务同时调整,引发不稳定。
三、配置 NTP 作为内网时间服务器
编辑配置文件:
sudo nano /etc/ntp.conf
推荐配置(使用本地时钟作为权威源,开放给内网):
# 使用本地系统时钟作为时间源(stratum=3)
server 127.127.1.0
fudge 127.127.1.0 stratum 3
# 默认安全策略:拒绝所有修改、对等连接,但允许查询和同步
restrict -4 default kod nomodify notrap nopeer limited
restrict -6 default kod nomodify notrap nopeer noquery
# 允许本机完全访问
restrict 127.0.0.1
restrict ::1
# 漂移文件(记录时钟频率偏差)
driftfile /var/lib/ntp/ntp.drift
🔒 说明:
limited:允许客户端查询和同步,但限制请求频率- 去掉
noquery是为了让 Windows 能通过w32tm查询状态- 不指定公网 NTP 源(因内网可能无法访问外网)
四、重启 NTP 服务使配置生效
sudo systemctl restart ntp
五、验证 NTP 服务是否正常
1. 查看同步源状态
ntpq -p
✅ 正确输出示例:
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 3 l 12 64 377 0.000 0.000 0.001
*表示当前主时间源st=3表示层级合法(Windows 可接受)reach=377表示连续 8 次通信成功
2. 检查是否监听 UDP 123
ss -uln | grep ':123'
应看到:
UNCONN 0 0 *:123 *:*
六、(可选)开放防火墙(如果启用)
如果你启用了 ufw,需放行 NTP 端口:
sudo ufw allow 123/udp
sudo ufw reload
若
ufw status显示inactive,则无需操作。
七、常见问题排查
| 问题 | 解决方法 |
|---|---|
ntpq -p 显示 st=16 | 未正确配置本地源,或仍尝试连接外网失败 |
command not found: ntpq | 未安装 ntp,请运行 sudo apt install ntp |
| Windows 同步失败 | 确保 Ubuntu stratum < 16,且 Windows 配置为 Type=NTP |
✅ 总结:安装 NTP 的核心命令(一键参考)
# 安装
sudo apt update
sudo apt install ntp -y
# 关闭冲突服务
sudo timedatectl set-ntp off
sudo systemctl disable --now systemd-timesyncd
# 配置(替换为本地源)
echo '
server 127.127.1.0
fudge 127.127.1.0 stratum 3
restrict -4 default kod nomodify notrap nopeer limited
restrict 127.0.0.1
driftfile /var/lib/ntp/ntp.drift
' | sudo tee /etc/ntp.conf
# 重启
sudo systemctl restart ntp
# 验证
ntpq -p
现在你的 Linux 主机已成功部署为 内网 NTP 时间服务器,可被 Windows、路由器、交换机等设备同步时间。
解决方案:Ubuntu 搭建 NTP 服务器 + Windows 成功同步时间
适用场景:
- Ubuntu 作为内网 NTP 时间服务器
- Windows 客户端无法同步时间(报错:“没有可用的时间数据”)
- 网络可达(
w32tm /stripchart能通信),但/resync失败
一、问题现象
- Ubuntu 上
ntpd服务运行正常,监听 UDP 123 - Windows 可通过
w32tm /stripchart /computer:xxx.xxx.xxx.xxx查看时间偏移(如-38.14s)也可执行w32tm /stripchart /computer:xxx.xxx.xxx.xxx /samples:5 /dataonly - 但执行
w32tm /resync /force报错:[FAILED] The computer did not resync because no time data was available. w32tm /query /source显示Local CMOS Clock,而非你的 NTP 服务器 IP
二、根本原因
-
Ubuntu NTP 未正确配置为权威时间源
→ 初始使用公网 pool,但因网络限制无法同步,导致 stratum=16(不可用) -
Windows 时间服务未切换到 NTP 客户端模式
→ 默认为NT5DS模式(用于域环境),会忽略手动配置,回退到本地 CMOS 时钟 -
未以管理员权限配置 Windows 时间服务
→ 配置未写入注册表,导致无效
三、解决方案
✅ 步骤 1:在 Ubuntu 上配置本地 NTP 服务器(内网模式)
# 停止 NTP 服务
sudo systemctl stop ntp
# 替换为本地时钟源配置(不限制客户端网段)
sudo tee /etc/ntp.conf > /dev/null <<'EOF'
server 127.127.1.0
fudge 127.127.1.0 stratum 3
# 允许任意 IPv4 客户端同步(仅限可信内网!)
restrict -4 default kod nomodify notrap nopeer limited
# 允许本机
restrict 127.0.0.1
driftfile /var/lib/ntp/ntp.drift
EOF
# 启动服务
sudo systemctl start ntp
# 验证状态(必须看到 st=3)
ntpq -p
✅ 正确输出示例:
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 3 l 10 64 377 0.000 0.000 0.001
⚠️ 若仍显示
.POOL.或st=16,说明配置未生效,请检查文件内容并重启服务。
✅ 步骤 2:在 Windows 上强制切换为 NTP 客户端模式
必须以管理员身份运行命令提示符!
:: 1. 配置手动 NTP 源
w32tm /config /manualpeerlist:"xxx.xxx.xxx.xxx" /syncfromflags:manual /reliable:yes /update
:: 2. 强制注册表类型为 NTP(关键!)
reg add "HKLMSYSTEMCurrentControlSetServicesW32TimeParameters" /v Type /t REG_SZ /d NTP /f
:: 3. 重启时间服务
net stop w32time && net start w32time
:: 4. 等待初始化
timeout /t 10
:: 5. 验证当前时间源
w32tm /query /source
✅ 成功输出应为:
xxx.xxx.xxx.xxx
❌ 若仍为 Local CMOS Clock,请检查:
- 是否以管理员身份运行
- 是否加入域(运行
systeminfo | findstr /i domain)
✅ 步骤 3:强制同步时间
w32tm /resync /force
✅ 成功提示:
[SUCCESS] The computer time was successfully synchronized.
四、附加说明
🔒 安全建议
- 仅在可信内网中开放 NTP 服务
- 如需限制客户端,将
restrict -4 default ...替换为具体网段:restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap limited
🌐 域环境处理
- 若 Windows 加入了域,不能手动指定 NTP 源
- 应在域控制器上配置外部 NTP 源,其他机器自动同步 DC
🔄 手动校准 Ubuntu 时间(可选)
若 Ubuntu 时间不准,可临时校准:
sudo date -s "2025-12-15 13:00:00"
五、验证清单
| 检查项 | 命令 | 期望结果 |
|---|---|---|
| Ubuntu NTP 状态 | ntpq -p | *LOCAL(0),st=3 |
| Windows 时间源 | w32tm /query /source | xxx.xxx.xxx.xxx |
| Windows 服务类型 | reg query ... /v Type | NTP |
| 同步结果 | w32tm /resync /force | [SUCCESS] |
✅ 完成以上步骤后,Windows 将持续从你的 Ubuntu 服务器同步时间,误差控制在毫秒级。






