最近一段时间因为项目需要,修改系统时间用以解决历史问题,所以做此总结。
简单的查询系统时间命令
系统时间:
$ date
2018年 10月 23日 星期二 11:51:10 CST
硬件时间:(root权限)
$ hwclock --show
2018年10月23日 星期二 03时52分14秒 -0.521667 秒
修改系统时间
1、需要root权限
2、输入命令timedatectl
Local time: 二 2018-10-23 11:46:10 CST
Universal time: 二 2018-10-23 03:46:10 UTC
RTC time: 二 2018-10-23 03:46:10
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: yes
DST active: n/a
如上所示,NTP项(自动更新时间)显示yes,此时需要关闭自动更新,否则后续修改时间将不成功
3、关闭自动更新
输入命令
timedatectl set-ntp false
可以使用上一条命令查看是否成功。
4、修改时间
date -s '2018-10-19 11:11:11' //时间按照自己需要修改即可
5、同步系统时间到硬件时间
hwclock --systohc或者clock --systohc 即用系统时钟同步硬件时钟
ps:hwclock --hctosys或者clock --hctosys hc代表硬件时间,sys代表系统时间,即用硬件时钟同步系统时钟
6、处理完数据之后恢复系统时间
timedatectl set-ntp true
hwclock --systohc或者clock --systohc
更新虚拟机时间
更新虚拟机为本地时间
$ date -R //我这里显示的时区正确
Tue, 23 Oct 2018 12:07:11 +0800
运行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.
#?
在这里我们选择亚洲 Asia,确认之后选择中国(China),最后选择北京(Beijing)
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.
复制文件到/etc目录下
$ cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
再次查看时间date -R,是否已经修改为北京时间
$ date -R
Tue, 23 Oct 2018 12:13:03 +0800