一、NTP 作用说明
NTP(Network Time Protocol) 用于在局域网或互联网中同步服务器时间,保证多台主机的系统时间一致。 在企业中常用于:
- 日志统一时间(方便排查问题)
- 数据库、监控、集群节点时间一致性
- 避免认证、证书过期等问题
二、实验环境说明
角色 | 主机名 | IP 地址 | 系统版本 |
NTP 服务端 | ntp-server | 10.0.0.12 | CentOS 7 |
客户端 | ntp-client | 10.0.0.13 | CentOS 7 |
三、服务端配置步骤(搭建 NTP 服务器)
1️⃣ 安装 NTP 服务
[root@ntp-server ~]# yum install -y ntp
💡 说明: 安装 ntpd 服务端程序,用于提供时间同步服务。
2️⃣ 编辑配置文件
#1.将所有上游服务器全部注释
[root@ntp-server ~]# sed -ri 's/(^server.*)/#\1/g' /etc/ntp.conf
#2.添加新的上游服务器
[root@ntp-server ~]# echo "server ntp.aliyun.com" >> /etc/ntp.conf
#3.允许哪些客户端同步时间(默认只允许本机)
[root@ntp-server ~]# echo "restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap" >> /etc/ntp.conf
查看配置文件
[root@ntp-server ~]# grep -E -v "^#|^$" /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
server ntp.aliyun.com
restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap
💡 说明:
server
:定义上游 NTP 源(公网或上级时间服务器)。iburst
:在初次同步时加快请求速度。restrict
:访问控制,nomodify
表示客户端只能查询时间,不可修改服务器配置。
3️⃣ 启动并设置开机自启
[root@ntp-server ~]# systemctl enable --now ntpd
💡 说明: --now
表示立即启动并设置为开机自启。
4️⃣ 检查同步状态
[root@ntp-server ~]# ntpq -p
💡 说明: 查看当前 NTP 服务器与上游的同步状态,带 *
的行代表正在同步的时间源。
示例输出:
remote refid st t when poll reach delay offset jitter
==============================================================================
*203.107.6.88 100.107.25.114 2 u 65 512 35 22.219 -11.245 5.768
5️⃣ 防火墙放行 NTP 服务端口
[root@ntp-server ~]# firewall-cmd --permanent --add-service=ntp
[root@ntp-server ~]# firewall-cmd --reload
💡 说明: NTP 默认端口为 UDP/123。
四、客户端配置步骤(使用 NTP 服务)
1️⃣ 安装客户端工具
[root@ntp-client ~]# yum install -y ntp
或在 CentOS 8/Rocky 8 使用:
yum install -y chrony
2️⃣ 配置客户端同步服务端时间
方式1(使用 ntpdate 命令临时同步):
#1.测试同步命令
[root@ntp-client ~]# ntpdate 10.0.0.12
#2.关闭ntpd服务
[root@ntp-client ~]# systemctl stop ntpd
[root@ntp-client ~]# systemctl disable ntpd
#3.将同步命令写入到定时任务
[root@ntp-client ~]# echo "*/5 * * * * /usr/sbin/ntpdate 10.0.0.12 &> /dev/null" >> /var/spool/cron/root
💡 说明: 直接与指定的 NTP 服务器同步系统时间。
方式2(长期自动同步 - 修改配置文件): 修改配置文件
#1.将所有上游服务器全部注释
[root@ntp-client ~]# sed -ri 's/(^server.*)/#\1/g' /etc/ntp.conf
#2.添加新的上游服务器
[root@ntp-client ~]# echo "server 10.0.0.12" >> /etc/ntp.conf
开启开机自动启动
[root@ntp-client ~]# systemctl restart ntpd
[root@ntp-client ~]# systemctl enable ntpd
3️⃣ 验证同步结果
ntpd 客户端查看:
[root@ntp-client ~]# ntpdate 10.0.0.12
9 Oct 20:09:08 ntpdate[2345]: adjust time server 10.0.0.12 offset 0.017092 sec
#注意:此命令仅可方式1可执行,如果使用方式2的方式,再执行此命令会报错。详细报错在下面内容
五、常见问题与排查
1️⃣: the NTP socket is in use, exiting
[root@ntp-client ~]# ntpdate 10.0.0.12
9 Oct 17:20:47 ntpdate[1410]: the NTP socket is in use, exiting
#当前系统里已经有 NTP 客户端服务(ntpd 或 chronyd)正在运行,占用了 UDP/123 端口。
#所以 ntpdate 无法再临时启动自己的 socket 去同步时间,就直接退出了。
解决方法:
1.关闭ntpd服务,则可以执行ntpdate 10.0.0.12
2.但是守护进程模式本身就是自动向上游服务器同步时间的,没有必要关闭。所以知道情况即可,不需要执行这个命令。
2️⃣: no server suitable for synchronization
[root@ntp-client ~]# ntpdate 10.0.0.12 9
Oct 17:29:43 ntpdate[1454]: no server suitable for synchronization found
#表示 客户端能访问命令本身,但没能成功从 10.0.0.12 获取到时间信息。
#这是 NTP 服务端通信失败或配置不允许访问 的常见问题。
解决办法:
1.ping 上游服务器,看是否能ping通。
2.检查防火墙和selinux
3.检查配置文件,是否开放了下游服务器网段是使用
4.检查上游服务器是否开启服务,与开放端口
3️⃣: 修改配置文件重启后,没有立马生效
这是正常现象,当重启 ntpd 服务后,它并不会立刻对外提供可用时间,而是会经过一段“自我校准”过程。
这个过程一般需要 1~5 分钟,取决于网络延迟和上游服务器响应速度。