在 Ubuntu Server 内网环境里设置 NTP(时间同步)服务器,通常有两种常见场景:
- 这台 Ubuntu 作为客户端:去同步你内网已有的时间服务器
- 这台 Ubuntu 作为内网 NTP 服务器:给其他机器提供时间服务
最常用的是用 chrony(Ubuntu 新版本默认推荐),比老的 ntpd 更轻量、稳定。
方案一:Ubuntu 作为 NTP 客户端(同步内网时间源)
假设你的内网 NTP 服务器 IP 是 192.168.1.100
1. 安装 chrony
bash
sudo apt update
sudo apt install chrony -y
2. 修改配置文件
编辑:
bash
sudo nano /etc/chrony/chrony.conf
找到默认的公网 server/pool 行(类似 pool ntp.ubuntu.com iburst),注释掉:
conf
# pool ntp.ubuntu.com iburst
添加你的内网时间源:
conf
server 192.168.1.100 iburst
如果有多个内网时间源:
conf
server 192.168.1.100 iburst
server 192.168.1.101 iburst
server 192.168.1.102 iburst
3. 重启服务
bash
sudo systemctl restart chrony
sudo systemctl enable chrony
4. 检查同步状态
bash
chronyc sources
以及:
bash
chronyc tracking
正常会看到类似:
text
Leap status : Normal
Reference ID : C0A80164
方案二:Ubuntu 作为内网 NTP 服务器
如果你想让这台服务器给整个局域网提供授时:
1. 安装 chrony
bash
sudo apt install chrony -y
2. 编辑配置
bash
sudo nano /etc/chrony/chrony.conf
增加:
conf
allow 192.168.1.0/24
local stratum 10
说明:
allow:允许哪个网段访问local stratum 10:即使没公网,也可作为本地时间源
如果它上游还能同步外部或上级内网 NTP:
conf
server 192.168.1.100 iburst
完整示例:
conf
server 192.168.1.100 iburst
allow 192.168.1.0/24
local stratum 10
3. 开启服务
bash
sudo systemctl restart chrony
sudo systemctl enable chrony
4. 防火墙放行 UDP 123
如果启用了 ufw:
bash
sudo ufw allow 123/udp
检查是否监听 NTP 端口
bash
ss -lunp | grep 123
应看到:
text
udp UNCONN 0 0 0.0.0.0:123
客户端测试
其他机器可执行:
bash
ntpdate -q 192.168.1.10
(其中 192.168.1.10 是你的 Ubuntu NTP 服务器)
推荐(生产环境)
在内网环境建议这样部署:
text
上级授时源(GPS/防火墙/交换机)
↓
Ubuntu NTP Server
↓
所有业务服务器
优点:
- 所有机器时间统一
- 避免每台服务器单独访问外部时间源
- 适合离线/隔离网络
如果你告诉我你的具体场景(比如 Ubuntu 22.04 还是 20.04 ,以及是 做客户端还是做授时服务器),我可以给你一套最简配置。