NTP服务配置与管理指南

Linux-DeploymentService-NTPConfig

一、检查并安装NTP服务

1.首先查询NTP软件版本

1
2
3
rpm -qa | grep ntp
ntpdate-4.2.6p5-29.el7.centos.2.x86_64
ntp-4.2.6p5-29.el7.centos.2.x86_64

2. 安装 NTP 软件包:

1
2
3
4
5
6
7
8
sudo yum -y install ntp
已安装:
ntp.x86_64 0:4.2.6p5-29.el7.centos.2

作为依赖被安装:
autogen-libopts.x86_64 0:5.18-5.el7 ntpdate.x86_64 0:4.2.6p5-29.el7.centos.2

完毕!

3. 启动 NTP 服务:

1
sudo systemctl start ntpd

4. 设置 NTP 服务开机自启:

1
2
sudo systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

5. 检查 NTP 服务状态:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
sudo systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2025-04-09 17:25:07 CST; 12s ago
Main PID: 8015 (ntpd)
CGroup: /system.slice/ntpd.service
└─8015 /usr/sbin/ntpd -u ntp:ntp -g

4月 09 17:25:07 localhost.localdomain ntpd[8015]: Listen and drop on 1 v6wildcard :: UDP 123
4月 09 17:25:07 localhost.localdomain ntpd[8015]: Listen normally on 2 lo 127.0.0.1 UDP 123
4月 09 17:25:07 localhost.localdomain ntpd[8015]: Listen normally on 3 ens33 192.168.184.128 UDP 123
4月 09 17:25:07 localhost.localdomain ntpd[8015]: Listen normally on 4 lo ::1 UDP 123
4月 09 17:25:07 localhost.localdomain ntpd[8015]: Listen normally on 5 ens33 fe80::cbd9:ea82:743b:52...123
4月 09 17:25:07 localhost.localdomain ntpd[8015]: Listening on routing socket on fd #22 for interfac...tes
4月 09 17:25:08 localhost.localdomain ntpd[8015]: 0.0.0.0 c016 06 restart
4月 09 17:25:08 localhost.localdomain ntpd[8015]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
4月 09 17:25:08 localhost.localdomain ntpd[8015]: 0.0.0.0 c011 01 freq_not_set
4月 09 17:25:14 localhost.localdomain ntpd[8015]: 0.0.0.0 c614 04 freq_mode
Hint: Some lines were ellipsized, use -l to show in full.

6. 手动同步时间(可选):

如果你想立即同步时间,可以使用以下命令:

1
sudo ntpdate pool.ntp.org

7. 备份配置文件

1
2
3
4
# /etc/ntp.conf 是NTP服务的主要配置文件,用于控制如何同步系统时间。
cp -p /etc/ntp.conf /etc/ntp.conf.bak
# /etc/sysconfig/ntpd 此配置文件主要用来定义NTP守护程序(ntpd)启动时使用的各种选项。
cp -p /etc/sysconfig/ntpd /etc/sysconfig/ntpd.bak

二、编辑/etc/ntp.conf配置文件

1. 注释默认的 server 配置

默认的 NTP 服务器可能是 pool.ntp.org 或一些公共 NTP 池服务器,但这些服务器可能:

  • 响应较慢(受网络延迟影响)。
  • 不稳定(公共服务器可能过载)。
  • 不符合企业内网需求(如需要同步内部时间源)。

调整方式

1
2
3
# 注释掉原有的 server 行,例如:
# server 0.centos.pool.ntp.org iburst
# server 1.centos.pool.ntp.org iburst

2. 加入自定义 NTP 服务器

1
2
echo "server 首要NTP服务器IP地址 prefer" >> /etc/ntp.conf
echo "server 备用NTP服务器IP地址" >> /etc/ntp.conf
  • **prefer**:标记为首选服务器,NTP 会优先同步它(即使延迟稍高)。
  • 备用服务器:在主服务器不可用时自动切换,提高可靠性。
  • 适用场景
    • 企业内部 NTP 服务器(如 192.168.1.10)。
    • 国家授时中心(如 cn.pool.ntp.org)。
    • 云厂商提供的 NTP(如阿里云 ntp.aliyun.com)。

3. 注释掉 restrict default ignore

默认配置可能包含:

1
# restrict default ignore
  • 作用:拒绝所有外部客户端访问本机的 NTP 服务。

  • 为什么要注释掉?

    • 如果这台 NTP 服务器需要为内网其他设备提供时间同步服务,必须允许客户端访问。

    • 替代方案(推荐):

      1
      restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

      仅允许 192.168.1.0/24 网段同步时间,但不允许修改配置(nomodify notrap)。


4. 注释掉本地时钟源(server 127.0.0.1)

默认可能包含:

1
2
# server 127.0.0.1
# fudge 127.0.0.1 stratum 10
  • 作用:当所有外部 NTP 服务器不可用时,回退到本地时钟(但本地硬件时钟通常不准)。
  • 为什么要注释掉?
    • 避免在网络隔离环境下依赖不准确的本地时钟。
    • 如果确实需要本地时钟作为兜底,可以保留,但 stratum 应设为较高值(如 10),表示优先级最低。

