修改Linux Operating System的时间与时区

                 修改Linux Operating System的时间与时区

                                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  有的小伙伴可能会遇到安装了linux系统后,尽管时区选择正确,也会发现系统时间不对。这是由于安装系统时采用了UTC,那么什么是UTC呢,简单的说UTC就是0时区的时间,是国际标准,而中国处于"UTC +800"时区。所以安装linux系统时,不要选择使用UTC。接下来我们就一起来学习一下如何将错误的时间修改为正确的时间。

一.修改Linux的本地时间

    linux系统时钟有两个,一个是硬件时钟,即BIOS时间,就是我们进行CMOS设置时看到的时间,另一个是系统时钟,是linux系统Kernel时间。当Linux启动时,系统Kernel会去读取硬件时钟的设置,然后系统时钟就会独立于硬件运作。有时我们会发现系统时钟和硬件时钟不一致,因此需要执行时间同步,下面就分享一下时间设置及时钟同步的命令使用方法。

1>.查看系统时间

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# date 
Tue Mar 19 14:48:03 CST 2019
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# date -s "2018-10-10 11:11:11"        #修改系统时间
Wed Oct 10 11:11:11 CST 2018
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# date 
Wed Oct 10 11:11:12 CST 2018
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

2>.查看系统硬件时钟

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hwclock 
Tue 19 Mar 2019 02:49:28 PM CST  -0.393951 seconds
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hwclock --show
Tue 19 Mar 2019 02:49:34 PM CST  -0.954759 seconds
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# clock --show
Tue 19 Mar 2019 02:49:44 PM CST  -0.470718 seconds
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# type -a hwclock 
hwclock is /usr/sbin/hwclock
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /usr/sbin/hwclock 
-rwxr-xr-x. 1 root root 49648 Oct 31 06:48 /usr/sbin/hwclock
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# type -a clock
clock is /usr/sbin/clock
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /usr/sbin/clock
lrwxrwxrwx. 1 root root 7 Mar 11 13:56 /usr/sbin/clock -> hwclock
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
clock与hwclock相同,其实clock底层调用的就是hwclock,详情请戳我!

3>.将系统时间同步到硬件时间中

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hwclock -r              
Tue 19 Mar 2019 03:23:15 PM CST  -0.158685 seconds
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hwclock --set --date="06/18/18 13:55"
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hwclock -r
Tue 19 Mar 2019 03:23:25 PM CST  -0.752022 seconds
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hwclock --set --date="06/18/18 13:55"        #cento7.6 设置硬件时间无效案例!
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# date 
Tue Mar 19 15:25:02 CST 2019
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# date -s "2018-10-10 11:11:11"
Wed Oct 10 11:11:11 CST 2018
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# date 
Wed Oct 10 11:11:13 CST 2018
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hwclock -r
Tue 19 Mar 2019 03:25:16 PM CST  -0.674275 seconds
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hwclock -w              #centos7.6 将系统时间同步到硬件时间无效案例!
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hwclock -r
Tue 19 Mar 2019 03:25:25 PM CST  -0.830223 seconds
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hwclock -w                        #centos7.6 将系统时间同步到硬件时间无效案例! 

二.通过配置文件的方式修改Linux时区

1>.查看Linux当前时区(温馨提示:负号(“-”) 代表西 , 正号(“+”) 代表东, 北京时间是东八区)

[root@yinzhengjie oozie-4.0.0-cdh5.3.6]# date -R
Fri, 07 Sep 2018 08:55:59 -0400
[root@yinzhengjie oozie-4.0.0-cdh5.3.6]# 

2>.查看时区的配置文件所在位置

