一、Linux时间设置
Linux有两个时间:软件时间、硬件时间
1、软件时间
查看时间
[root@master-Elastic ~]# date
2024年 02月 19日 星期一 15:19:06 CST
修改时区(将软件时区修改为上海时区)
[root@master-Elastic ~]# 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
注:选择 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
注:选择 9 中国
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1
注:选择 1 北京
The following information has been given:
China
Beijing Time
Therefore TZ='Asia/Shanghai' will be used.
Local time is now: Mon Feb 19 15:24:53 CST 2024.
Universal Time is now: Mon Feb 19 07:24:53 UTC 2024.
Is the above information OK?
1) Yes
2) No
#? 1
注:选择 1 Yes
删除文件
rm -rf /etc/localtime
创建软连接
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
安装ntpdate
yum install -y ntpdate
NTPdate简介
NTPdate是一种在Linux和类Unix系统中同步网络时间协议(NTP)服务器时间的命令行程序。它可以让你的系统时钟与互联网上的标准时间服务器保持一致,从而确保系统时间的准确性。
时间同步
ntpdate us.pool.ntp.org
查看时间
[root@master-Elastic ~]# date
2024年 02月 19日 星期一 15:19:06 CST
2、硬件时间
软件时间和硬件时间可能会不同步
查看硬件时间
[root@master-Elastic ~]# hwclock
2024年02月19日 星期一 15时34分46秒 -0.047622 秒
将软件时间写入到硬件时间
sudo hwclock -w
3、防止重启后时间重置
需要用命令将正确的时间写入BIOS,否则下次重启还是会还原
clock -w 写入BIOS
有些系统是hwclock --systohc
hwclock -r显示bios时间