这篇指南将涵盖从基本概念、客户端配置,到搭建一个企业级内部 NTP 服务器的全过程。

什么是 NTP?
NTP (Network Time Protocol) 是一种用于在计算机之间同步时钟的协议,它能够提供高精度、高可靠性的时间服务,确保网络中的所有设备时钟保持一致。
为什么需要时间同步?
- 系统日志:确保所有服务器日志的时间戳一致,便于故障排查和安全审计。
- 分布式系统:在数据库集群、分布式计算等场景下,精确的时间至关重要,例如防止数据冲突和进行事务排序。
- 安全认证:许多安全协议(如 Kerberos、SSL/TLS)依赖时间戳来验证证书的有效性,时间不同步会导致认证失败。
- 定时任务:确保
cron等定时任务在正确的时间执行。
Linux 客户端配置(最常见场景)
大多数情况下,你的 Linux 机器作为客户端,从一个或多个公共 NTP 服务器同步时间,现代 Linux 发行版通常使用 chrony 或 ntpd 作为 NTP 客户端/服务器。chrony 因其对网络不稳定环境的适应性更强、同步速度更快,已成为许多发行版(如 RHEL/CentOS 8+, Ubuntu 20.04+)的默认选择。
使用 chrony (推荐)
安装 chrony

# 对于 RHEL/CentOS/Rocky Linux sudo yum install chrony # 对于 Debian/Ubuntu sudo apt update sudo apt install chrony
配置文件 /etc/chrony.conf
这个文件是 chrony 的核心配置,打开它进行编辑:
sudo vim /etc/chrony.conf
关键配置项解释:
server: 指定要同步的 NTP 服务器。- 公共 NTP 服务器池:推荐使用公共池,
pool.ntp.org,它会自动分配给你地理位置较近的服务器。 - 官方推荐池:你可以使用
pool.ntp.org,pool.ntp.org等。 - 硬件时钟:
local stratum 10指当没有外部时间源时,使用系统自身的硬件时钟作为时间源,并设置为 Stratum 10,表示这是一个低优先级的时间源。
- 公共 NTP 服务器池:推荐使用公共池,
一个典型的 /etc/chrony.conf 配置:
# 使用公共 NTP 服务器池 pool 2.centos.pool.ntp.org iburst pool 2.debian.pool.ntp.org iburst # 允许从本地网络的其他机器同步时间 # 192.168.1.0/24 代表你的局域网网段,请根据实际情况修改 # allow 192.168.1.0/24 # 硬件时钟,作为后备时间源 local stratum 10 # 其他配置...
iburst: 这是一个重要的选项,当chrony启动时,它会发送一系列快速的时间请求,以便在几秒钟内快速同步时间,而不是等待数分钟。
启动并启用 chrony 服务
# 启动服务 sudo systemctl start chronyd # 设置为开机自启 sudo systemctl enable chronyd # 检查服务状态 sudo systemctl status chronyd
验证时间同步状态
使用 chronyc 命令行工具来查看同步状态。
# 查看时间源信息 chronyc sources -v
你会看到类似下面的输出,^ 表示当前正在使用的时间源, 表示可用的候选源。
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.1.1 2 6 377 17 -2025us[-2240us] +/- 17ms
^- 120.251.180.5 2 6 377 17 +107us[ +107us] +/- 17ms
^+ 120.251.180.6 2 6 377 17 +321us[ +321us] +/- 17ms
# 查看同步统计信息 chronyc tracking
这个命令会显示与时间源的同步偏差、延迟等详细信息。
# 查看系统时间 timedatectl
输出会显示 Local time, Universal time, RTC time 和 Time zone,以及 NTP synchronized: yes,表示已成功同步。
使用 ntpd (传统方式)
虽然 chrony 是趋势,但 ntpd 仍然非常普遍,尤其是在一些旧系统上。
安装 ntpd
# RHEL/CentOS sudo yum install ntp # Debian/Ubuntu sudo apt install ntp
配置文件 /etc/ntp.conf
sudo vim /etc/ntp.conf
关键配置项:
server: 指定 NTP 服务器。restrict: 用于控制对 NTP 服务的访问权限。
一个典型的 /etc/ntp.conf 配置:
# Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst server 3.centos.pool.ntp.org iburst # 允许所有客户端查询时间,但不允许修改 restrict default nomodify notrap nopeer noquery # 允许本地环回接口 restrict 127.0.0.1 restrict ::1 # 允许局域网内的客户端同步时间 # restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
启动并启用 ntpd 服务
sudo systemctl start ntpd sudo systemctl enable ntpd
验证时间同步状态
# 查看 ntp 同步状态 ntpq -p
输出与 chronyc sources -v 类似, 表示当前同步源。
remote refid st t when poll reach delay offset jitter
==============================================================================
*ntp1.aliyun.com .POOL. 16 p - 64 0 0.000 0.000 0.000
+ntp2.aliyun.com .POOL. 16 p - 64 0 0.000 0.000 0.000
...
搭建内部 NTP 服务器
如果你的网络环境有特殊要求(如内网隔离、高精度要求、减少对外部依赖),可以搭建一台内部 NTP 服务器,这台服务器从公共 NTP 源同步,然后为内网其他设备提供服务。
前提条件:你需要一台稳定的服务器,并且这台服务器能够访问外部的公共 NTP 服务器。
步骤 1:配置 NTP 服务器(以 chrony 为例)
安装 chrony
与客户端安装步骤相同。
编辑 /etc/chrony.conf
配置文件需要两处关键修改:
- 上游服务器:指定从哪里同步时间。
- 客户端访问权限:允许内网客户端连接。
sudo vim /etc/chrony.conf
配置示例:
# ------------------------------------------------------------ # 1. 上游时间源配置 (Server Configuration) # ------------------------------------------------------------ # 从公共 NTP 服务器池同步时间 server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst server 3.centos.pool.ntp.org iburst # 或者使用指定的公共 NTP 服务器 # server ntp.aliyun.com iburst # server time.windows.com iburst # ------------------------------------------------------------ # 2. 本地服务配置 (Local Service Configuration) # ------------------------------------------------------------ # 允许其他机器通过本机的 chrony 服务同步时间 # 192.168.1.0/24 替换为你的内网网段 allow 192.168.1.0/24 # 监听所有网络接口,默认监听 127.0.0.1 # bindaddress 0.0.0.0 # 如果是 NTP 服务器,推荐使用硬件时钟作为后备 local stratum 10
启动并启用服务
sudo systemctl restart chronyd sudo systemctl enable chronyd
步骤 2:配置客户端
客户端的配置与第二部分完全相同,唯一需要修改的是 NTP 服务器地址,将 server 指向你的内部 NTP 服务器。
客户端 /etc/chrony.conf 配置示例:
# 将 server 指向你的内部 NTP 服务器 # 192.168.1.100 替换为你的 NTP 服务器 IP 地址 server 192.168.1.100 iburst # 其他配置...
步骤 3:验证服务器状态
在 NTP 服务器上执行,检查是否有客户端连接。
# 查看正在连接的客户端 chronyc clients # 输出示例 # Number of clients: 5 # Name IP Remote Service Leap Refid ST Last Poll Poll Reach LastRx Last sample # ================================================================================================================= # client1.example.com 192.168.1.101 NTP 10 64 377 17 -2025us[-2240us] +/- 17ms # ...
常见问题与最佳实践
-
chronyvsntpd如何选择?- 新系统:优先选择
chrony,它对网络延迟和抖动不敏感,同步速度快,占用资源少。 - 旧系统:如果系统非常老旧,或者你的软件环境对
ntpd有强依赖,继续使用ntpd。
- 新系统:优先选择
-
防火墙设置
-
客户端:通常不需要放行任何端口,只需要
chronyd或ntpd能访问外部的123/udp即可。 -
NTP 服务器:必须放行
123/udp端口,以便客户端能连接。# 对于 firewalld (RHEL/CentOS) sudo firewall-cmd --permanent --add-service=ntp sudo firewall-cmd --reload # 对于 ufw (Ubuntu/Debian) sudo ufw allow 123/udp
-
-
手动调整时间
-
如果需要手动设置时间(在加入网络前),使用
timedatectl。# 设置日期和时间 (YYYY-MM-DD HH:MM:SS) sudo timedatectl set-time "2025-10-27 10:30:00" # 设置时区 sudo timedatectl set-timezone Asia/Shanghai
-
手动设置后,
chronyd或ntpd会自动接管并继续同步。
-
-
硬件时钟
- 定期将系统时间同步到硬件时钟(BIOS/CMOS 时钟),以确保重启后时间准确。
# 同步系统时间到硬件时钟 sudo hwclock --systohc
- 定期将系统时间同步到硬件时钟(BIOS/CMOS 时钟),以确保重启后时间准确。
-
配置多个上游源
- 为了高可用性,建议配置多个上游 NTP 服务器。
chrony和ntpd都会自动从中选择最优的一个。
- 为了高可用性,建议配置多个上游 NTP 服务器。
通过以上步骤,你就可以在 Linux 环境中成功配置和管理 NPT 时间同步了。
