1.使用“timedatectl status”命令查看服务器时间信息,根据Local time可发现时间也是错的。
master2@master2:~$ date
Thu Apr 7 12:41:38 UTC 2022
master2@master2:~$ timedatectl status
Local time: Thu 2022-04-07 12:41:43 UTC ##本地時間
Universal time: Thu 2022-04-07 12:41:43 UTC ##世界時間
RTC time: Thu 2022-04-07 12:41:44 ##服務器硬件時間
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
master2@master2:~$
这里使用" timedatectl list-timezones"查看可用的時區列表
master2@master2:~$ timedatectl list-timezones
使用"sudo timedatectl set-timezone Asia/Shanghai"命令配置时区,输入密码,配置成功。
master2@master2:~$ sudo timedatectl set-timezone Asia/Shanghai
[sudo] password for master2:
master2@master2:~$ date
Thu Apr 7 21:08:53 CST 2022
master2@master2:~$ timedatectl status
Local time: Thu 2022-04-07 21:09:08 CST
Universal time: Thu 2022-04-07 13:09:08 UTC
RTC time: Thu 2022-04-07 13:09:09
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
master2@master2:~$
二、Failed to set time zone: Invalid or not installed time zone 'Asia/shanghai’解决方案
如下所示,报错说"设置时区失败:时区“亚洲/上海”无效或未安装",
所以我们首先检查 时区本身是否存在,或有拼写错误。
master2@master2:~$ sudo timedatectl set-timezone Asia/shanghai
[sudo] password for master2:
Failed to set time zone: Invalid or not installed time zone 'Asia/shanghai'
如果排除了时区本身的问题,且排查其他因素较为复杂,我们这里使用"tzselect"这个工具跟随向导一步一步设置时区。
root@worker1:~# timedatectl status
Local time: Thu 2022-04-07 13:56:41 UTC
Universal time: Thu 2022-04-07 13:56:41 UTC
RTC time: Thu 2022-04-07 13:56:42
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
root@worker1:~# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa 5) Atlantic Ocean 9) Pacific Ocean
2) Americas 6) Australia 10) coord - I want to use geographical coordinates.
3) Antarctica 7) Europe 11) TZ - I want to specify the timezone using the Posix TZ format.
4) Asia 8) Indian Ocean
#? 4
Please select a country whose clocks agree with yours.
1) Afghanistan 9) Cambodia 17) Iran 25) Kuwait 33) Nepal 41) Singapore 49) Uzbekistan
2) Antarctica 10) China 18) Iraq 26) Kyrgyzstan 34) Oman 42) Sri Lanka 50) Vietnam
3) Armenia 11) Cyprus 19) Israel 27) Laos 35) Pakistan 43) Syria 51) Yemen
4) Azerbaijan 12) East Timor 20) Japan 28) Lebanon 36) Palestine 44) Taiwan
5) Bahrain 13) Georgia 21) Jordan 29) Macau 37) Philippines 45) Tajikistan
6) Bangladesh 14) Hong Kong 22) Kazakhstan 30) Malaysia 38) Qatar 46) Thailand
7) Bhutan 15) India 23) Korea (North) 31) Mongolia 39) Russia 47) Turkmenistan
8) Brunei 16) Indonesia 24) Korea (South) 32) Myanmar (Burma) 40) Saudi Arabia 48) United Arab Emirates
#? 10
Please select one of the following timezones.
1) Beijing Time
2) Xinjiang Time
#? 1
The following information has been given:
China
Beijing Time
Therefore TZ='Asia/Shanghai' will be used.
Selected time is now: Thu Apr 7 21:57:13 CST 2022.
Universal Time is now: Thu Apr 7 13:57:13 UTC 2022.
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
root@worker1:~# date
Thu 07 Apr 2022 09:57:18 PM CST
跟随"tzselect"工具向导配置完成后,还需修改"/etc/profile"配置文件,在配置文件末尾增加 “TZ=‘Asia/Shanghai’; export TZ”
/etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "${PS1-}" ]; then
if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
TZ='Asia/Shanghai'; export TZ ##在配置文件末尾增加 “TZ='Asia/Shanghai'; export TZ”
使文件生效,然后注销或者重启服务器。
root@worker1:~# source /etc/profile
后检查时区和时间,时间OK。
root@worker1:~# date
Thu 07 Apr 2022 09:59:24 PM CST