凌峰创科服务平台

CentOS NTP服务器如何正确配置?

NTP (Network Time Protocol) 简介

NTP(网络时间协议)用于在互联网上同步计算机的时间,它可以使计算机的时间与标准时间源(如原子钟)保持高度一致,这对于日志记录、安全认证、任务调度等场景至关重要。

CentOS NTP服务器如何正确配置?-图1
(图片来源网络,侵删)

配置步骤

我们将配置一个名为 ntp.example.com 的 CentOS 7 服务器,使其成为一个 NTP 服务器。

第 1 步:检查当前时间和时区

在配置之前,先检查一下服务器当前的时间和时区设置是否正确。

  1. 检查当前时间

    date

    确保显示的时间大致准确,即使不精确也没关系,因为 NTP 会同步它。

    CentOS NTP服务器如何正确配置?-图2
    (图片来源网络,侵删)
  2. 检查并设置时区 推荐将时区设置为 UTC(协调世界时),这是服务器最常用的时区。

    # 查看当前时区
    timedatectl status
    # 设置时区为 Asia/Shanghai (根据你的地理位置选择)
    # timedatectl set-timezone Asia/Shanghai
    # 或者设置为 UTC
    sudo timedatectl set-timezone UTC
    # 再次确认时区已更改
    timedatectl status

第 2 步:安装 NTP 软件包

使用 yumdnf 包管理器来安装 ntp 软件包。

# 对于 CentOS 7
sudo yum install -y ntp
# 对于 CentOS 8/9
sudo dnf install -y ntp

安装完成后,ntpd 服务默认是未启动的。

第 3 步:配置 NTP 服务器 (/etc/ntp.conf)

这是配置的核心部分,我们需要编辑 /etc/ntp.conf 文件,定义 NTP 服务器从哪里同步时间,以及哪些客户端可以同步到本服务器。

CentOS NTP服务器如何正确配置?-图3
(图片来源网络,侵删)
  1. 备份原始配置文件

    sudo cp /etc/ntp.conf /etc/ntp.conf.bak
  2. 编辑配置文件

    sudo vi /etc/ntp.conf
  3. 修改关键配置项

    打开文件后,进行如下修改:

    A. 注释或删除默认的上游服务器 文件开头有几行 server ... 的配置,它们定义了默认的公共 NTP 源,如果你希望你的服务器从这些源同步,可以保留或修改它们,为了演示,我们将它们注释掉,并添加一些国内或公共的可靠源。

    # 注释掉或删除默认的 server 行
    # server 0.centos.pool.ntp.org
    # server 1.centos.pool.ntp.org
    # server 2.centos.pool.ntp.org
    # server 3.centos.pool.ntp.org iburst
    # 添加一些公共的 NTP 服务器源 (iburst 表示快速同步)
    server 0.cn.pool.ntp.org
    server 1.cn.pool.ntp.org
    server 2.cn.pool.ntp.org
    server 3.cn.pool.ntp.org

    B. 设置允许同步的客户端(非常重要!) 为了安全,默认情况下 ntpd 只会响应来自同一子网的客户端请求,我们需要明确指定哪些网络或 IP 可以同步时间。

    找到以下行:

    #restrict default kod nomodify notrap nopeer noquery
    #restrict -6 default kod nomodify notrap nopeer noquery

    这两行默认拒绝了所有外部客户端的查询和同步请求,你需要根据你的网络环境进行修改。

    场景 1:仅允许局域网同步 假设你的局域网网段是 168.1.0/24,你可以这样配置:

    # 拒绝所有默认的客户端
    restrict default ignore
    # 允许本地环回接口
    restrict 127.0.0.1
    restrict -6 ::1
    # 允许局域网 192.168.1.0/24 网段的所有客户端进行查询和同步
    # nomodify: 客户端不能修改服务器时间
    # notrap: 不提供 trap 服务
    # noquery: 禁止客户端查询服务器状态
    # 你可以根据需要组合这些选项
    restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

    restrict 常用选项:

    • ignore: 完全忽略来自该客户端的包。
    • nomodify: 客户端不能修改服务器的时间。
    • noquery: 客户端不能使用 ntpqntpdc 等工具查询服务器状态。
    • notrap: 不提供 ntpdc 的 trap 服务。
    • nopeer: 不与客户端建立对等关系。
    • kod: 发送 KoD (Kiss of Death) 包给有问题的客户端。

    场景 2:允许所有客户端同步(不推荐用于生产环境) 如果你希望任何人都可同步,可以这样做:

    # 允许所有客户端进行查询和同步
    restrict default nomodify notrap nopeer noquery

    C. 设置本地时钟(可选但推荐) 在配置文件末尾,你可以添加以下几行来提高本地时钟的稳定性。server 127.127.1.0 表示使用服务器自身的硬件时钟作为后备时间源。

    # 使用本地硬件时钟作为后备时间源
    server 127.127.1.0
    fudge 127.127.1.0 stratum 10

    D. 设置 drift 文件 确保 driftfile 路径正确,这个文件用于记录本地时钟与标准时间的频率偏差,ntpd 会根据这个文件来调整本地时钟,使其长期保持准确。

    driftfile /var/lib/ntp/drift

第 4 步:配置防火墙

