在Debian系統中,定時任務通常是通過 systemd 的定時器單元(timer units)來管理的。然而,關于使用定時器直接同步系統時間,實際上并不常見,因為系統時間同步更多地依賴于專門的NTP(網絡時間協議)服務。以下是使用 systemd-timesyncd 和 NTP 服務進行時間同步的方法:
sudo apt update
sudo apt install ntp
sudo systemctl enable ntp
sudo systemctl start ntp
/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
保存文件后,重新加載systemd配置:
sudo systemctl daemon-reload
/etc/systemd/system/ntp-sync.timer:[Unit]
Description=Run ntpdate every 15 minutes
[Timer]
OnCalendar=:0/15
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl enable ntp-sync.timer
sudo systemctl start ntp-sync.timer
sudo systemctl list-timers --all
sudo apt update
sudo apt install ntp
/etc/ntp.conf 文件,添加或修改NTP服務器地址。例如:server 0.debian.pool.ntp.org
server 1.debian.pool.ntp.org
server 2.debian.pool.ntp.org
server 3.debian.pool.ntp.org
sudo systemctl restart ntp
sudo systemctl status ntp
date
通過以上步驟,你可以在Debian系統上配置時間同步,確保系統時間的準確性。