5.重启NTP服务器

注意:所有配置修改后都需要重启服务生效:

1
systemctl restart ntpd

三、NTP配置文件详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 指定NTP守护进程用来记录频率偏差的文件位置。
driftfile /var/lib/ntp/drift

# 默认情况下,拒绝所有对NTP服务器状态或时间的查询请求。
restrict default noquery

# IPv4 默认访问控制规则(严格限制所有外部客户端)
# - kod: 发送Kiss-of-Death包以减缓过于频繁的请求。
# - nomodify: 发送Kiss-of-Death包以减缓过于频繁的请求。
# - notrap: 禁用 trap 服务(防攻击)
# - nopeer: 发送Kiss-of-Death包以减缓过于频繁的请求。
# - noquery: 阻止查询NTP服务器的时间或状态。
restrict default kod nomodify notrap nopeer noquery
#restrict规则是叠加生效的,最后出现的规则会覆盖之前的同名规则

# IPv6 默认访问控制规则(同 IPv4,但针对 IPv6 地址)
restrict -6 default kod nomodify notrap nopeer noquery

# 允许本地回环接口(127.0.0.1)完全访问 NTP 服务
restrict 127.0.0.1

# 允许 IPv6 本地回环接口(::1)完全访问 NTP 服务
restrict -6 ::1

# 加载加密认证文件(如 Autokey 或对称密钥)
# 若未使用认证功能,可删除以简化配置
includefile /etc/ntp/crypto/pw

# 指定 NTP 密钥文件路径(用于认证)
keys /etc/ntp/keys

# 配置首选 NTP 服务器(10.252.17.10)
# - prefer: 标记为优先使用的服务器
# - minpoll 4 maxpoll 4: 固定轮询间隔为 16 秒(2^4)
server 10.252.17.10 prefer minpoll 4 maxpoll 4

# 配置备用 NTP 服务器(10.252.17.11)
# 轮询间隔同样固定为 16 秒
server 10.252.17.11 minpoll 4 maxpoll 4

# 禁用 NTP 监控功能(安全加固)
# 防止滥用 monlist 命令导致的 DDoS 攻击
disable monitor

注意:所有配置修改后都需要重启服务生效:

1
systemctl restart ntpd

四、配置step-tickers文件

/etc/ntp/step-tickers 文件在基于NTP进行时间同步的系统中用于指定初次启动时要同步的服务器。

默认情况下,我们配置的NTP服务器不会去时间源那里同步时间,所以必须修改/etc/ntp/step-tickers文件,加入我们的时间源,这样每次启动ntp服务的时候就会自动更新时间。

1. 将首要和备用NTP服务器IP地址写入

1
2
3
4
5
# echo "首要NTP服务器IP地址" >>/etc/ntp/step-tickers
echo "10.252.17.10" > /etc/ntp/step-tickers

# echo "备用NTP服务器IP地址" >>/etc/ntp/step-tickers
echo "10.252.17.11" >> /etc/ntp/step-tickers

2. ntp.conf和step-tickers的区别

2.1 /etc/ntp.conf

  • 作用:NTP 服务的主配置文件,用于定义 NTP 守护进程(ntpd)的所有行为。
  • 内容
    • 指定时间服务器(NTP server)的地址(如 pool.ntp.org 或具体 IP)。
    • 配置访问控制规则(如允许哪些客户端同步时间)。
    • 定义本地时钟(如系统时钟作为备用时间源)。
    • 其他高级参数(如日志、 drift 文件路径等)。

2.2 /etc/ntp/step-tickers

  • 作用:仅用于在 NTP 服务启动时快速初始化系统时钟(通常用于 ntpd 启动前的第一次时间同步)。
  • 使用场景
    • 当系统时间与真实时间偏差较大时(例如相差几分钟以上),ntpd 默认会逐步调整(缓慢校准)。但通过 step-tickers 中列出的服务器,系统可以在启动时立即大步调整时间(一步到位)。
    • 通常由 /etc/init.d/ntpdsystemd 服务脚本在启动 ntpd 前调用 ntpdate 命令读取此文件。
  • 内容
    • 简单列出 NTP 服务器的地址(每行一个)。

2.3 主要区别对比

特性 /etc/ntp.conf /etc/ntp/step-tickers
用途 定义 ntpd 的长期运行行为 仅在启动时用于快速初始化时间
生效阶段 ntpd 运行时持续生效 ntpd 启动前通过 ntpdate 使用
时间同步方式 渐进式校准(默认) 立即大步调整(若时间偏差大)
依赖关系 必需 可选(部分旧系统可能不需要)

五、检查NTP同步情况

ntpq -p

1
2
3
4
5
$ ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*10.252.17.10 10.252.15.2 2 u 15 16 377 3.129 -0.022 0.063
+10.252.17.11 10.252.15.1 2 u 5 16 377 3.092 -0.928 0.027

* 号代理首选NTP服务器

+号代理备选的可用NTP服务器

offset的绝对值越接近0越精准

ntpstat

1
2
3
4
$ ntpstat
synchronised to NTP server (10.252.17.10) at stratum 3
time correct to within 20 ms
polling server every 16 s

一般生产环境要求time correct小于 100ms 即合格