自动安全更新
及时安装安全更新是保障服务器安全的基本措施。dnf-automatic 是 RHEL/CentOS/Rocky Linux/AlmaLinux 系列中用于自动化更新的官方工具,支持自动下载、自动安装以及邮件通知等功能。
安装 dnf-automatic
Section titled “安装 dnf-automatic”sudo dnf install dnf-automatic -y验证安装:
rpm -qi dnf-automatic配置文件详解
Section titled “配置文件详解”dnf-automatic 的主配置文件位于 /etc/dnf/automatic.conf。
sudo vi /etc/dnf/automatic.conf完整配置文件解析
Section titled “完整配置文件解析”[commands]# 更新类型:# default - 所有可用更新# security - 仅安全更新upgrade_type = security
# 要生成的随机延迟秒数(0 表示不延迟)random_sleep = 0
# 是否在下载后自动安装# yes = 自动下载并安装# no = 仅下载,不安装apply_updates = yes
# 当 apply_updates=no 时,是否仅下载更新download_updates = yes
# 更新完成后的重启策略(dnf-automatic 3.0+)# never - 从不重启# when-changed - 有内核等需要重启的更新时重启# when-needed - 安装需要重启的更新后重启reboot = never
# 重启前等待时间(秒)reboot_command = "shutdown -r +5 'Rebooting after applying updates'"
[emitters]# 通知方式:# stdio - 输出到标准输出(记入日志)# email - 发送邮件# motd - 更新 /etc/motd# command_email - 使用外部命令发送邮件emit_via = email
[email]# 邮件发件人email_from = dnf-automatic@hostname# 邮件收件人email_to = admin@example.com# SMTP 服务器email_host = localhost
[command_email]# 使用外部邮件命令(当 emit_via = command_email 时)command_format = "cat"email_from = dnf-automatic@hostnameemail_to = admin@example.comstdin_encoding = utf-8
[base]# 调试级别(0-10)debuglevel = 1# 排除的包(不自动更新的包)# exclude = kernel* php*配置策略详解
Section titled “配置策略详解”download_updates vs apply_updates
Section titled “download_updates vs apply_updates”这两个选项决定了 dnf-automatic 的行为模式:
| 组合 | 行为 | 适用场景 |
|---|---|---|
download_updates=yes + apply_updates=no | 仅下载更新,不安装 | 需要手动确认后再安装 |
download_updates=yes + apply_updates=yes | 自动下载并安装 | 安全更新全自动处理 |
仅安装安全更新(推荐)
Section titled “仅安装安全更新(推荐)”[commands]upgrade_type = securityapply_updates = yes下载所有更新但不安装
Section titled “下载所有更新但不安装”[commands]upgrade_type = defaultdownload_updates = yesapply_updates = no在 [base] 段中添加排除项,避免自动更新可能影响业务的关键软件:
[base]# 排除内核和特定应用exclude = kernel* mysql* nginx*dnf-automatic 通过 systemd timer 来定期触发。系统提供了多个预定义的 timer。
可用的 Timer 单元
Section titled “可用的 Timer 单元”| Timer | 说明 |
|---|---|
dnf-automatic.timer | 下载并安装更新 |
dnf-automatic-download.timer | 仅下载更新 |
dnf-automatic-install.timer | 下载并安装更新 |
dnf-automatic-notifyonly.timer | 仅检查并通知,不下载不安装 |
启用自动安装定时器
Section titled “启用自动安装定时器”sudo systemctl enable --now dnf-automatic-install.timer启用仅下载定时器
Section titled “启用仅下载定时器”sudo systemctl enable --now dnf-automatic-download.timer启用仅通知定时器
Section titled “启用仅通知定时器”sudo systemctl enable --now dnf-automatic-notifyonly.timer查看定时器状态
Section titled “查看定时器状态”sudo systemctl status dnf-automatic-install.timersudo systemctl list-timers --all | grep dnf自定义执行时间
Section titled “自定义执行时间”默认的 timer 通常在每天凌晨执行,并带有随机延迟。如需自定义时间:
sudo systemctl edit dnf-automatic-install.timer添加覆盖配置:
[Timer]# 清除默认设置OnCalendar=RandomizedDelaySec=0# 设置为每天凌晨 3 点执行OnCalendar=*-*-* 03:00:00验证定时器配置:
sudo systemctl daemon-reloadsudo systemctl list-timers | grep dnf配置邮件通知
Section titled “配置邮件通知”使用本地邮件服务
Section titled “使用本地邮件服务”确保系统已安装并配置了邮件传输代理(MTA):
sudo dnf install postfix mailx -ysudo systemctl enable --now postfix在 automatic.conf 中配置:
[emitters]emit_via = email
[email]email_from = dnf-automatic@$(hostname)email_to = admin@example.comemail_host = localhost使用外部 SMTP 服务器
Section titled “使用外部 SMTP 服务器”如果需要通过外部 SMTP 发送邮件:
[emitters]emit_via = email
[email]email_from = server-updates@example.comemail_to = admin@example.comemail_host = smtp.example.com:587使用 MOTD 通知
Section titled “使用 MOTD 通知”将更新信息写入 /etc/motd,用户登录时可以看到:
[emitters]emit_via = motd多种通知方式组合
Section titled “多种通知方式组合”可以同时使用多种通知方式:
[emitters]emit_via = email,motd不真正执行更新,只查看将要做什么:
sudo dnf-automatic --timer手动触发 Timer
Section titled “手动触发 Timer”sudo systemctl start dnf-automatic-install.service查看执行日志
Section titled “查看执行日志”sudo journalctl -u dnf-automatic-install.service --no-pager -lsudo journalctl -u dnf-automatic-install.timer --no-pager -l查看 dnf 历史
Section titled “查看 dnf 历史”# 查看最近的更新历史sudo dnf history
# 查看最近一次更新的详情sudo dnf history info last完整实战:部署自动安全更新
Section titled “完整实战:部署自动安全更新”-
安装 dnf-automatic:
Terminal window sudo dnf install dnf-automatic -y -
配置自动安全更新策略:
Terminal window sudo cp /etc/dnf/automatic.conf /etc/dnf/automatic.conf.baksudo tee /etc/dnf/automatic.conf <<'EOF'[commands]upgrade_type = securityapply_updates = yesrandom_sleep = 300reboot = never[emitters]emit_via = email,motd[email]email_from = dnf-automatic@myserver.example.comemail_to = admin@example.comemail_host = localhost[command_email]command_format = "cat"email_from = dnf-automatic@myserver.example.comemail_to = admin@example.comstdin_encoding = utf-8[base]debuglevel = 1# 排除内核更新,手动处理exclude = kernel*EOF -
安装并启动邮件服务(如需邮件通知):
Terminal window sudo dnf install postfix mailx -ysudo systemctl enable --now postfix -
启用自动安装定时器:
Terminal window sudo systemctl enable --now dnf-automatic-install.timer -
自定义执行时间为每天凌晨 2 点:
Terminal window sudo mkdir -p /etc/systemd/system/dnf-automatic-install.timer.dsudo tee /etc/systemd/system/dnf-automatic-install.timer.d/override.conf <<'EOF'[Timer]OnCalendar=RandomizedDelaySec=0OnCalendar=*-*-* 02:00:00EOFsudo systemctl daemon-reload -
验证配置:
Terminal window # 检查定时器状态sudo systemctl status dnf-automatic-install.timersudo systemctl list-timers | grep dnf# 手动测试运行sudo systemctl start dnf-automatic-install.servicesudo journalctl -u dnf-automatic-install.service --no-pager -l -
定期检查更新历史:
Terminal window sudo dnf historysudo dnf history info last
通过合理配置 dnf-automatic,可以在保障服务器安全的同时减少运维负担。对于安全更新建议设置自动安装,对于内核和关键业务软件建议排除后手动处理。