温馨提示×

centos如何校准时间戳

小樊
34
2026-07-08 01:06:38
栏目: 智能运维

在 CentOS 系统中校准时间戳(系统时间)可以通过 NTP(网络时间协议)手动设置 来完成。以下是常用方法:


一、使用 NTP 自动校准(推荐)

1. 安装 NTP 服务

CentOS 7/8 默认可能未安装 NTP:

yum install -y ntp

2. 启动并启用 NTP 服务

systemctl start ntpd
systemctl enable ntpd

3. 手动同步时间

ntpdate pool.ntp.org

4. 查看同步状态

ntpq -p

二、使用 chrony(CentOS 7/8 推荐)

CentOS 7 以后默认使用 chrony 替代 ntpd

1. 安装 chrony

yum install -y chrony

2. 启动并启用

systemctl start chronyd
systemctl enable chronyd

3. 手动同步

chronyc -a makestep

4. 查看时间源

chronyc sources

三、使用 timedatectl(systemd 系统)

1. 查看当前时间状态

timedatectl

2. 开启 NTP 同步

timedatectl set-ntp true

3. 设置时区(如 Asia/Shanghai)

timedatectl set-timezone Asia/Shanghai

四、手动设置时间(不推荐长期使用)

1. 设置日期和时间

date -s "2026-01-21 15:30:00"

2. 写入硬件时钟

hwclock --systohc

五、同步硬件时间(BIOS 时间)

hwclock --systohc   # 系统时间 → 硬件时间
hwclock --hctosys   # 硬件时间 → 系统时间

六、常见问题排查

1. 时间总是偏差

  • 检查是否有 NTP 服务在运行
  • 检查虚拟机是否启用了时间同步(如 VMware Tools)

2. 时区错误

timedatectl list-timezones | grep Shanghai

推荐方案(总结)

场景 推荐方式
CentOS 7/8 chrony
老系统 ntp
临时调整 timedatectl / date

如果你能告诉我 CentOS 版本(6/7/8)物理机/虚拟机,我可以给你更精确的方案。

0