Centos时间同步

发布于:2025-03-13 ⋅ 阅读:(11) ⋅ 点赞:(0)

一、安装NTP服务

  1. 安装并配置 NTP 服务
    • 安装 NTP 客户端,执行命令 yum install -y ntp
    • 编辑 NTP 配置文件 /etc/ntp.conf ,添加或修改 NTP 服务器,例如使用国内常用的 NTP 服务器 cn.pool.ntp.org ,在文件中添加一行 server cn.pool.ntp.org 。也可以使用全球 NTP 服务器池 pool.ntp.org,即添加 server pool.ntp.org
    • 重启 NTP 服务,让配置生效,命令为 systemctl restart ntpd 。
    • 可以设置 NTP 服务开机自启,执行 systemctl enable ntpd 。
    • 在 CentOS 系统中,让 NTP 服务开机自启可以按照以下步骤进行:

    • 确保 NTP 服务已安装:执行命令rpm -qa | grep ntp,检查 NTP 是否已经安装。如果没有安装,可以使用yum install -y ntp命令进行安装5。
    • 设置开机自启:使用systemctl命令设置 NTP 服务开机自启,执行systemctl enable ntpd。执行命令后,系统会将 NTP 服务设置为开机自启状态123。
    • 检查自启设置是否成功:执行systemctl is-enabled ntpd命令,如果输出为enabled,则表示 NTP 服务开机自启设置成功1。
    •  

      此外,如果系统中安装了chrony服务,它可能会与ntp服务冲突,导致ntp服务开机自启失败。可以执行systemctl status chronyd查看chrony服务状态,若处于运行状态,执行systemctl stop chronyd停止该服务,并执行systemctl disable chronyd禁止其开机自启7。

    • [root@localhost ~]# systemctl stop chronyd [root@localhost ~]# systemctl disable chronyd Removed symlink /etc/systemd/system/multi-user.target.wants/chronyd.service.

      从你执行的命令和得到的输出结果来看,你已经成功停止并禁用了 chronyd 服务。Removed symlink /etc/systemd/system/multi-user.target.wants/chronyd.service. 这条信息表明系统已经移除了 chronyd 服务在 multi-user.target.wants 目录下的符号链接,也就意味着 chronyd 服务不会在开机时自动启动了。

      接下来你可以继续按照之前的步骤操作,确保 ntpd 服务正常运行并设置为开机自启,以便让系统时间能够通过 NTP 服务器进行同步。

    • 确保 ntpd 服务已启动:
      执行 systemctl start ntpd 命令来启动 ntpd 服务。
    • 检查 ntpd 服务状态:
      使用 systemctl status ntpd 命令查看 ntpd 服务是否成功启动并运行正常。
    • 手动同步时间:
      安装 ntpdate 工具(如果未安装,执行 yum install -y ntpdate),然后使用 ntpdate 时间服务器地址 来手动同步时间,例如 ntpdate cn.pool.ntp.org
    • 如果一切顺利,你的系统时间应该能够正确同步并在后续保持更新了。

    • 检查时间同步状态:
      再次执行 timedatectl 命令,查看 NTP synchronized 状态是否变为 yes,确认时间是否能正常自动更新。
    • 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-03-12 13:21:24 CST; 3s ago Process: 21387 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 21388 (ntpd) Tasks: 1 Memory: 1.4M CGroup: /system.slice/ntpd.service └─21388 /usr/sbin/ntpd -u ntp:ntp -g 3月 12 13:21:24 localhost.nnhh ntpd[21388]: Listen normally on 9 ens33 fe80::9fd:4449:eb46:c7c6 UDP 123 3月 12 13:21:24 localhost.nnhh ntpd[21388]: Listen normally on 10 vethd1f3e94 fe80::9457:1dff:fea7:b6f8 UDP 123 3月 12 13:21:24 localhost.nnhh ntpd[21388]: Listen normally on 11 vethcc55d74 fe80::3493:18ff:fe20:1387 UDP 123 3月 12 13:21:24 localhost.nnhh ntpd[21388]: Listen normally on 12 vethb0d2b1e fe80::3c38:96ff:fe8b:d3f UDP 123 3月 12 13:21:24 localhost.nnhh ntpd[21388]: Listen normally on 13 veth2c0f7ac fe80::d0be:53ff:fe0d:1aac UDP 123 3月 12 13:21:24 localhost.nnhh ntpd[21388]: Listen normally on 14 br-8e7438411b24 fe80::42:efff:fe02:c759 UDP 123 3月 12 13:21:24 localhost.nnhh ntpd[21388]: Listening on routing socket on fd #31 for interface updates 3月 12 13:21:24 localhost.nnhh ntpd[21388]: 0.0.0.0 c016 06 restart 3月 12 13:21:24 localhost.nnhh ntpd[21388]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM 3月 12 13:21:24 localhost.nnhh ntpd[21388]: 0.0.0.0 c011 01 freq_not_set

      从 systemctl status ntpd 的输出结果来看,ntpd 服务已经成功启动并正在运行(Active: active (running))。这是一个好的进展,接下来你可以进一步确认时间是否已经成功同步:

    • 查看 NTP 同步状态
      执行 timedatectl 命令,查看 NTP synchronized 字段的值。如果显示为 yes,说明系统已经成功通过 NTP 服务器同步了时间。如果显示为 no,可以继续执行下一步手动同步时间。

    • 手动同步时间
      安装 ntpdate 工具(如果还未安装,执行 yum install -y ntpdate),然后使用 ntpdate 命令手动同步时间。例如:

    • bash

      ntpdate cn.pool.ntp.org
      

      或者使用其他可用的 NTP 服务器地址,如 pool.ntp.org 等。

    • 再次检查时间和 NTP 同步状态
      手动同步时间后,再次执行 date 命令查看当前系统时间是否正确,以及执行 timedatectl 命令查看 NTP synchronized 字段是否变为 yes

    • 等待自动同步(如果手动同步成功)
      如果手动同步时间成功且 NTP synchronized 变为 yes,那么 ntpd 服务会在后续按照其默认的时间间隔自动与 NTP 服务器进行时间同步。你可以等待一段时间后再次检查时间是否仍然准确。

    • 如果在上述步骤中遇到任何问题,比如手动同步时间失败或 NTP synchronized 始终无法变为 yes,可以继续检查 ntpd 的配置文件(/etc/ntp.conf)中 NTP 服务器的设置是否正确,以及网络连接是否正常等情况。

    • ntp.conf文件配置

    • # For more information about this file, see the man pages
      # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

      driftfile /var/lib/ntp/drift

      # Permit time synchronization with our time source, but do not
      # permit the source to query or modify the service on this system.
      restrict default nomodify notrap nopeer noquery

      # Permit all access over the loopback interface.  This could
      # be tightened as well, but to do so would effect some of
      # the administrative functions.
      restrict 127.0.0.1
      restrict ::1

      # Hosts on local network are less restricted.
      restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

      # Use public servers from the pool.ntp.org project.
      # Please consider joining the pool (http://www.pool.ntp.org/join.html).
      server 0.centos.pool.ntp.org iburst
      server 1.centos.pool.ntp.org iburst
      server 2.centos.pool.ntp.org iburst
      server 3.centos.pool.ntp.org iburst
      server cn.pool.ntp.org iburst
      server time.nist.gov.cn iburst
      #broadcast 192.168.1.255 autokey        # broadcast server
      #broadcastclient                        # broadcast client
      #broadcast 224.0.1.1 autokey            # multicast server

  2. 手动同步时间并更新硬件时钟
    • 若不想使用 NTP 服务,也可以手动同步时间。先安装 ntpdate,执行 yum install -y ntpdate 。
    • 然后使用 ntpdate 时间服务器地址 来同步时间,如 ntpdate time.nist.gov ,若该服务器同步不了,可尝试 0.asia.pool.ntp.org 、1.asia.pool.ntp.org 等。
    • 同步完系统时间后,将系统时间写入硬件时钟,执行 hwclock --systohc 。
  3. 检查虚拟机设置(如果是虚拟机环境)
    • 部分虚拟机软件(如 VMware),有与宿主机时间同步的选项。以 VMware 为例,选中虚拟机,点击 “虚拟机” 菜单,选择 “设置”,在弹出窗口中找到 “选项”,点击 “时间”,勾选 “与主机时间同步” 。
  4. 检查硬件时钟电池(较少见情况):如果是物理机,系统时间不准确可能是硬件时钟(RTC)电池电量不足或损坏导致。打开机箱,找到主板上的纽扣电池,更换为同型号新电池,然后进入 BIOS 设置界面,将时间设置为正确值 。

