debian

Debian 定时器如何设置定时同步

小樊
46
2025-10-04 06:20:08
栏目: 智能运维

在 Debian 系统中,你可以使用 systemd 的定时器(timer)功能来设置定时同步。以下是具体步骤:

  1. 安装 NTP 服务(如果尚未安装):

    sudo apt update
    sudo apt install ntp
    
  2. 启用并启动 NTP 服务

    sudo systemctl enable ntp
    sudo systemctl start ntp
    
  3. 创建 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 命令。

  4. 重新加载 systemd 配置

    sudo systemctl daemon-reload
    
  5. 启动并启用定时器

    sudo systemctl start ntp-sync.timer
    sudo systemctl enable ntp-sync.timer
    
  6. 检查定时器状态

    sudo systemctl list-timers --all
    

    你应该能看到 ntp-sync.timer 的状态信息。

通过以上步骤,你就成功设置了 Debian 系统的定时同步功能。你可以根据需要调整 OnCalendar 行中的时间表达式,以适应不同的同步频率。

0
看了该问题的人还看了