Centos7.4永久修改系统时间

[root@V3B01-zsy yum.repos.d]# date -s "2019-09-24 17:02:30"
2019年 09月 24日 星期二 17:02:30 CST
[root@V3B01-zsy yum.repos.d]# hwclock --show
2019年09月25日 星期三 01时01分42秒 -0.689057 秒
[root@V3B01-zsy yum.repos.d]# hwclock --set --date "2019-09-24 17:03:30"
[root@V3B01-zsy yum.repos.d]# 
[root@V3B01-zsy yum.repos.d]# hwclock --show
2019年09月24日 星期二 17时03分35秒 -0.750970 秒
[root@V3B01-zsy yum.repos.d]# hwclock --hctosys
[root@V3B01-zsy yum.repos.d]# clock -w
[root@V3B01-zsy yum.repos.d]# 
[root@V3B01-zsy yum.repos.d]# hwclock --show
2019年09月24日 星期二 17时04分02秒 -0.813435 秒
[root@V3B01-zsy yum.repos.d]# 
[root@V3B01-zsy yum.repos.d]# 
[root@V3B01-zsy yum.repos.d]# timedatectl 
Local time: 二 2019-09-24 17:05:16 CST
Universal time: 二 2019-09-24 09:05:16 UTC
RTC time: 二 2019-09-24 17:03:51
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@V3B01-zsy yum.repos.d]# timedatectl set-local-rtc 1
[root@V3B01-zsy yum.repos.d]# 
[root@V3B01-zsy yum.repos.d]# 
[root@V3B01-zsy yum.repos.d]# timedatectl 
Local time: 二 2019-09-24 17:06:03 CST
Universal time: 二 2019-09-24 09:06:03 UTC
RTC time: 二 2019-09-24 17:06:03
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: yes
DST active: n/a

Warning: The system is configured to read the RTC time in the local time zone.
This mode can not be fully supported. It will create various problems
with time zone changes and daylight saving time adjustments. The RTC
time is never updated, it relies on external facilities to maintain it.
If at all possible, use RTC in UTC by calling
'timedatectl set-local-rtc 0'.
[root@V3B01-zsy yum.repos.d]# 
[root@V3B01-zsy yum.repos.d]# timedatectl set-timezone Asia/Shanghai
[root@V3B01-zsy yum.repos.d]# 
[root@V3B01-zsy yum.repos.d]# hwclock --show
2019年09月24日 星期二 17时07分11秒 -0.407534 秒
[root@V3B01-zsy yum.repos.d]# 
[root@V3B01-zsy yum.repos.d]# init 6

Last login: Wed Sep 25 00:57:21 2019 from 172.16.52.201
[root@V3B01-zsy ~]# 
[root@V3B01-zsy ~]# 
[root@V3B01-zsy ~]# date
2019年 09月 24日 星期二 17:13:09 CST
[root@V3B01-zsy ~]#

 

参考以下

安装在虚拟机上的CentOS7的时间分为系统时间和硬件时间。二者都修改,重启系统(init 6 )才会永久生效。
修改步骤如下
       1.查看当前系统时间 date
       2.修改当前系统时间 date -s "2018-2-22 19:10:30
       3.查看硬件时间 hwclock --show
       4.修改硬件时间 hwclock --set --date "2018-2-22 19:10:30"
       5.同步系统时间和硬件时间 hwclock --hctosys
       6.保存时钟 clock -w
       7.重启系统(init 6)后便发现系统时间被修改了
时区的修改
yum -y install ntp [root@localhost kevin]# systemctl enable ntpd
systemctl start ntpd

ntpdate us.pool.ntp.org

Linux 系统(我特指发行版, 没说内核) 下大部分软件的风格就是不会仔细去考虑向后 的兼容性, 比如你上个版本能用这种程序配置, 没准到了下一个版本, 该程序已经不见了. 比如 sysvinit 这种东西. 

设置时区同样, 在 CentOS 7 中, 引入了一个叫 timedatectl 的设置设置程序. 

用法很简单: 

# timedatectl # 查看系统时间方面的各种状态
      Local time: 四 2014-12-25 10:52:10 CST
  Universal time: 四 2014-12-25 02:52:10 UTC
        RTC time: 四 2014-12-25 02:52:10
        Timezone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
 
# timedatectl list-timezones # 列出所有时区
 
# timedatectl set-local-rtc 1 # 将硬件时钟调整为与本地时钟一致, 0 为设置为 UTC 时间
 
# timedatectl set-timezone Asia/Shanghai # 设置系统时区为上海
 
其实不考虑各个发行版的差异化, 从更底层出发的话, 修改时间时区比想象中要简单:

# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

 

posted @ 2022-06-09 11:41  傻白甜++  阅读(142)  评论(0编辑  收藏  举报
TOP