050-5217-4037 お見積もり

開発者ブログ

2023年5月9日

【Ubuntu】セキュリティアップデートの自動適用

Skrumエンジニアの根岸です。

Ubuntu サーバでセキュリティアップデートを自動適用する方法をご紹介します。

自動アップデートは unattended-upgrades というパッケージで行います。
unattended-upgrades は Ubuntu サーバにデフォルトでインストールされています。

デフォルトの設定では、
自動アップデート:される
自動再起動   :されない

となっています。

アップデートの中には、サーバの再起動が不要なアップデート再起動が必要なアップデートがあります。
よって、再起動が必要なアップデートを適用するためには、下記のどちらかの対応が必要となります。
1. 定期的に手動でサーバを再起動する
2. unattended-upgrades の設定で、再起動が必要なアップデートがあった際に自動で再起動されるようにする

今回は 2 の方法をご紹介します。

unattended-upgrades の設定ファイルは主に下記の2つがあります。
/etc/apt/apt.conf.d/20auto-upgrades
/etc/apt/apt.conf.d/50unattended-upgrades

まずは 20auto-upgrades ファイルを見てみましょう。

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

APT::Periodic::Update-Package-Lists → パッケージリストの自動アップデート
APT::Periodic::Unattended-Upgrade → パッケージの自動アップグレード
の設定です。
デフォルトの状態で、それぞれ “1” となっており、有効化されています。
“0” とすることで無効化できます。
20auto-upgrades ファイルはデフォルトのままで良いのでこのままにします。

次に 50unattended-upgrades ファイルを見てみましょう。
まずはどのアップデートの種類が自動化の対象になっているかを確認します。

Unattended-Upgrade::Allowed-Origins {
        "${distro_id}:${distro_codename}";
        "${distro_id}:${distro_codename}-security";
        // Extended Security Maintenance; doesn't necessarily exist for
        // every release and this system may not have it installed, but if
        // available, the policy for updates is such that unattended-upgrades
        // should also install from here by default.
        "${distro_id}ESMApps:${distro_codename}-apps-security";
        "${distro_id}ESM:${distro_codename}-infra-security";
//      "${distro_id}:${distro_codename}-updates";
//      "${distro_id}:${distro_codename}-proposed";
//      "${distro_id}:${distro_codename}-backports";
};

デフォルトでは上記のようになっており、”-updates” がコメントアウトされており、セキュリティ関連のアップデートのみが対象になっていることがわかります。
セキュリティ関連以外を対象にしてしまうと、サーバ上にデプロイしているアプリケーションの動作に影響が出てしまう可能性があるため、自動アップデートはしないようにした方が良いでしょう。
こちらもデフォルトのままでOKです。

ここまでで、自動セキュリティアップデートがデフォルトで有効化されていることがわかりました。
それでは、自動再起動について確認しましょう。
50unattended-upgrades ファイルの下記部分を確認します。

// Automatically reboot *WITHOUT CONFIRMATION* if
//  the file /var/run/reboot-required is found after the upgrade
//Unattended-Upgrade::Automatic-Reboot "false";

// If automatic reboot is enabled and needed, reboot at the specific
// time instead of immediately
//  Default: "now"
//Unattended-Upgrade::Automatic-Reboot-Time "02:00";

Unattended-Upgrade::Automatic-Reboot → 自動再起動の有効/無効
Unattended-Upgrade::Automatic-Reboot-Time → 自動再起動の時刻設定

です。
デフォルトでは両方ともコメントアウトされていますので、下記のように変更します。

// Automatically reboot *WITHOUT CONFIRMATION* if
//  the file /var/run/reboot-required is found after the upgrade
Unattended-Upgrade::Automatic-Reboot "true";

// If automatic reboot is enabled and needed, reboot at the specific
// time instead of immediately
//  Default: "now"
Unattended-Upgrade::Automatic-Reboot-Time "05:00";

Unattended-Upgrade::Automatic-Reboot は “true” にし、Unattended-Upgrade::Automatic-Reboot-Time には任意の時刻を設定します。
バッチ処理が動いていない適切な時刻を設定するように細心の注意を払いましょう。

最後に unattended-upgrades を再起動すれば全ての設定が完了です。

sudo systemctl reload unattended-upgrades.service
Pagetop