[root@yinzhengjie oozie-4.0.0-cdh5.3.6]# cd /usr/share/zoneinfo/
[root@yinzhengjie zoneinfo]# ll
total 280
drwxr-xr-x.  2 root root  4096 Aug  9 19:14 Africa
drwxr-xr-x.  6 root root  4096 Aug  9 19:14 America
drwxr-xr-x.  2 root root  4096 Aug  9 19:14 Antarctica
drwxr-xr-x.  2 root root    25 Aug  9 19:14 Arctic
drwxr-xr-x.  2 root root  4096 Aug  9 19:14 Asia
drwxr-xr-x.  2 root root  4096 Aug  9 19:14 Atlantic
drwxr-xr-x.  2 root root  4096 Aug  9 19:14 Australia
drwxr-xr-x.  2 root root    55 Aug  9 19:14 Brazil
drwxr-xr-x.  2 root root  4096 Aug  9 19:14 Canada
-rw-r--r--.  1 root root  2102 Oct  7  2015 CET
drwxr-xr-x.  2 root root    43 Aug  9 19:14 Chile
-rw-r--r--.  1 root root  2294 Oct  7  2015 CST6CDT
-rw-r--r--.  2 root root  2411 Oct  7  2015 Cuba
-rw-r--r--.  1 root root  1876 Oct  7  2015 EET
-rw-r--r--.  2 root root  1946 Oct  7  2015 Egypt
-rw-r--r--.  2 root root  3533 Oct  7  2015 Eire
-rw-r--r--.  1 root root   118 Oct  7  2015 EST
-rw-r--r--.  1 root root  2294 Oct  7  2015 EST5EDT
drwxr-xr-x.  2 root root  4096 Aug  9 19:14 Etc
drwxr-xr-x.  2 root root  4096 Aug  9 19:14 Europe
-rw-r--r--.  7 root root  3661 Oct  7  2015 GB
-rw-r--r--.  7 root root  3661 Oct  7  2015 GB-Eire
-rw-r--r--. 10 root root   118 Oct  7  2015 GMT
-rw-r--r--. 10 root root   118 Oct  7  2015 GMT0
-rw-r--r--. 10 root root   118 Oct  7  2015 GMT-0
-rw-r--r--. 10 root root   118 Oct  7  2015 GMT+0
-rw-r--r--. 10 root root   118 Oct  7  2015 Greenwich
-rw-r--r--.  2 root root  1175 Oct  7  2015 Hongkong
-rw-r--r--.  1 root root   119 Oct  7  2015 HST
-rw-r--r--.  2 root root  1176 Oct  7  2015 Iceland
drwxr-xr-x.  2 root root  4096 Aug  9 19:14 Indian
-rw-r--r--.  2 root root  1638 Oct  7  2015 Iran
-rw-r--r--.  1 root root  4494 May  2  2015 iso3166.tab
-rw-r--r--.  3 root root  8981 Oct  7  2015 Israel
-rw-r--r--.  2 root root   481 Oct  7  2015 Jamaica
-rw-r--r--.  2 root root   333 Oct  7  2015 Japan
-rw-r--r--.  2 root root   211 Oct  7  2015 Kwajalein
-rw-r--r--.  2 root root   641 Oct  7  2015 Libya
-rw-r--r--.  1 root root  2102 Oct  7  2015 MET
drwxr-xr-x.  2 root root    50 Aug  9 19:14 Mexico
-rw-r--r--.  1 root root   118 Oct  7  2015 MST
-rw-r--r--.  1 root root  2294 Oct  7  2015 MST7MDT
-rw-r--r--.  4 root root  2427 Oct  7  2015 Navajo
-rw-r--r--.  4 root root  2434 Oct  7  2015 NZ
-rw-r--r--.  2 root root  2031 Oct  7  2015 NZ-CHAT
drwxr-xr-x.  2 root root  4096 Aug  9 19:14 Pacific
-rw-r--r--.  2 root root  2679 Oct  7  2015 Poland
-rw-r--r--.  2 root root  3439 Oct  7  2015 Portugal
drwxr-xr-x. 18 root root  4096 Aug  9 19:14 posix
-rw-r--r--.  3 root root  3519 Oct  7  2015 posixrules
-rw-r--r--.  5 root root   388 Oct  7  2015 PRC
-rw-r--r--.  1 root root  2294 Oct  7  2015 PST8PDT
drwxr-xr-x. 18 root root  4096 Aug  9 19:14 right
-rw-r--r--.  2 root root   774 Oct  7  2015 ROC
-rw-r--r--.  2 root root   557 Oct  7  2015 ROK
-rw-r--r--.  2 root root   402 Oct  7  2015 Singapore
-rw-r--r--.  3 root root  2721 Oct  7  2015 Turkey
-rw-r--r--.  2 root root   118 Oct  7  2015 UCT
-rw-r--r--.  6 root root   118 Oct  7  2015 Universal
drwxr-xr-x.  2 root root  4096 Aug  9 19:14 US
-rw-r--r--.  6 root root   118 Oct  7  2015 UTC
-rw-r--r--.  1 root root  1873 Oct  7  2015 WET
-rw-r--r--.  2 root root  1502 Oct  7  2015 W-SU
-rw-r--r--.  1 root root 20181 Sep 24  2015 zone.tab
-rw-r--r--.  6 root root   118 Oct  7  2015 Zulu
[root@yinzhengjie zoneinfo]#

3>.删除本地配置文件

[root@yinzhengjie zoneinfo]# rm -rf /etc/localtime 
[root@yinzhengjie zoneinfo]# 

4>.创建一个软连接

[root@yinzhengjie zoneinfo]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@yinzhengjie zoneinfo]# 

5>.再次查看Linux当前时区(时区变成东八区了)

[root@yinzhengjie zoneinfo]# date -R
Fri, 07 Sep 2018 21:00:28 +0800
[root@yinzhengjie zoneinfo]# 

三.通过Linux命令的方式修改Linux时区

[root@node101 ~]# date -R          #我们通过该命令查看到当前到时区不正确!
Thu, 14 Feb 2019 05:07:22 -0500
[root@node101 ~]# 
[root@node101 ~]# timedatectl        #当然,我们可以同这条命令查看比上面到命令现实到信息更加详细。
      Local time: Thu 2019-02-14 05:10:08 EST
  Universal time: Thu 2019-02-14 10:10:08 UTC
        RTC time: Thu 2019-02-14 10:10:08
       Time zone: America/New_York (EST, -0500)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: no
 Last DST change: DST ended at
                  Sun 2018-11-04 01:59:59 EDT
                  Sun 2018-11-04 01:00:00 EST
 Next DST change: DST begins (the clock jumps one hour forward) at
                  Sun 2019-03-10 01:59:59 EST
                  Sun 2019-03-10 03:00:00 EDT
[root@node101 ~]# 
[root@node101 ~]# 
[root@node101 ~]# timedatectl list-timezones |  grep  -E "Asia/S.*"        #查看关于亚洲到区域。
Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk
[root@node101 ~]# 
[root@node101 ~]# timedatectl set-timezone Asia/Shanghai        #我们设置当前到时区为亚洲上海时间
[root@node101 ~]# 
[root@node101 ~]# timedatectl                         #再次查看日期,发现是亚洲时间了
      Local time: Thu 2019-02-14 18:10:24 CST
  Universal time: Thu 2019-02-14 10:10:24 UTC
        RTC time: Thu 2019-02-14 10:10:24
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@node101 ~]# 
[root@node101 ~]# date -R                        #恭喜你,修改时区成功啦~
Thu, 14 Feb 2019 18:12:05 +0800
[root@node101 ~]# 

 

原文地址:https://www.cnblogs.com/yinzhengjie/p/9557555.html