二、检查时区

1.查看当前时区

执行命令 timedatectl,在输出信息中找到 Timezone 对应的内容,这就是当前系统时区。比如输出中可能有类似 Timezone: Asia/Shanghai 这样的信息,就表示当前时区是上海时区。

设置时区

如果当前时区不对,使用以下命令设置:

  1. 使用 timedatectl 命令
    比如要设置为上海时区,命令是 timedatectl set-timezone Asia/Shanghai 。
  2. 通过复制时区文件设置
    可以直接复制相应的时区文件到 /etc/localtime 来设置时区。例如设置为上海时区,执行命令 cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 。

设置完时区后,可以再次使用 timedatectl 命令确认时区是否设置正确,也可以用 date 命令查看时间显示是否符合预期。

[root@localhost ~]# timedatectl Local time: 三 2025-03-12 13:16:02 CST Universal time: 三 2025-03-12 05:16:02 UTC RTC time: 三 2025-03-12 05:31:21 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: no RTC in local TZ: no DST active: n/a

NTP synchronized(NTP 同步状态)显示为 no,即没有成功通过网络时间协议(NTP)同步时间,这可能是时间无法自动更新的原因。

1. 检查 NTP 服务状态

执行命令 systemctl status ntpd(若系统未安装 NTP,先执行 yum install -y ntp 进行安装),查看 NTP 服务是否已启动且运行正常。如果服务未启动,执行 systemctl start ntpd 启动服务;如果服务状态异常,可以尝试重启 NTP 服务,命令是 systemctl restart ntpd

