静看光阴荏苒
不管不顾不问不说也不念

Debian/CentOS配置自动安全更新

如果你的服务器上通过包管理器安装了很多软件,有些时候这些软件可能会被爆出存在漏洞,这个时候大多数的发行版都会立即推送新的软件包到存储库用于修复旧版本的漏洞。

这个时候虽然新的软件包已经存在于存储库了,但是还需要人工去手动升级,往往就是这个地方耽误了很长时间从而造成了安全隐患。

实际上我们可以配置包管理器的自动更新功能,让包管理器自动去安装这些更新,无需人工干预。

下面记录一下apt/yum/dnf包管理器的配置方法。

首先来看apt包管理器的,安装无人值守升级的软件包:

apt -y update
apt -y install unattended-upgrades

设置开机自启:

systemctl enable --now unattended-upgrades

[可选]编辑配置文件:

nano /etc/apt/apt.conf.d/50unattended-upgrades

默认情况下这个配置文件内的内容无需做更改就可以正常工作的。

如果你只打算安装安全类的更新,保证和下面的这个配置一致即可:

Unattended-Upgrade::Origins-Pattern {
...
//      "origin=Debian,codename=${distro_codename}-updates";
//      "origin=Debian,codename=${distro_codename}-proposed-updates";
        "origin=Debian,codename=${distro_codename},label=Debian";
        "origin=Debian,codename=${distro_codename},label=Debian-Security";
...
};

如图所示:

如果你有一些特殊的需求可以参考一下下面的这些配置:

// 删除未使用的内核软件包
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
// 删除未使用的依赖项
Unattended-Upgrade::Remove-New-Unused-Dependencies "true";
// 删除未使用的软件包
Unattended-Upgrade::Remove-Unused-Dependencies "true";
// 启用日志记录
Unattended-Upgrade::SyslogEnable "true";

配置好了之后使用下面的命令生成一个20auto-upgrades的文件:

dpkg-reconfigure -plow unattended-upgrades

你也可以手动创建这个文件:

nano /etc/apt/apt.conf.d/20auto-upgrades

确保里面有如下内容:

// 自动运行apt update
APT::Periodic::Update-Package-Lists "1";
// 自动运行apt upgrade
APT::Periodic::Unattended-Upgrade "1";

最后测试运行:

unattended-upgrade --dry-run

没有问题的话就配置好了。

接下来是CentOS的配置方法,CentOS8自带了yum和dnf两个包管理器,任意选其中一个使用即可。

首先安装dnf-automatic:

yum -y install dnf-automatic

编辑配置文件:

vi /etc/dnf/automatic.conf

将里面的配置改为下面所示的:

// 仅更新安全相关的内容
upgrade_type = security
// 下载更新
download_updates = yes
// 安装更新
apply_updates = yes

最后启动dnf-automatic即可:

systemctl enable --now dnf-automatic.timer
赞(5)
未经允许不得转载:荒岛 » Debian/CentOS配置自动安全更新
分享到: 更多 (0)

评论 10

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    lalalalalalalalalalala! :!:

    hofaeair3年前 (2021-01-30) Google Chrome 87.0.4280.141 Google Chrome 87.0.4280.141 GNU/Linux x64 GNU/Linux x64回复
    • lala,manjaro关机特别慢是不是不正常?关机界面要转个30秒左右。

      hofaeair3年前 (2021-01-30) Google Chrome 88.0.4324.104 Google Chrome 88.0.4324.104 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • Linux装了桌面环境后关机一般是会慢一些,但是你最好能弄点log来看看,这样就凭一个大概的关机时间神仙也不知道是不是真的出问题了。。。

        LALA3年前 (2021-01-31) Google Chrome 88.0.4324.96 Google Chrome 88.0.4324.96 GNU/Linux x64 GNU/Linux x64回复
        • 找到问题了,是因为systemd无法终止某些服务的问题造成的。把终止服务的等待时间更改短一些就好了,我后来改成了一秒,关机超快。

          hofaeair3年前 (2021-02-01) Google Chrome 88.0.4324.104 Google Chrome 88.0.4324.104 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 就是厂商的logo下有一个manjaro的logo的界面

      hofaeair3年前 (2021-01-30) Google Chrome 88.0.4324.104 Google Chrome 88.0.4324.104 Windows 10 x64 Edition Windows 10 x64 Edition回复
  2. #2

    debian,如果想使用默认设置自动更新所有软件,完成《设置开机自启》这一步就算结束了吗?

    小白3年前 (2021-01-31) Google Chrome 88.0.4324.96 Google Chrome 88.0.4324.96 GNU/Linux x64 GNU/Linux x64回复
    • 默认是只更新安全类的更新,要自动更新所有软件需要去掉这行前面的注释:
      “origin=Debian,codename=${distro_codename}-updates”;
      你可以找个旧版本的debian在虚拟机上安装,之后配置完了用这个命令测试:
      unattended-upgrade –dry-run
      可以列出来将要更新的软件有哪些。

      LALA3年前 (2021-01-31) Google Chrome 88.0.4324.96 Google Chrome 88.0.4324.96 GNU/Linux x64 GNU/Linux x64回复
      • 万分感谢!!!

        小白3年前 (2021-02-01) Google Chrome 88.0.4324.96 Google Chrome 88.0.4324.96 GNU/Linux x64 GNU/Linux x64回复
  3. #3

    这个好,收藏了先

    chanat11个月前 (04-12) Google Chrome 112.0.0.0 Google Chrome 112.0.0.0 Android 13 Android 13回复

分享创造快乐

广告合作资源投稿