跳转到内容
搜索 RunEntLinux 文档

仅搜索 RunEntLinux 通用文档,各发行版文档使用各自的搜索入口。

    浏览当前文档目录

    自动安全更新

    及时安装安全更新是保障服务器安全的基本措施。dnf-automatic 是 RHEL/CentOS/Rocky Linux/AlmaLinux 系列中用于自动化更新的官方工具,支持自动下载、自动安装以及邮件通知等功能。

    Terminal window
    sudo dnf install dnf-automatic -y

    验证安装:

    Terminal window
    rpm -qi dnf-automatic

    dnf-automatic 的主配置文件位于 /etc/dnf/automatic.conf。

    Terminal window
    sudo vi /etc/dnf/automatic.conf
    [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@hostname
    email_to = admin@example.com
    stdin_encoding = utf-8
    [base]
    # 调试级别(0-10)
    debuglevel = 1
    # 排除的包(不自动更新的包)
    # exclude = kernel* php*

    这两个选项决定了 dnf-automatic 的行为模式:

    组合行为适用场景
    download_updates=yes + apply_updates=no仅下载更新,不安装需要手动确认后再安装
    download_updates=yes + apply_updates=yes自动下载并安装安全更新全自动处理
    [commands]
    upgrade_type = security
    apply_updates = yes
    [commands]
    upgrade_type = default
    download_updates = yes
    apply_updates = no

    在 [base] 段中添加排除项,避免自动更新可能影响业务的关键软件:

    [base]
    # 排除内核和特定应用
    exclude = kernel* mysql* nginx*

    dnf-automatic 通过 systemd timer 来定期触发。系统提供了多个预定义的 timer。

    Timer说明
    dnf-automatic.timer按 /etc/dnf/automatic.conf 的 apply_updates 执行(默认 False,只检查/下载,不安装)
    dnf-automatic-download.timer仅下载更新
    dnf-automatic-install.timer下载并安装更新
    dnf-automatic-notifyonly.timer仅检查并通知,不下载不安装
    Terminal window
    sudo systemctl enable --now dnf-automatic-install.timer
    Terminal window
    sudo systemctl enable --now dnf-automatic-download.timer
    Terminal window
    sudo systemctl enable --now dnf-automatic-notifyonly.timer
    Terminal window
    sudo systemctl status dnf-automatic-install.timer
    sudo systemctl list-timers --all | grep dnf

    默认的 timer 通常在每天凌晨执行,并带有随机延迟。如需自定义时间:

    Terminal window
    sudo systemctl edit dnf-automatic-install.timer

    添加覆盖配置:

    [Timer]
    # 清除默认设置
    OnCalendar=
    RandomizedDelaySec=0
    # 设置为每天凌晨 3 点执行
    OnCalendar=*-*-* 03:00:00

    验证定时器配置:

    Terminal window
    sudo systemctl daemon-reload
    sudo systemctl list-timers | grep dnf

    确保系统已安装并配置了邮件传输代理(MTA):

    Terminal window
    sudo dnf install postfix mailx -y
    sudo systemctl enable --now postfix

    在 automatic.conf 中配置:

    [emitters]
    emit_via = email
    [email]
    email_from = dnf-automatic@$(hostname)
    email_to = admin@example.com
    email_host = localhost

    如果需要通过外部 SMTP 发送邮件:

    [emitters]
    emit_via = email
    [email]
    email_from = server-updates@example.com
    email_to = admin@example.com
    # 主机名与端口是分开的选项;写成 host:587 连不上
    default = smtp.example.com
    email_port = 587
    email_tls = True

    将更新信息写入 /etc/motd,用户登录时可以看到:

    [emitters]
    emit_via = motd

    可以同时使用多种通知方式:

    [emitters]
    emit_via = email,motd

    dnf-automatic 没有 dry-run。--timer 是给 systemd timer 用的控制参数(按 automatic.conf 真正执行),不是「模拟运行」:

    Terminal window
    # 想先看会做什么:用 dnf 自己的检查/下载命令,它们不改系统
    sudo dnf check-update
    sudo dnf --refresh makecache
    # --timer 的真实含义:按 automatic.conf 执行一次(apply_updates=yes 时就是真装)
    sudo dnf-automatic --timer
    Terminal window
    sudo systemctl start dnf-automatic-install.service
    Terminal window
    sudo journalctl -u dnf-automatic-install.service --no-pager -l
    sudo journalctl -u dnf-automatic-install.timer --no-pager -l
    Terminal window
    # 查看最近的更新历史
    sudo dnf history
    # 查看最近一次更新的详情
    sudo dnf history info last
    1. 安装 dnf-automatic:

      Terminal window
      sudo dnf install dnf-automatic -y
    2. 配置自动安全更新策略:

      Terminal window
      sudo cp /etc/dnf/automatic.conf /etc/dnf/automatic.conf.bak
      sudo tee /etc/dnf/automatic.conf <<'EOF'
      [commands]
      upgrade_type = security
      apply_updates = yes
      random_sleep = 300
      reboot = never
      [emitters]
      emit_via = email,motd
      [email]
      email_from = dnf-automatic@myserver.example.com
      email_to = admin@example.com
      email_host = localhost
      [command_email]
      command_format = "cat"
      email_from = dnf-automatic@myserver.example.com
      email_to = admin@example.com
      stdin_encoding = utf-8
      [base]
      debuglevel = 1
      # 排除内核更新,手动处理
      exclude = kernel*
      EOF
    3. 安装并启动邮件服务(如需邮件通知):

      Terminal window
      sudo dnf install postfix mailx -y
      sudo systemctl enable --now postfix
    4. 启用自动安装定时器:

      Terminal window
      sudo systemctl enable --now dnf-automatic-install.timer
    5. 自定义执行时间为每天凌晨 2 点:

      Terminal window
      sudo mkdir -p /etc/systemd/system/dnf-automatic-install.timer.d
      sudo tee /etc/systemd/system/dnf-automatic-install.timer.d/override.conf <<'EOF'
      [Timer]
      OnCalendar=
      RandomizedDelaySec=0
      OnCalendar=*-*-* 02:00:00
      EOF
      sudo systemctl daemon-reload
    6. 验证配置:

      Terminal window
      # 检查定时器状态
      sudo systemctl status dnf-automatic-install.timer
      sudo systemctl list-timers | grep dnf
      # 手动测试运行
      sudo systemctl start dnf-automatic-install.service
      sudo journalctl -u dnf-automatic-install.service --no-pager -l
    7. 定期检查更新历史:

      Terminal window
      sudo dnf history
      sudo dnf history info last

    通过合理配置 dnf-automatic,可以在保障服务器安全的同时减少运维负担。对于安全更新建议设置自动安装,对于内核和关键业务软件建议排除后手动处理。