温馨提示×

centos怎样校对时间戳

小樊
58
2026-09-29 22:12:03
栏目: 智能运维

在 CentOS 上“校对时间戳”通常指同步系统时间(时钟),让系统时间与标准时间一致。下面按常见场景说明。


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

1. 安装 chrony

yum install -y chrony

2. 启动并设置开机自启

systemctl start chronyd
systemctl enable chronyd

3. 手动立即同步时间

chronyc -a makestep

4. 查看时间同步状态

chronyc tracking

5. 配置文件(可选)

编辑:

vi /etc/chrony.conf

常见 NTP 服务器:

server ntp.aliyun.com iburst
server time1.cloud.tencent.com iburst

修改后重启:

systemctl restart chronyd

二、使用 ntpd(传统方式)

1. 安装 ntp

yum install -y ntp

2. 启动 ntp

systemctl start ntpd
systemctl enable ntpd

3. 手动同步

ntpdate ntp.aliyun.com

若提示 ntpdate: the NTP socket is in use,先停 ntpd:

systemctl stop ntpd
ntpdate ntp.aliyun.com
systemctl start ntpd

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

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

写入硬件时钟:

hwclock --systohc

四、查看当前时间戳

date
date +%s      # 秒级时间戳
date +%s%3N   # 毫秒级

五、时间时区问题(常被误认为“时间不对”)

查看时区:

timedatectl

修改时区(如上海):

timedatectl set-timezone Asia/Shanghai

六、常见问题排查

  • 虚拟机时间漂移 → 用 chrony + 宿主机时间同步
  • 防火墙阻挡 NTP(123/UDP) → 放行或改用 HTTP 时间源
  • 系统时钟快/慢 → chronyc makestep 强制校正

如果你能说明是 CentOS 几、物理机/虚拟机、是否离线环境,我可以给你更精确的方案。

0 踩