指南

VPS 上的自动安全更新

最后更新:2026 年 11 月 15 日

服务条款、可接受使用政策、隐私政策和退款政策均为英文;英文文本是具有约束力的版本。

许多入侵利用的是已有修复的已知漏洞。自动安全更新会替您安装这些修复。代价是部分更新(主要是内核)需要重启才能生效。如果您是以 root 身份登录的,请省略 sudo。

Fast VPS 每月 $7.99 起

查看 Fast VPS 套餐下单流程

Debian 和 Ubuntu:unattended-upgrades

sudo apt update
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

选择“Yes”。此后安全更新会自动安装。用试运行(dry run)检查设置:

sudo unattended-upgrade --dry-run --debug | tail -20
ls /var/log/unattended-upgrades/

重启

有些更新要重启后才会生效。查看是否有待处理的重启:

[ -f /var/run/reboot-required ] && echo "reboot needed" || echo "no reboot needed"

如果该文件存在,请在方便的时候重启。若想让服务器在安静的时段自行重启,请编辑 /etc/apt/apt.conf.d/50unattended-upgrades,找到下面这两行,删掉行首的 // 并设置如下(时间以服务器自己的时钟为准,通常是 UTC):

Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";

只有在服务器上运行的内容可以接受在该时间短暂重启时,才这样做;并请确保您的服务在开机后会自行启动(systemctl enable,容器使用 restart: unless-stopped)。

AlmaLinux 和 Rocky:dnf-automatic

sudo dnf install -y dnf-automatic
sudo sed -i 's/^apply_updates = .*/apply_updates = yes/' /etc/dnf/automatic.conf
sudo systemctl enable --now dnf-automatic.timer

用 systemctl list-timers | grep dnf 检查定时器。查看是否有待处理的重启:sudo dnf needs-restarting -r(软件包 yum-utils)。若只安装安全更新,请在同一文件中设置 upgrade_type = security。

需要了解

常见问答

如何在 Ubuntu 或 Debian 上启用自动安全更新?
安装 unattended-upgrades,运行 sudo dpkg-reconfigure -plow unattended-upgrades,并选择 Yes。

自动更新会重启服务器吗?
默认不会。有些更新需要重启才能生效;请检查 /var/run/reboot-required,或者在可以接受短暂重启时设置自动重启时间。

AlmaLinux 或 Rocky 怎么办?
安装 dnf-automatic,在 /etc/dnf/automatic.conf 中设置 apply_updates = yes,并启用 dnf-automatic.timer。

自动更新能代替备份吗?
不能。请保留您自己的备份;更新也不涵盖 Docker 镜像或您自己的应用。

想试一试?Fast VPS 每月 $7.99 起

查看 Fast VPS 套餐下单流程