温馨提示×

centos怎样修改系统时间戳

小樊
43
2026-08-03 04:40:19
栏目: 智能运维

在 CentOS 系统中修改系统时间戳(日期和时间),常用方法如下,按推荐程度排序,并区分 CentOS 7/8CentOS 6


一、使用 timedatectl推荐,CentOS 7/8

1️⃣ 查看当前时间

timedatectl

2️⃣ 修改日期和时间

sudo timedatectl set-time "2026-01-21 15:30:00"

3️⃣ 关闭 NTP 同步(否则时间会被自动纠正)

sudo timedatectl set-ntp false

4️⃣ 修改时区(如需要)

sudo timedatectl set-timezone Asia/Shanghai

二、使用 date 命令(临时修改)

⚠️ 重启后可能失效(如果 NTP 开启)

1️⃣ 查看当前时间

date

2️⃣ 修改系统时间

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

3️⃣ 同步到硬件时钟(重要)

sudo hwclock --systohc

三、手动修改硬件时间(RTC)

查看硬件时间

hwclock

设置硬件时间

sudo hwclock --set --date="2026-01-21 15:30:00"

用硬件时间同步系统时间

sudo hwclock --hctosys

四、CentOS 6 及更早版本

修改系统时间

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

同步硬件时间

hwclock -w

五、使用 NTP 同步(推荐生产环境)

CentOS 7/8

sudo timedatectl set-ntp true

CentOS 6

ntpdate pool.ntp.org

⚠️ 注意事项

  1. 修改时间可能影响
    • 程序日志
    • 数据库(尤其是 MySQL、Oracle)
    • SSL 证书校验
  2. 生产环境建议
    • 不要随意回拨时间
    • 优先使用 NTP 同步
  3. 若系统安装了 chronyd
    systemctl stop chronyd
    

如果你能说明:

  • CentOS 版本
  • 是云服务器还是物理机
  • 是否需要永久修改

我可以给你更精确的方案 ✅

0