在 Debian 系统中,你可以使用 systemd 的定时器(timer)功能来实现时间同步。以下是具体步骤:
首先,确保你的系统上安装了 NTP 服务。你可以使用以下命令来安装:
sudo apt update
sudo apt install ntp
安装完成后,启用并启动 NTP 服务:
sudo systemctl enable ntp
sudo systemctl start ntp
接下来,创建一个 systemd 定时器来定期同步时间。你可以使用 timedatectl 命令来设置时间同步。
编辑 /etc/systemd/timesyncd.conf 文件,设置时间同步的频率。例如,每 15 分钟同步一次:
[Time]
NTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
FallbackNTP=ntp.ubuntu.com ntp.apple.com
创建一个新的 systemd 定时器单元文件,例如 /etc/systemd/system/ntp-sync.timer:
[Unit]
Description=Run ntpdate every 15 minutes
[Timer]
OnCalendar=*:0/15
Persistent=true
Unit=ntp.service
[Install]
WantedBy=timers.target
创建一个新的 systemd 服务单元文件,例如 /etc/systemd/system/ntp.service:
[Unit]
Description=Set system time
[Service]
Type=oneshot
ExecStart=/usr/sbin/ntpdate -u 0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
启用并启动刚刚创建的定时器:
sudo systemctl enable ntp-sync.timer
sudo systemctl start ntp-sync.timer
你可以使用以下命令来查看定时器的状态:
sudo systemctl list-timers --all
你应该能看到 ntp-sync.timer 的状态,并且它应该每 15 分钟触发一次。
你可以使用以下命令来检查时间同步的状态:
timedatectl status
这将显示当前系统时间是否已同步。
通过以上步骤,你可以在 Debian 系统中设置一个定时器来定期同步时间。