Linux —— 时间(tzselect、timedatactl命令,查看和修改时区,修改时区为东八区)

2023-05-16

一、设置linux修改时区

首先查看当前时区

#date -R

显示结果为:
[root@localhost 20190528]# date -R

Wed, 29 May 2019 19:56:23 +0100
很显然不是东八区的时间。

方案一:使用tzselect命令实现

1、若不是,准备修改时区

#tzselect

 

2、查看是不是更改为东八区

[root@localhost 20190528]# date -R

Wed, 29 May 2019 19:57:14 +0100
结果非预期所期望的那样……怎么回事???

3、经排查,第一步中选了yes之后,有下面一段话。

此时时间并没有修改,需要执行TZ='Asia/Shanghai'; export TZ,并将这条命令写入.bash_profile文件。

4、执行命令TZ='Asia/Shanghai'; export TZ;

#TZ='Asia/Shanghai'; export TZ

5、再次查看是不是更改为东八区,显示成功

[root@localhost 20190528]# date -R

Wed, 29 May 2019 22:59:13 +0400
 

方案二:使用timedatectl命令

比较新的linux发行版(笔者尝试过ubuntu 16.04和centos7)均支持一个方便的命令timedatectl

1、使用timedatectl命令,查看当前的时区

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

      Local time: 三 2019-05-29 19:17:22 UTC
  Universal time: 三 2019-05-29 19:17:22 UTC
        RTC time: 三 2019-05-29 19:17:22
       Time zone: n/a (UTC, +0000)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

2、使用timedatectl命令,设置时区为东八区

# timedatectl list-timezones  | grep "Asia/S"
Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk
# timedatectl set-timezone "Asia/Shanghai"

3、查看当前的时区状态

# timedatectl status 
Warning: Ignoring the TZ variable. Reading the system's time zone setting only.

      Local time: 三 2019-05-29 23:23:59 SCT
  Universal time: 三 2019-05-29 19:23:59 UTC
        RTC time: 三 2019-05-29 19:23:59
       Time zone: Asia/Shanghai (SCT, +0400)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

4、使用date命令查看,其显示并不是东八区……

我试了好久,,,,都没有成功!!不知道少什么配置文件,哪位大神晓得,欢迎留言指教!

# date -R
Wed, 29 May 2019 16:32:58 -0300

二、设置系统时间为此刻的北京时间

假设时区现在是正确的,此时的正确时间应该是“2019-5-29 23:50:00”;

//查看此时的时区与系统时间
# date -R         
# timedatectl status

方案一、使用date命令:

………………date的使用………………Linux —— 时间(date设置系统时间)

[root@localhost ~]# date -s "2019-05-19 23:51:46"   //设置时间为此刻北京时间
2019年 05月 19日 星期日 23:51:46 SCT
[root@localhost ~]# date                            //查看时间
2019年 05月 19日 星期日 23:51:47 SCT

方案二、查看此时的硬件时间,若硬件时间正确可以同步系统时间与硬件时间相同

……………………hwclock的使用…………Linux —— 时间(hwlock设置硬件时间)

[root@localhost ~]# hwclock -r           //查看此时的硬件时间
2019年05月29日 星期三 23时52分55秒  -0.788625 秒     
[root@localhost ~]# hwclock --hctosys   //使得系统时间变换为硬件时间
[root@localhost ~]# date                //再次查看当前时间
2019年 05月 29日 星期三 23:53:12 SCT

 

参考链接:

https://www.cnblogs.com/flying607/p/9900071.html

https://jingyan.baidu.com/article/39810a235cd638b637fda664.html

https://www.cnblogs.com/jiu0821/p/5999566.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Linux —— 时间(tzselect、timedatactl命令,查看和修改时区,修改时区为东八区) 的相关文章

随机推荐