CentOS 7/8/9 使用 firewalld 作为默认防火墙,NTP 服务使用 udp 端口 123,你需要开放这个端口。

  1. 永久开放 NTP 端口

    sudo firewall-cmd --permanent --add-service=ntp

    或者直接开放端口:

    sudo firewall-cmd --permanent --add-port=123/udp
  2. 重新加载防火墙使配置生效

    sudo firewall-cmd --reload
  3. 验证规则是否添加成功

    sudo firewall-cmd --list-all
    # 你应该能在输出中看到 "ntp" 服务或 "123/udp" 端口

第 5 步:启动并设置开机自启 NTP 服务

启动 ntpd 服务并设置为开机自动启动。

# 启动服务
sudo systemctl start ntpd
# 设置开机自启
sudo systemctl enable ntpd
# 检查服务状态
sudo systemctl status ntpd

如果看到 Active: active (running),说明服务已成功启动。

第 6 步:验证 NTP 服务器状态

配置完成后,等待几分钟让 NTP 服务器与上游源进行同步,然后进行验证。

  1. 使用 ntpq 命令查看同步状态 ntpq 是 NTP 的标准查询工具。peers 命令可以显示与服务器同步的其他 NTP 服务器。

    ntpq -p

    你会看到类似下面的输出:

     remote           refid      st t when poll reach   delay   offset  jitter
    ===============================================================================
    0.cn.pool.ntp .POOL.          16 p    -   64    0    0.000    0.000   0.000
    1.cn.pool.ntp .POOL.          16 p    -   64    0    0.000    0.000   0.000
    2.cn.pool.ntp .POOL.          16 p    -   64    0    0.000    0.000   0.000
    3.cn.pool.ntp .POOL.          16 p    -   64    0    0.000    0.000   0.000
    *pool.ntp.org .POOL.          16 p    -   64    0    0.000    0.000   0.000
    • remote: 对等 NTP 服务器的地址。
    • refid: 该服务器同步的上游源。
    • st: Stratum(层级),层级越小,离主时钟越近。
    • when: 上一次同步到现在经过的秒数。
    • poll: 同步请求的间隔(秒)。
    • reach: 一个八进制数,表示与服务器连通性。
    • delay: 网络延迟。
    • offset: 时间偏移量(毫秒),这个值越小越好。
    • jitter: 时间抖动(毫秒),值越小越稳定。

    offsetjitter 的值变得很小且稳定时,表示同步成功,最前面带 的行表示当前正在同步的主服务器。

  2. 使用 ntptime 命令查看本地时钟状态 这个命令可以显示本地时钟的频率偏差和调整信息。

    ntptime

客户端配置

配置好 NTP 服务器后,局域网内的其他设备(如另一台 Linux、Windows、路由器等)就可以将这台服务器作为时间源了。

Linux 客户端配置

在客户端机器上,将 /etc/ntp.conf/etc/chrony.conf (CentOS 8/9 默认使用 chrony) 中的 server 指向你的 NTP 服务器即可。

使用 chrony (CentOS 8/9 推荐):

# 编辑配置文件
sudo vi /etc/chrony.conf
# 注掉或修改默认 server,添加你的 NTP 服务器
# server 2.centos.pool.ntp.org iburst
server 192.168.1.100 iburst  # 替换为你的 NTP 服务器 IP
# 重启 chrony 服务
sudo systemctl restart chronyd
sudo systemctl enable chronyd

使用 ntpd (CentOS 7):

# 编辑配置文件
sudo vi /etc/ntp.conf
# 注掉或修改默认 server,添加你的 NTP 服务器
# server 1.centos.pool.ntp.org
server 192.168.1.100  # 替换为你的 NTP 服务器 IP
# 重启 ntpd 服务
sudo systemctl restart ntpd
sudo systemctl enable ntpd

Windows 客户端配置

  1. 打开“命令提示符”或“PowerShell”(以管理员身份运行)。
  2. 执行以下命令,将 NTP 服务器地址设置为你的服务器 IP:
    w32tm /config /syncfromflags:manual /manualpeerlist:"192.168.1.100,0x8"
    • 168.1.100 替换为你的 NTP 服务器 IP。
    • 0x8 表示使用 NTP 协议。
  3. 重新启动 Windows 时间服务:
    w32tm /resync
  4. 检查同步状态:
    w32tm /query /status

故障排除

  • 服务无法启动:检查 /var/log/messagesjournalctl -u ntpd 查看错误日志。
  • 客户端无法同步
    • 检查客户端和服务器之间的网络是否连通(ping 命令)。
    • 检查服务器防火墙是否允许 udp/123 端口。
    • 检查 /etc/ntp.conf 中的 restrict 规则是否正确。
    • 检查客户端配置的 NTP 服务器 IP 或域名是否正确。
  • 时间偏移量 (offset) 很大:这很正常,尤其是在刚开始同步时,等待一段时间后,ntpd 会逐渐调整时间,使其趋于稳定。

通过以上步骤,您已经成功地将一台 CentOS 服务器配置为一个功能完善的 NTP 服务器,它不仅可以自身保持精确的时间,还能为整个局域网提供稳定可靠的时间同步服务,在生产环境中,建议使用公共 NTP 池(如 pool.ntp.org)作为上游源,以保证高可用性。

分享:
扫描分享到社交APP
上一篇
下一篇