AIGC标识 Linux 上位机对无网下位机的 NTP 授时方案

1. 方案概述

本方案用于以下场景:

  • 上位机 Linux 可以访问互联网;
  • 下位机没有互联网访问能力;
  • 上位机和下位机通过网线连接;
  • 上位机从公网 NTP 服务器校时;
  • 下位机只从上位机获取时间。

网络结构如下:

公网 NTP 服务器
        ▲
        │ Wi-Fi / Internet
上位机 Linux
公网网卡:wlp0s20f3
局域网 IP:192.168.127.111
运行 chrony NTP Server
        │ eth0 / enp2s0
        │ 192.168.127.0/24
        ▼
下位机 Linux
局域网 IP:192.168.127.x
无公网网络
运行 chrony NTP Client

数据流为:

上位机 chrony ← 公网 NTP
下位机 chrony ← 192.168.127.111

本方案不要求下位机访问互联网,也不要求配置默认网关或 NAT。下位机只需要能访问上位机的 UDP 123 端口。

2. 上位机配置

2.1 安装并启动 chrony

sudo apt update
sudo apt install chrony
sudo systemctl enable --now chrony

2.2 配置公网时间源和局域网访问权限

编辑配置文件:

sudo nano /etc/chrony/chrony.conf

保留或配置公网时间源,例如:

pool ntp.ubuntu.com iburst

允许下位机所在网段访问上位机的 NTP 服务:

allow 192.168.127.0/24

修改后重启:

sudo systemctl restart chrony

确认上位机已经同步到公网时间源:

sudo chronyc sources -v
sudo chronyc tracking

检查 chrony 是否监听 UDP 123:

sudo ss -lunp | grep ':123'

理想情况下可以看到 chronyd 监听 UDP 123。

如果上位机启用了 UFW 防火墙,允许下位机网段访问 NTP:

sudo ufw allow from 192.168.127.0/24 to any port 123 proto udp

3. 下位机配置

3.1 安装并启动 chrony

sudo apt update
sudo apt install chrony
sudo systemctl enable --now chrony

3.2 删除或注释公网时间源

编辑:

sudo nano /etc/chrony/chrony.conf

注释掉默认公网源,例如:

#pool ntp.ubuntu.com        iburst maxsources 4
#pool 0.ubuntu.pool.ntp.org iburst maxsources 1
#pool 1.ubuntu.pool.ntp.org iburst maxsources 1
#pool 2.ubuntu.pool.ntp.org iburst maxsources 2

添加上位机地址:

server 192.168.127.111 iburst

重启 chrony:

sudo systemctl restart chrony

立即触发同步:

sudo chronyc burst 4/4
sudo chronyc makestep

200 OK 只表示命令被 chronyd 接收,不代表授时已经成功。

4. 验证授时是否成功

查看下位机时间源:

sudo chronyc sources -v
sudo chronyc tracking
timedatectl status

成功时,sources -v 应出现:

^* 192.168.127.111

其中:

^*       当前正在使用的时间源
^?       时间源不可用或没有收到响应
Reach 0  最近没有收到有效响应

tracking 正常时应类似:

Reference ID    : 192.168.127.111
Stratum         : 2 或 3
Leap status     : Normal

timedatectl status 正常时应显示:

System clock synchronized: yes
NTP service: active

5. 是否需要一直运行

需要。上位机和下位机的 chrony 服务都应持续运行。

上位机 chrony 持续提供 NTP 服务,下位机 chrony 周期性向上位机请求时间并修正本地时钟。停止服务后,系统不会立即停止运行,但本地时钟会因晶振误差逐渐漂移。

建议设置开机自动启动:

sudo systemctl enable chrony

查看服务状态:

systemctl status chrony

6. RTC 时间处理

如果看到:

RTC time: Thu 1970-01-01

说明硬件 RTC 没有保存正确时间。它不影响当前 Linux 系统通过 NTP 校时,但下次断电启动时系统时间可能再次回到错误时间。

确认 NTP 同步成功后,将系统时间写入 RTC:

sudo hwclock --systohc

然后确认:

sudo hwclock --show
timedatectl status

RTC in local TZ: no 是推荐设置,表示 RTC 使用 UTC 保存时间。

7. 常见错误

下位机配置了错误的上位机地址

例如 chrony 配置为:

server 192.168.127.111 iburst

但上位机实际局域网地址是 192.168.54.111,则会出现 Reach 0。应以 ip addr 查到的上位机局域网地址为准。

上位机没有配置 allow

仅安装 chrony 不代表默认允许其他主机访问。上位机必须配置:

allow 192.168.127.0/24

把上位机当作 NTP Server,但上位机没有运行 NTP 服务

下位机配置 server 192.168.127.111 后,上位机必须运行 chrony、ntpd 或其他 NTP Server,并监听 UDP 123。

误以为 chronyc burst 4/4 成功就代表授时成功

应以以下结果为准:

^* 192.168.127.111
Leap status: Normal
System clock synchronized: yes

多个时间同步服务同时运行

chrony、systemd-timesyncd 和 ntpd 通常只应运行一个。检查:

ps -ef | grep -E 'chronyd|systemd-timesyncd|ntpd'

9. 精度和适用范围

该方案是普通 Linux NTP 授时,适合:

  • 上下位机日志时间统一;
  • 普通状态数据时间戳;
  • 非严格实时控制;
  • 毫秒级时间同步。

NTP 的实际精度受网络延迟、Linux 调度、CPU 负载和系统时钟质量影响。它不适合严格的微秒级传感器采样同步。

如果需要相机、IMU、激光雷达等设备在同一采样时刻工作,应额外使用 PTP、PPS、SYNC 或硬件触发。NTP 只负责让设备时钟大致一致,不等于传感器采样事件完全同时。

10. 最终命令摘要

上位机:

sudo apt install chrony
sudo systemctl enable --now chrony
sudo systemctl restart chrony
sudo ss -lunp | grep ':123'
sudo chronyc tracking

上位机 /etc/chrony/chrony.conf:

pool ntp.ubuntu.com iburst
allow 192.168.127.0/24

下位机 /etc/chrony/chrony.conf:

server 192.168.127.111 iburst

下位机:

sudo systemctl enable --now chrony
sudo systemctl restart chrony
sudo chronyc burst 4/4
sudo chronyc makestep
sudo chronyc sources -v
sudo chronyc tracking
timedatectl status

最终判断标准:下位机显示 ^* 192.168.127.111,Reach 为非零,Leap status 为 Normal,并且 timedatectl 显示 System clock synchronized: yes。

posted @ 2026-11-04 17:30  xiao东  阅读(2)  评论(0)    收藏  举报