2. 手动同步 NTP 时间

安装 ntpdate 工具(如果未安装,执行 yum install -y ntpdate),然后使用 ntpdate 时间服务器地址 来手动同步时间。例如:

  • 使用中国国家授时中心的服务器同步时间,命令为 ntpdate time.nist.gov.cn
  • 也可以使用全球 NTP 服务器池,如 ntpdate pool.ntp.org 。

3. 设置 NTP 自动同步

确保 NTP 服务开机自启,执行命令 systemctl enable ntpd,这样系统每次开机时会自动启动 NTP 服务并尝试同步时间。

4. 检查防火墙设置

如果 NTP 服务还是无法同步时间,可能是防火墙阻挡了 NTP 协议的端口(UDP 123 端口)。检查防火墙规则,若使用的是 firewalld,执行 firewall-cmd --list-ports 查看已开放的端口列表,若没有 123/UDP 端口,执行 firewall-cmd --permanent --add-port=123/UDP 开放端口,然后重启防火墙 firewall-cmd --reload

完成上述操作后,等待一段时间,再次执行 timedatectl 命令查看 NTP synchronized 状态是否变为 yes,确认时间是否能正常自动更新。

[root@localhost ~]# systemctl status ntpd ● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled) Active: inactive (dead) [root@localhost ~]#

分享

从输出结果可知,NTP 服务处于未激活(inactive (dead))状态,这就是时间无法通过 NTP 同步的原因。