在 Linux 系统中,保持准确的系统时间对于日志记录、安全认证、定时任务等至关重要。而 NTP(Network Time Protocol,网络时间协议)就是实现这一目标的关键技术。本文将从零开始,详细讲解如何在主流 Linux 发行版中配置 NTP 时间同步服务,即使你是小白也能轻松上手。
NTP 是一种用于同步计算机系统时钟的协议。它通过连接到互联网上的权威时间服务器(如 pool.ntp.org),自动校准本地系统时间,误差通常在毫秒级别以内。
目前,大多数现代 Linux 系统使用以下两种方式之一来管理时间同步:
chrony 是 Red Hat/CentOS/Fedora 和 Ubuntu/Debian 等系统广泛采用的时间同步工具。
在基于 Debian/Ubuntu 的系统中:
sudo apt updatesudo apt install chrony -y 在基于 RHEL/CentOS/Fedora 的系统中:
sudo dnf install chrony -y # Fedora / CentOS Stream# 或sudo yum install chrony -y # CentOS 7 / older sudo systemctl start chronydsudo systemctl enable chronyd 编辑配置文件 /etc/chrony/chrony.conf(Debian/Ubuntu)或 /etc/chrony.conf(RHEL/CentOS):
sudo nano /etc/chrony/chrony.conf 找到以 pool 开头的行,可以替换为国内更稳定的 NTP 服务器,例如:
pool cn.pool.ntp.org iburstpool ntp.aliyun.com iburst 保存后重启服务:
sudo systemctl restart chronyd chronyc sources -v 如果看到星号(*)标记的服务器,说明已成功同步时间。
如果你不需要复杂功能,systemd 自带的 systemd-timesyncd 足够使用。
sudo timedatectl set-ntp true timedatectl status 输出中若显示 “System clock synchronized: yes”,则表示同步成功。
编辑配置文件:
sudo nano /etc/systemd/timesyncd.conf 取消注释并修改 NTP= 行:
[Time]NTP=ntp.aliyun.com cn.pool.ntp.org 然后重启服务:
sudo systemctl restart systemd-timesyncd ntpd 和 chronyd),应只保留一个timedatectl 命令查看整体时间状态通过本教程,你已经掌握了在 Linux 系统中配置 Linux NTP配置 的两种主流方法。无论是选择功能强大的 chrony配置,还是轻量便捷的 systemd-timesyncd,都能有效实现 NTP时间同步,确保系统时间精准可靠。建议生产环境优先使用 chrony,个人设备可选用 systemd-timesyncd。
现在,你的 Linux 系统时间再也不会“走丢”啦!
本文由主机测评网于2025-11-26发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://vpshk.cn/202511807.html