如何在Debian/Ubuntu上设置自动安全更新(无人值守更新)
对于 Linux 管理员来说重要的任务之一是让系统保持最新状态,这可以使得你的系统更加稳健并且可以避免不想要的访问与攻击。
在 Linux 上安装软件包是小菜一碟,用相似的方法我们也可以更新安全补丁。
这是一个向你展示如何配置系统接收自动安全更新的简单教程。当你运行自动安全包更新而不经审查会给你带来一定风险,但是也有一些好处。
如果你不想错过安全补丁,且想要与最新的安全补丁保持同步,那你应该借助无人值守更新机制设置自动安全更新。
如果你不想要自动安全更新的话,你可以在 Debian/Ubuntu 系统上手动安装安全更新。
我们有许多可以自动化更新的办法,然而我们将先采用官方的方法之后我们会介绍其它方法。
如何在 Debian/Ubuntu 上安装无人值守更新包
无人值守更新包默认应该已经装在你的系统上。但万一它没被安装,就用下面的命令来安装。
使用 APT-GET 命令和 APT 命令来安装 unattended-upgrades
软件包。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">apt-get</span><span class="pln"> install unattended</span><span class="pun">-</span><span class="pln">upgrades</span>
下方两个文件可以使你自定义该机制:
<span class="str">/etc/</span><span class="pln">apt</span><span class="pun">/</span><span class="pln">apt</span><span class="pun">.</span><span class="pln">conf</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="lit">50unattended</span><span class="pun">-</span><span class="pln">upgrades</span>
<span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apt</span><span class="pun">/</span><span class="pln">apt</span><span class="pun">.</span><span class="pln">conf</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="lit">20auto</span><span class="pun">-</span><span class="pln">upgrades</span>
在 50unattended-upgrades 文件中做出必要修改
默认情况下只有安全更新需要的最必要的选项被启用。但并不限于此,你可以配置其中的许多选项以使得这个机制更加有用。
我修改了一下文件并仅加上被启用的行以方便阐述:
<span class="com">#</span><span class="pln"> </span><span class="kwd">vi</span><span class="pln"> </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apt</span><span class="pun">/</span><span class="pln">apt</span><span class="pun">.</span><span class="pln">conf</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="lit">50unattended</span><span class="pun">-</span><span class="pln">upgrades</span>
<span class="typ">Unattended</span><span class="pun">-</span><span class="typ">Upgrade</span><span class="pun">::</span><span class="typ">Allowed</span><span class="pun">-</span><span class="typ">Origins</span><span class="pln"> </span><span class="pun">{</span>
<span class="pln"> </span><span class="str">"${distro_id}:${distro_codename}"</span><span class="pun">;</span>
<span class="pln"> </span><span class="str">"${distro_id}:${distro_codename}-security"</span><span class="pun">;</span>
<span class="pln"> </span><span class="str">"${distro_id}ESM:${distro_codename}"</span><span class="pun">;</span>
<span class="pln"> </span><span class="pun">};</span>
<span class="typ">Unattended</span><span class="pun">-</span><span class="typ">Upgrade</span><span class="pun">::</span><span class="typ">DevRelease</span><span class="pln"> </span><span class="str">"false"</span><span class="pun">;</span>
有三个源被启用,细节如下:
${distro_id}:${distro_codename}
:这是必须的,因为安全更新可能会从非安全来源拉取依赖。${distro_id}:${distro_codename}-security
:这用来从来源得到安全更新。${distro_id}ESM:${distro_codename}
:这是用来从 ESM(扩展安全维护)获得安全更新。
启用邮件通知: 如果你想要在每次安全更新后收到邮件通知,那么就修改以下行段(取消其注释并加上你的 email 账号)。
从:
<span class="com">//Unattended-Upgrade::Mail "root";</span>
修改为:
<span class="typ">Unattended</span><span class="pun">-</span><span class="typ">Upgrade</span><span class="pun">::</span><span class="typ">Mail</span><span class="pln"> </span><span class="str">"[email protected]"</span><span class="pun">;</span>
自动移除不用的依赖: 你可能需要在每次更新后运行 sudo apt autoremove
命令来从系统中移除不用的依赖。
我们可以通过修改以下行来自动化这项任务(取消注释并将 false
改成 true
)。
从:
<span class="com">//Unattended-Upgrade::Remove-Unused-Dependencies "false";</span>
修改为:
<span class="typ">Unattended</span><span class="pun">-</span><span class="typ">Upgrade</span><span class="pun">::</span><span class="typ">Remove</span><span class="pun">-</span><span class="typ">Unused</span><span class="pun">-</span><span class="typ">Dependencies</span><span class="pln"> </span><span class="str">"true"</span><span class="pun">;</span>
启用自动重启: 你可能需要在安全更新安装至内核后重启你的系统。你可以在以下行做出修改:
从:
<span class="com">//Unattended-Upgrade::Automatic-Reboot "false";</span>
到:取消注释并将 false
改成 true
以启用自动重启。
<span class="typ">Unattended</span><span class="pun">-</span><span class="typ">Upgrade</span><span class="pun">::</span><span class="typ">Automatic</span><span class="pun">-</span><span class="typ">Reboot</span><span class="pln"> </span><span class="str">"true"</span><span class="pun">;</span>
启用特定时段的自动重启: 如果自动重启已启用,且你想要在特定时段进行重启,那么做出以下修改。
从:
<span class="com">//Unattended-Upgrade::Automatic-Reboot-Time "02:00";</span>
到:取消注释并将时间改成你需要的时间。我将重启设置在早上 5 点。
<span class="typ">Unattended</span><span class="pun">-</span><span class="typ">Upgrade</span><span class="pun">::</span><span class="typ">Automatic</span><span class="pun">-</span><span class="typ">Reboot</span><span class="pun">-</span><span class="typ">Time</span><span class="pln"> </span><span class="str">"05:00"</span><span class="pun">;</span>
如何启用自动化安全更新?
现在我们已经配置好了必须的选项,一旦配置好,打开以下文件并确认是否这两个值都已设置好?值不应为0。(1=启用,0=禁止)。