Debian & Ubuntu 自动检查安装更新
服务器与常用桌面环境不同,基本在部署好服务后就很少登录机器,除非有良好习惯才会经常上去检查更新。要想省心解决这个问题,可以设置系统自动检查安装更新。具体方法步骤如下。
激活自动更新程序
部分系统版本自带有 unattended-upgrades
软件包。如果没有,就要手动安装。
# 检查系统是否已安装(没安装则无信息返回) dpkg -l | grep unattended-upgrades # 从包管理器获取安装 apt-get update && apt-get install unattended-upgrades
安装后运行 dpkg-reconfigure -plow unattended-upgrades
命令启动服务配置向导。
┌────────────┤ Configuring unattended-upgrades ├─────────────┐ │ │ │ Applying updates on a frequent basis is an important part │ │ of keeping systems secure. By default, updates need to be │ │ applied manually using package management tools. │ │ Alternatively, you can choose to have this system │ │ automatically download and install important updates. │ │ │ │ Automatically download and install stable updates? │ │ │ │ <Yes> <No> │ │ │ └────────────────────────────────────────────────────────────┘
选择 YES 确认开启。如果提示选择更新模式,直接回车用默认值。之后程序会生成 /etc/apt/apt.conf.d/20auto-upgrades
配置文件激活服务。
通过编辑该配置文件,可以按需设置自动更新运行方式,并添加其它操作。下面为示例配置。
// 每天刷新软件包列表 APT::Periodic::Update-Package-Lists "1"; // 每天下载可用更新文件 APT::Periodic::Download-Upgradeable-Packages "1"; // 每 7 天运行升级软件包 APT::Periodic::Unattended-Upgrade "7"; // 每 21 天清除包缓存文件 APT::Periodic::AutocleanInterval "21";
更多功能参数查看 /etc/cron.daily/apt
文件描述。
配置自动更新选项
如果需要调整更新策略,例如添加或排除软件包更新。在 /etc/apt/apt.conf.d/50unattended-upgrades
文件内设置,里面有详细配置说明。
Unattended-Upgrade::Origins-Pattern
选项控制自动更新软件包类型,默认只升级系统安全更新,适合大部分情况下使用。
// Debian 默认值 "origin=Debian,codename=${distro_codename},label=Debian-Security"; // Ubuntu 默认值 "origin=Ubuntu,archive=${distro_codename}-security";
为掌握自动更新情况,可以添加邮箱地址接收更新报告。别忘了取消注释使之生效。
Unattended-Upgrade::Mail "user@example.com";
安装配置邮件服务
要使上面设置的邮件通知功能生效,需要安装和配置 mailx
服务,详细流程可参考这篇文章。
配置好邮件服务后,用下面命令测试邮件是否可以正常发送。
echo "This is a test mailing" | mail -s "Test mail" user@example.com
测试自动更新服务
完成上面设置后,运行更新命令测试更新服务是否正常。
unattended-upgrade -v -d
如无意外,更新后会收到报告邮件,说明更新了哪些软件包,是否需要重启等信息。
到处,无人值守自动更新服务就设置完成了,以后不用再惦记检查更新这档子事啦。