问题

多个 RHEL(Red Hat Enterprise Linux)服务器的系统时钟未同步,可能导致依赖时间同步的服务(如 Kerberos、SELinux 或分布式应用程序)运行异常。

故障排除步骤

  1. 检查 NTP 服务状态
    首先,确认 NTP 服务是否正在运行。运行以下命令:

    bash systemctl status ntpd

    如果服务未运行,输出将显示服务处于“inactive”或“failed”状态。此时,需要启动 NTP 服务:

    bash systemctl start ntpd

  2. 确保已安装 NTP 守护进程
    如果系统中未安装 NTP 服务,需要安装它。运行以下命令安装 NTP:

    bash yum install ntp

    安装完成后,启动并启用 NTP 服务,使其在开机时自动启动:

    bash systemctl start ntpd systemctl enable ntpd

  3. 查看 NTP 配置文件
    检查 NTP 配置文件 /etc/ntp.conf,确保配置正确。运行以下命令查看配置文件:

    bash cat /etc/ntp.conf

    关注以下配置项:

    • server:指定 NTP 服务器的地址。通常配置为公共 NTP 服务器(如 0.centos.pool.ntp.org)或内部时间服务器。
    • restrict:限制客户端对 NTP 服务器的访问权限。
    • driftfile:记录时钟漂移的文件路径。

    示例配置:

    plaintext server 0.centos.pool.ntp.org server 1.centos.pool.ntp.org server 2.centos.pool.ntp.org restrict default kod nomodify notrap nopeer noquery restrict -6 default kod nomodify notrap nopeer noquery driftfile /var/lib/ntp/drift

  4. 验证 NTP 服务器的连通性
    确保服务器能够成功连接到配置的 NTP 服务器。运行以下命令测试连接:

    bash ntpdate -q <ntp-server>

    例如:

    bash ntpdate -q 0.centos.pool.ntp.org

    如果连接失败,可能是网络问题或 NTP 服务器不可用。检查网络配置和防火墙规则,确保 NTP 流量(UDP 端口 123)未被阻止。

  5. 重启 NTP 服务
    在确认配置无误且 NTP 服务器可访问后,重启 NTP 服务以应用更改:

    bash systemctl restart ntpd

  6. 验证时间同步状态
    使用以下命令验证时间同步状态:

    bash ntpq -p

    此命令会显示当前 NTP 客户端的状态和同步的服务器信息。输出示例:

    remote refid st t when poll reach delay offset jitter ============================================================================== *0.centos.pool.ntp. .POOL. 16 p - 64 0 0.000 0.000 0.000 1.centos.pool.ntp. .POOL. 16 p - 64 0 0.000 0.000 0.000 2.centos.pool.ntp. .POOL. 16 p - 64 0 0.000 0.000 0.000 +192.168.1.100 .GPS. 1 u 12 64 37 1.234 0.567 0.123

    • * 表示当前同步的服务器。
    • + 表示候选服务器。
    • offset 表示时间偏差,应接近于 0。
  7. 检查系统时间是否已同步
    使用以下命令检查系统时间:

    bash date

    或:

    bash timedatectl

    确保所有服务器的时间一致。

  8. 考虑使用 chrony 替代 ntpd(可选)
    chrony 是一个更现代的时间同步工具,比 ntpd 提供更好的精度和更快的同步速度。如果需要,可以安装并配置 chrony:

    bash yum install chrony systemctl enable --now chronyd systemctl disable --now ntpd

    编辑 /etc/chrony.conf 文件,配置时间服务器,然后重启服务:

    bash systemctl restart chronyd

注意事项

  • 如果服务器位于防火墙后面,确保防火墙允许 UDP 端口 123 的流量。
  • 如果使用内部时间服务器,确保其配置正确且与外部时间源同步。
  • 定期检查 NTP 服务的状态和配置,以确保时间同步的持续性。
  • 如果服务器数量较多,可以考虑设置一个内部 NTP 服务器,减少对外部服务器的依赖。

通过以上步骤,您应该能够解决多个 RHEL 服务器时间不同步的问题。如果问题仍未解决,请提供更多的配置信息和日志内容以便进一步分析。