- 设置时区为 CST(中国标准时间):
sudo timedatectl set-timezone Asia/ShanghaiAsia/Shanghai 是代表中国标准时间(CST)的时区名称。
- 验证时区设置:
执行以下命令检查当前时区设置是否正确。
timedatectl输出中应该会显示类似于:
Local time: Sat 2024-08-24 14:16:20 CST
Universal time: Sat 2024-08-24 06:16:20 UTC
RTC time: Sat 2024-08-24 06:16:20
Time zone: Asia/Shanghai (CST, +0800)这表明服务器的本地时间已经设置为 CST。
- 确认日期命令的输出:
运行 date 命令确认是否正确显示 CST 时间。
date你应该看到类似于:
Sat Aug 24 14:16:20 CST 2024
这样,你的服务器时间就会以中国标准时间显示了。
现在我时区修改为CST了,但是时间没有改变,再配置一下自动同步吧。
同步系统时间:
sudo apt-get install ntpdate sudo ntpdate time.windows.com启用 systemd-timesyncd:
sudo timedatectl set-ntp true sudo systemctl restart systemd-timesyncd重新启动时间服务:
sudo systemctl restart systemd-timesyncd- 检查硬件时钟(RTC):
在某些情况下,硬件时钟(RTC)可能不同步。你可以使用以下命令同步硬件时钟:
sudo hwclock --systohc再次执行timedatectl命令检查,发现System clock synchronized是no表示 NTP 服务还没有启用或同步。
root@dfsf2318:~# timedatectl
Local time: Sat 2024-08-24 15:26:16 CST
Universal time: Sat 2024-08-24 07:26:16 UTC
RTC time: Sat 2024-08-24 07:26:16
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: n/a
RTC in local TZ: no启用 systemd-timesyncd 服务:
如果你的系统支持 systemd-timesyncd,可以按照以下步骤启用并启动它:sudo timedatectl set-ntp true sudo systemctl restart systemd-timesyncd之后,可以再次使用 timedatectl 查看同步状态:
timedatectl输出中的 System clock synchronized 应该显示为 yes,表示时间已经自动同步。
如果系统上没有安装 systemd-timesyncd 服务解决办法:
- 确认是否安装了 systemd-timesyncd
首先,检查 systemd-timesyncd 是否已安装。可以通过以下命令查看是否存在:
sudo apt-get install systemd-timesyncd
如果未安装,执行安装命令:
sudo apt-get update
sudo apt-get install systemd-timesyncd
- 启用并启动 systemd-timesyncd
如果 systemd-timesyncd 已安装,但服务文件不可用,尝试启用和启动它:
sudo systemctl enable systemd-timesyncd
sudo systemctl start systemd-timesyncd
如果以上命令仍然无法找到服务文件,请使用以下命令检查系统是否有时间同步服务:
sudo systemctl list-units --type=service | grep timesync