B005 设置和修改时间与时区

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

 

时间设置

一、date 查看/设置系统时间

日期时间设置:

date -s “2016-4-18 15:14:23”

date -s 2012-08-02

date -s 10:08:00

查看当前时区:

# date +"%Z %z"

CST +0800

 

二、hwclock/clock 查看/设置硬件时间

 

命令参数:

-r, --show 读取并打印硬件时钟(read hardware clock and print result )

-s, --hctosys 将硬件时钟同步到系统时钟(set the system time from the hardware clock )

-w, --systohc 将系统时钟同步到硬件时钟(set the hardware clock to the current system time )

 

--adjust  hwclock每次更改硬件时钟时,都会记录在/etc/adjtime文件中。使用--adjust参数,可使hwclock根据先前的记录来估算硬件时钟的偏差,并用来校正目前的硬件时钟。

--debug  显示hwclock执行时详细的信息。

--directisa  hwclock预设从/dev/rtc设备来存取硬件时钟。若无法存取时,可用此参数直接以I/O指令来存取硬件时钟。

--hctosys  将系统时钟调整为与目前的硬件时钟一致。

--set --date=<日期与时间>  设定硬件时钟。

--show  显示硬件时钟的时间与日期。

--systohc  将硬件时钟调整为与目前的系统时钟一致。

--test  仅测试程序,而不会实际更改硬件时钟。

--utc  若要使用格林威治时间,请加入此参数,hwclock会执行转换的工作。

--version  显示版本信息。

 

1、查看系统硬件时钟

[root@linux-node ~]# hwclock --show 或者

[root@linux-node ~]# clock --show

 

2、设置硬件时间

[root@linux-node ~]# hwclock --set --date="11/03/17 14:55" (月/日/年时:分:秒) 或者

[root@linux-node ~]# clock --set --date="11/03/17 14:55" (月/日/年时:分:秒)

 

三、同步系统及硬件时钟

 

[root@linux-node ~]# hwclock --hctosys 或者

[root@linux-node ~]# clock --hctosys

备注:hc代表硬件时间,sys代表系统时间,以硬件时间为基准,系统时间找硬件时间同步

 

[root@linux-node ~]# hwclock --systohc或者

[root@linux-node ~]# clock --systohc

备注:以系统时间为基准,硬件时间找系统时间同步

 

时区设置

时区设置用tzselect 命令来实现。但是通过tzselect命令设置TZ这个环境变量来选择的时区,需要将变量添加到.profile文件中。

 

一、tzselect命令执行

 

执行tzselect命令 --> 选择Asia(5) --> 选择China(9) --> 选择Beijing Time(1)-->然后输入Yes(1)。

 

[root@test01 ~]# tzselect

Please identify a location so that time zone rules can be set correctly.

Please select a continent or ocean.

1) Africa

2) Americas

3) Antarctica

4) Arctic Ocean

5) Asia

6) Atlantic Ocean

7) Australia

8) Europe

9) Indian Ocean

10) Pacific Ocean

11) none - I want to specify the time zone using the Posix TZ format.

#? 5

Please select a country.

1) Afghanistan 18) Israel 35) Palestine

2) Armenia 19) Japan 36) Philippines

3) Azerbaijan 20) Jordan 37) Qatar

4) Bahrain 21) Kazakhstan 38) Russia

5) Bangladesh 22) Korea (North) 39) Saudi Arabia

6) Bhutan 23) Korea (South) 40) Singapore

7) Brunei 24) Kuwait 41) Sri Lanka

8) Cambodia 25) Kyrgyzstan 42) Syria

9) China 26) Laos 43) Taiwan

10) Cyprus 27) Lebanon 44) Tajikistan

11) East Timor 28) Macau 45) Thailand

12) Georgia 29) Malaysia 46) Turkmenistan

13) Hong Kong 30) Mongolia 47) United Arab Emirates

14) India 31) Myanmar (Burma) 48) Uzbekistan

15) Indonesia 32) Nepal 49) Vietnam

16) Iran 33) Oman 50) Yemen

17) Iraq 34) Pakistan

#? 9

Please select one of the following time zone regions.

1) Beijing Time

2) Xinjiang Time

#? 1

The following information has been given:

China

Beijing Time

Therefore TZ='Asia/Shanghai' will be used.

Local time is now: Thu Mar 14 15:43:25 CST 2019.

Universal Time is now: Thu Mar 14 07:43:25 UTC 2019.

Is the above information OK?

1) Yes

2) No

#? 1

You can make this change permanent for yourself by appending the line

TZ='Asia/Shanghai'; export TZ

to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you

can use the /usr/bin/tzselect command in shell scripts:

Asia/Shanghai

 

执行完tzselect命令选择时区后,时区并没有更改,只是在命令最后提示你可以执行 TZ='Asia/Shanghai'; export TZ 并将这行命令添加到.profile中,然后退出并重新登录。

 

[root@test01 ~]# TZ='Asia/Shanghai'; export TZ

[root@test01 ~]# date

Thu Mar 14 15:45:06 CST 2019

 

二、修改配置文件来修改时区

 

[root@test01 ~]# echo "ZONE=Asia/Shanghai" >> /etc/sysconfig/clock

[root@test01 ~]# cat /etc/sysconfig/clock

ZONE=Asia/Shanghai

[root@test01 ~]# rm -f /etc/localtime

#链接到上海时区文件

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

执行完上述过程后,重启机器,即可看到时区已经更改。

 

备注:

在centos7中设置时区的命令可以通过 timedatectl 命令来实现

[root@linux-node ~]# timedatectl set-timezone Asia/Shanghai

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值