修改Linux Operating System的时间与时区

时间:2023-03-09 13:39:21
修改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 :: CST
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# date -s "2018-10-10 11:11:11"        #修改系统时间
Wed Oct :: CST
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# date
Wed Oct :: CST
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

2>.查看系统硬件时钟

[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# hwclock
Tue Mar :: PM CST -0.393951 seconds
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# hwclock --show
Tue Mar :: PM CST -0.954759 seconds
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# clock --show
Tue Mar :: 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. root root Oct : /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. root root Mar : /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. (Core)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# hwclock -r
Tue Mar :: 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 Mar :: 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. (Core)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# date
Tue Mar :: CST
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# date -s "2018-10-10 11:11:11"
Wed Oct :: CST
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# date
Wed Oct :: CST
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# hwclock -r
Tue Mar :: PM CST -0.674275 seconds
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# hwclock -w              #centos7. 将系统时间同步到硬件时间无效案例!
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# hwclock -r
Tue Mar :: 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.-cdh5.3.6]# date -R
Fri, Sep :: -
[root@yinzhengjie oozie-4.0.-cdh5.3.6]#

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

[root@yinzhengjie oozie-4.0.-cdh5.3.6]# cd /usr/share/zoneinfo/
[root@yinzhengjie zoneinfo]# ll
total
drwxr-xr-x. root root Aug : Africa
drwxr-xr-x. root root Aug : America
drwxr-xr-x. root root Aug : Antarctica
drwxr-xr-x. root root Aug : Arctic
drwxr-xr-x. root root Aug : Asia
drwxr-xr-x. root root Aug : Atlantic
drwxr-xr-x. root root Aug : Australia
drwxr-xr-x. root root Aug : Brazil
drwxr-xr-x. root root Aug : Canada
-rw-r--r--. root root Oct CET
drwxr-xr-x. root root Aug : Chile
-rw-r--r--. root root Oct CST6CDT
-rw-r--r--. root root Oct Cuba
-rw-r--r--. root root Oct EET
-rw-r--r--. root root Oct Egypt
-rw-r--r--. root root Oct Eire
-rw-r--r--. root root Oct EST
-rw-r--r--. root root Oct EST5EDT
drwxr-xr-x. root root Aug : Etc
drwxr-xr-x. root root Aug : Europe
-rw-r--r--. root root Oct GB
-rw-r--r--. root root Oct GB-Eire
-rw-r--r--. root root Oct GMT
-rw-r--r--. root root Oct GMT0
-rw-r--r--. root root Oct GMT-
-rw-r--r--. root root Oct GMT+
-rw-r--r--. root root Oct Greenwich
-rw-r--r--. root root Oct Hongkong
-rw-r--r--. root root Oct HST
-rw-r--r--. root root Oct Iceland
drwxr-xr-x. root root Aug : Indian
-rw-r--r--. root root Oct Iran
-rw-r--r--. root root May iso3166.tab
-rw-r--r--. root root Oct Israel
-rw-r--r--. root root Oct Jamaica
-rw-r--r--. root root Oct Japan
-rw-r--r--. root root Oct Kwajalein
-rw-r--r--. root root Oct Libya
-rw-r--r--. root root Oct MET
drwxr-xr-x. root root Aug : Mexico
-rw-r--r--. root root Oct MST
-rw-r--r--. root root Oct MST7MDT
-rw-r--r--. root root Oct Navajo
-rw-r--r--. root root Oct NZ
-rw-r--r--. root root Oct NZ-CHAT
drwxr-xr-x. root root Aug : Pacific
-rw-r--r--. root root Oct Poland
-rw-r--r--. root root Oct Portugal
drwxr-xr-x. root root Aug : posix
-rw-r--r--. root root Oct posixrules
-rw-r--r--. root root Oct PRC
-rw-r--r--. root root Oct PST8PDT
drwxr-xr-x. root root Aug : right
-rw-r--r--. root root Oct ROC
-rw-r--r--. root root Oct ROK
-rw-r--r--. root root Oct Singapore
-rw-r--r--. root root Oct Turkey
-rw-r--r--. root root Oct UCT
-rw-r--r--. root root Oct Universal
drwxr-xr-x. root root Aug : US
-rw-r--r--. root root Oct UTC
-rw-r--r--. root root Oct WET
-rw-r--r--. root root Oct W-SU
-rw-r--r--. root root Sep zone.tab
-rw-r--r--. root root Oct 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, Sep :: +
[root@yinzhengjie zoneinfo]#

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

[root@node101 ~]# date -R          #我们通过该命令查看到当前到时区不正确!
Thu, Feb :: -
[root@node101 ~]#
[root@node101 ~]# timedatectl        #当然,我们可以同这条命令查看比上面到命令现实到信息更加详细。
Local time: Thu -- :: EST
Universal time: Thu -- :: UTC
RTC time: Thu -- ::
Time zone: America/New_York (EST, -)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: no
Last DST change: DST ended at
Sun -- :: EDT
Sun -- :: EST
Next DST change: DST begins (the clock jumps one hour forward) at
Sun -- :: EST
Sun -- :: 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 -- :: CST
Universal time: Thu -- :: UTC
RTC time: Thu -- ::
Time zone: Asia/Shanghai (CST, +)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@node101 ~]#
[root@node101 ~]# date -R                        #恭喜你,修改时区成功啦~
Thu, Feb :: +
[root@node101 ~]#