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