Files
chill_notes/Linux/CentOS/CentOS7搭建NTP服务器.md
2026-04-21 20:37:55 +08:00

2.4 KiB
Executable File

CentOS7 搭建 NTP 服务器

NTP (Network Time Protocol) 时间同步


简介

NTP 用于同步网络中各个计算机的时间,精度在局域网可达 0.1ms


环境

角色 IP
NTP Server 192.168.0.15
NTP Client 192.168.0.16

NTP 服务器配置

1. 检查并安装 NTP

# 查看是否已安装
rpm -qa | grep ntp

# 安装 NTP
yum install -y ntp

2. 修改配置文件

vim /etc/ntp.conf

注释默认时间服务器,添加:

# 中国 NTP 服务器
server 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst
server 2.cn.pool.ntp.org iburst
server 3.cn.pool.ntp.org iburst

# 允许上层时间服务器修改本机时间
restrict 0.cn.pool.ntp.org nomodify notrap noquery
restrict 1.cn.pool.ntp.org nomodify notrap noquery

3. 启动 NTP 服务

# 启动
systemctl start ntpd

# 开机自启
systemctl enable ntpd

# 查看状态
systemctl status ntpd

4. 查看同步状态

ntpq -p

输出示例:

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*119.28.206.193  100.122.36.196  2 u   128   128   377    19.711   -0.468   5.363

* 表示已成功同步

5. 开放防火墙端口

firewall-cmd --permanent --zone=public --add-port=123/udp
firewall-cmd --reload

NTP 客户端配置

1. 安装 NTP

yum install -y ntp

2. 修改配置文件

vim /etc/ntp.conf

添加 NTP 服务器地址:

# 允许 NTP Server 主动修改本机时间
restrict 192.168.0.15 nomodify notrap noquery

# 时间服务器
server 192.168.0.15

3. 立即同步时间

ntpdate -u 192.168.0.15

4. 启动 NTP 服务

systemctl start ntpd
systemctl enable ntpd

5. 查看同步状态

ntpq -p

常用命令

# 手动同步(客户端)
ntpdate -u 192.168.0.15

# 查看 NTP 服务器列表
ntpq -p

# 查看时间
date

# 查看硬件时间
hwclock

# 同步硬件时间
hwclock --systohc

中国 NTP 服务器

服务器 地址
中国教育网 1.cn.pool.ntp.org
阿里云 ntp.aliyun.com
腾讯云 time1.cloud.tencent.com

参考:CentOS7 NTP 搭建