通过 NTP(网络时间协议) 同步校准 Linux 服务器时间是最稳定、推荐的方式,能避免手动修改带来的时间漂移问题。主流 Linux 系统常用两种 NTP 工具:chronyd(CentOS 7+、RHEL 7+ 默认)和 ntpd(Ubuntu/Debian、老版本 CentOS),下面分步骤说明。
一、确认当前 NTP 服务状态
先检查系统是否已运行 NTP 服务:
# 检查 chronyd 状态(CentOS 7+)
sudo systemctl status chronyd
# 检查 ntpd 状态(Ubuntu/Debian)
sudo systemctl status ntp
二、使用 chronyd 同步时间(推荐,CentOS 7+ / RHEL 7+)
chronyd 轻量且适合网络不稳定的场景,默认优先同步系统时区对应的时间。
安装 chronyd(若未安装)
sudo yum install -y chrony
修改配置文件,添加国内 NTP 服务器编辑 /etc/chrony.conf,替换默认服务器为国内源(阿里云、中国国家授时中心),速度更快:
sudo vi /etc/chrony.conf
注释原有 server 行,添加以下内容:
server ntp.aliyun.com iburst
server time1.aliyun.com iburst
server time2.aliyun.com iburst
server ntp.ntsc.ac.cn iburst # 中国科学院授时中心
- iburst:启动时快速同步,缩短首次校准时间。
重启 chronyd 服务并设置开机自启
sudo systemctl restart chronyd
sudo systemctl enable chronyd
验证同步结果
# 查看 NTP 服务器同步状态
sudo chronyc sources -v
# 查看时间同步精度
sudo chronyc tracking
输出中 ^* 开头的行表示当前正在同步的 NTP 服务器,说明同步成功。
三、使用 ntpd 同步时间(Ubuntu/Debian/ 老系统)
ntpd 是传统 NTP 服务,适合对时间精度要求高的场景。
安装 ntpd
sudo apt update && sudo apt install -y ntp
修改配置文件,添加国内 NTP 服务器编辑 /etc/ntp.conf,替换默认服务器:
sudo vi /etc/ntp.conf
注释原有 server 行,添加:
server ntp.aliyun.com prefer
server time1.aliyun.com
server time2.aliyun.com
server ntp.ntsc.ac.cn
- prefer:优先使用该服务器同步。
重启 ntpd 服务并设置开机自启
sudo systemctl restart ntp
sudo systemctl enable ntp
验证同步结果
# 查看 NTP 同步状态
ntpq -p
输出列表中会显示已连接的 NTP 服务器及同步延迟。
四、临时同步时间(无 NTP 服务,用 ntpdate)
如果只是临时校准时间,不想安装服务,可使用 ntpdate 工具(需先停止正在运行的 NTP 服务,否则会冲突):
# 停止 chronyd/ntpd 服务
sudo systemctl stop chronyd
# 或
sudo systemctl stop ntp
# 同步国内 NTP 服务器时间
sudo ntpdate ntp.aliyun.com
# 同步后将系统时间写入硬件时钟
sudo hwclock -w
网硕互联帮助中心




评论前必须登录!
注册