admin 管理员组

文章数量: 1184232

一、同步时间

运行ntpdate ntp1.aliyun命令同步时间

# 将 `ntpdate ntp1.aliyun` 加入以下文件的底部,实现开机自动同步。
vi /etc/rc.d/rc.local

# 赋予执行权限
chmod +x /etc/rc.d/rc.local

二、解决相差8小时

CST应该是指(China Shanghai Time,东八区时间)
UTC应该是指(Coordinated Universal Time,标准时间)
所以,这2个时间实际上应该相差8个小时。

1、查看时间信息

[root@hub ~]# timedatectl
Warning: Ignoring the TZ variable. Reading the system's time zone setting only.

      Local time: Thu 2022-02-17 18:38:37 CST
  Universal time: Thu 2022-02-17 10:38:37 UTC
        RTC time: Thu 2022-02-17 10:38:37
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@hub ~]# date
Thu Feb 17 10:40:38 CST 2022
[root@hub ~]# date
Thu Feb 17 10:40:40 CST 2022

2、改硬件时间

hwclock --show 查看硬件的时间
hwclock --set --date '2020-05-20 10:10:00' 设置硬件时间
hwclock --hctosys 设置系统时间和硬件时间同步
hwclock -s  让系统时间和硬件时间保持一致

3、改系统时间

再改系统时间,上一步搞定的话,这里也可以不设置

date -s '10:10:00'

4、重设系统时区

(1)方法一

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'Asia/Shanghai' >/etc/timezone
#jenkins时需要使用root用户权限
docker exec -u root -it jenkins /bin/bash

(2)方法二

# 在/etc/profile文件中增加一行
export TZ='CST-8'
# 使文件立即生效
source /etc/profile 或者 . /etc/profile

5、再次查看时间

[root@hub ~]# timedatectl
Warning: Ignoring the TZ variable. Reading the system's time zone setting only.

      Local time: Thu 2022-02-17 10:51:18 CST
  Universal time: Thu 2022-02-17 02:51:18 UTC
        RTC time: Thu 2022-02-17 02:51:18
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

三、vmware 相差8小时

如果linux系统是在vmware虚拟机中,并且上面的设置不生效,则继续按下方的步骤进行设置。

本文标签: 小时 时间 Linux