PTP时钟同步
发布时间:2025-09-19 15:35:26.782 文章来源:AiSoftCloud 浏览次数:119 下载次数:1 

PTP时钟同步

安装 PTP 工具

  1. # Ubuntu/Debian系统
  2. sudo apt update
  3. sudo apt install linuxptp
  4. # CentOS/RHEL系统
  5. sudo yum install linuxptp
  6. # Fedora系统
  7. sudo dnf install linuxptp

确认网络接口支持

确保你的网络接口支持硬件时间戳(PTP 需要硬件支持才能达到最高精度):

  1. # 检查是否支持硬件时间戳
  2. ethtool -T eth0 # 将eth0替换为你的网络接口名

配置 PTP 服务

linuxptp提供了两个主要程序:
ptp4l:PTP 协议的实现
phc2sys:用于同步系统时钟和 PTP 硬件时钟 (PHC)

创建配置文件:

  1. sudo vim /etc/ptp4l.conf

基本配置内容:

  1. # 网络接口
  2. interface eth0
  3. # PTP模式,master表示主时钟,slave表示从时钟
  4. masterOnly 1 # 1为主时钟,0为从时钟
  5. # 时间源
  6. timeSource 0xA0
  7. # 优先级
  8. priority1 128
  9. priority2 128

关闭其他同步服务

  1. sudo systemctl list-unit-files | grep -E 'chrony|ntp|timesyncd'

将列出来的服务全部关闭并禁用:

  1. sudo systemctl stop ntp-systemd-netif.path ntp-systemd-netif.service ntp.service systemd-timesyncd.service
  2. sudo systemctl disable ntp-systemd-netif.path ntp-systemd-netif.service ntp.service systemd-timesyncd.service

启动 PTP 服务

作为主时钟启动

  1. # 启动ptp4l作为主时钟
  2. sudo ptp4l -i eth0 -m -S
  3. # 启动phc2sys同步系统时钟和硬件时钟
  4. sudo phc2sys -s eth0 -c CLOCK_REALTIME -w -m

作为从时钟启动

  1. # 启动ptp4l作为从时钟
  2. sudo ptp4l -i eth0 -m -s
  3. # 启动phc2sys同步系统时钟和硬件时钟
  4. sudo phc2sys -s eth0 -c CLOCK_REALTIME -w -m

参数说明:
-i:指定网络接口
-m:输出详细日志
-S:作为主时钟运行
-s:作为从时钟运行

配置系统服务(可选)

主机端(master)

网卡地址假设为 enp6s0:

配置硬件时钟同步服务(ptp4l)

为了让 PTP 服务在系统启动时自动运行,可以创建 systemd 服务文件:

  1. sudo nano /etc/systemd/system/ptp4l.service

文件内容:

  1. [Unit]
  2. Description=PTP clock synchronization service
  3. After=network.target
  4. [Service]
  5. Type=simple
  6. ExecStart=/usr/sbin/ptp4l -i enp6s0 -m -S # 根据需要修改为主时钟或从时钟模式
  7. Restart=always
  8. [Install]
  9. WantedBy=multi-user.target

然后启动并设置开机自启:

  1. sudo systemctl daemon-reload
  2. sudo systemctl start ptp4l
  3. sudo systemctl enable ptp4l

配置系统时钟同步服务(phc2sys)

为了让 PTP 服务在系统启动时自动运行,可以创建 systemd 服务文件:

  1. sudo nano /etc/systemd/system/ptpphc2sys.service

文件内容:

  1. [Unit]
  2. Description=PTP phc2sys service
  3. After=network.target
  4. [Service]
  5. Type=simple
  6. ExecStart=/usr/sbin/phc2sys -s enp6s0 -c CLOCK_REALTIME -w -m
  7. Restart=always
  8. [Install]
  9. WantedBy=multi-user.target

然后启动并设置开机自启:

  1. sudo systemctl daemon-reload
  2. sudo systemctl start ptpphc2sys
  3. sudo systemctl enable ptpphc2sys

对phc2sys也做类似配置,创建对应的服务文件。

从机端(slave)

网卡地址假设为eth0:

配置硬件时钟同步服务(ptp4l)

为了让 PTP 服务在系统启动时自动运行,可以创建 systemd 服务文件:

  1. sudo nano /etc/systemd/system/ptp4l.service

文件内容:

  1. [Unit]
  2. Description=PTP clock synchronization service
  3. After=network.target
  4. [Service]
  5. Type=simple
  6. ExecStart=/usr/sbin/ptp4l -i eth0 -m -s # 根据需要修改为主时钟或从时钟模式
  7. Restart=always
  8. [Install]
  9. WantedBy=multi-user.target

然后启动并设置开机自启:

  1. sudo systemctl daemon-reload
  2. sudo systemctl start ptp4l
  3. sudo systemctl enable ptp4l

配置系统时钟同步服务(phc2sys)

为了让 PTP 服务在系统启动时自动运行,可以创建 systemd 服务文件:

  1. sudo nano /etc/systemd/system/ptpphc2sys.service

文件内容:

  1. [Unit]
  2. Description=PTP phc2sys service
  3. After=network.target
  4. [Service]
  5. Type=simple
  6. ExecStart=/usr/sbin/phc2sys -s eth0 -c CLOCK_REALTIME -w -m
  7. Restart=always
  8. [Install]
  9. WantedBy=multi-user.target

然后启动并设置开机自启:

  1. sudo systemctl daemon-reload
  2. sudo systemctl start ptpphc2sys
  3. sudo systemctl enable ptpphc2sys

对phc2sys也做类似配置,创建对应的服务文件。

验证同步状态

可以使用以下命令检查 PTP 同步状态:

  1. # 查看ptp4l状态
  2. sudo ptp4l -i eth0 -m -t
  3. # 检查时钟偏移
  4. sudo pmc -u -b 0 'GET PORT_DATA_SET'

通过以上步骤,你可以在 Linux 系统上配置 PTP 时钟同步,实现高精度的网络时间同步。根据你的需求,可以将某些节点配置为主时钟,其他节点配置为从时钟,以构建一个精确的时间同步网络。

更多文章可关注公众号
aisoftcloud