在 Debian 系统中,你可以使用 systemd
的定时器(timer)功能来设置定时同步。以下是具体步骤:
安装 NTP 服务(如果尚未安装):
sudo apt update
sudo apt install ntp
启用并启动 NTP 服务:
sudo systemctl enable ntp
sudo systemctl start ntp
创建 systemd 定时器单元文件: 你需要创建一个定时器单元文件和一个服务单元文件。
创建服务单元文件 /etc/systemd/system/ntp-sync.service
:
[Unit]
Description=Run NTP sync
[Service]
Type=oneshot
ExecStart=/usr/sbin/ntpdate pool.ntp.org
创建定时器单元文件 /etc/systemd/system/ntp-sync.timer
:
[Unit]
Description=Run NTP sync every hour
[Timer]
OnCalendar=*-*-* *:00:00
Persistent=true
Unit=ntp-sync.service
[Install]
WantedBy=timers.target
这个定时器配置为每小时运行一次 ntpdate
命令。
重新加载 systemd 配置:
sudo systemctl daemon-reload
启动并启用定时器:
sudo systemctl start ntp-sync.timer
sudo systemctl enable ntp-sync.timer
检查定时器状态:
sudo systemctl list-timers --all
你应该能看到 ntp-sync.timer
的状态信息。
通过以上步骤,你就成功设置了 Debian 系统的定时同步功能。你可以根据需要调整 OnCalendar
行中的时间表达式,以适应不同的同步频率。