在您的 Debian/Ubuntu VPS 上设置时间
本指南说明如何在 Debian/Ubuntu VPS 上正确配置时区和时间同步。
HostMyServers 拥有自己的 NTP 服务器(ntp.infra.hostmyservers.fr),强烈建议配置它以同步 VPS 时间。
先决条件
- 对 VPS 的 SSH 访问权限
- 管理权限(sudo)
方法 1 — systemd-timesyncd(默认)
systemd-timesyncd 是内置于 systemd 的轻量级 NTP 客户端,在大多数 Debian/Ubuntu 发行版上默认启用。
-
通过 SSH 连接到您的 VPS。
-
打开配置文件:
sudo nano /etc/systemd/timesyncd.conf -
将
#NTP=行修改为:NTP=ntp.infra.hostmyservers.fr -
保存文件:
- 按
Ctrl + X - 然后按
O确认 - 按
Enter
- 按
-
重启服务:
sudo systemctl restart systemd-timesyncd -
验证同步是否处于活动状态:
timedatectl status
方法 2 — Chrony
Chrony 是 systemd-timesyncd 更为强大的替代方案,特别适用于虚拟化环境(VPS、容器)。它提供更精确的同步和更好的时钟漂移处理。
安装
sudo apt update && sudo apt install chrony -y
note
安装 chrony 会自动禁用 systemd-timesyncd 以避免冲突。
配置
-
打开配置文件:
sudo nano /etc/chrony/chrony.conf -
找到 现有的
pool或server行,将其替换(或添加为第一行)为:server ntp.infra.hostmyservers.fr iburst您的配置应如下所示:
server ntp.infra.hostmyservers.fr iburst# 保留文件中的其他选项(driftfile、makestep、rtcsync…) -
保存文件(
Ctrl + X,然后O,然后Enter)。 -
重启服务:
sudo systemctl restart chronysudo systemctl enable chrony
验证
显示同步状态:
chronyc tracking
列出正在使用的 NTP 源:
chronyc sources -v
chronyc sources 输出中的 * 列表示当前选择用于同步的源。
通用验证
无论使用哪种方法,均可通过以下命令检查系统时间和同步状态:
timedatectl status
故障排除
如果遇到困难,请确保:
- 正在使用的 NTP 服务(
chrony或systemd-timesyncd)处于活动状态:systemctl status chrony或systemctl status systemd-timesyncd - 两个服务中只有一个在同时运行(它们互不兼容)
- 您的 VPS 可以访问 NTP 服务器(
ntp.infra.hostmyservers.fr) - 您有足够的权限来修改配置