AWS / PHP / Python ちょいメモ

amazon web service , PHP, Python を使ったときのメモ。日本語でググってもわからなかった事を中心に。

Ubuntu 無人セキュリティアップグレード を 適切に設定

無人セキュリティアップグレードコマンド unattended-upgrades 使ってますか?

聞き慣れないかもしれませんが、Ubuntuをデフォルトで使ってる場合、ほぼ100%の方が自動で使ってます。

Server用途で使う場合、想定してないサーバー動作につながるので、確認しておくと良いかと。

確認環境 : Ubuntu 20.04 LTS

unattended-upgrades

apt の Daily Service

Ubuntu の パッケージ管理コマンド apt を導入すると、次の2つの 日次実行サービスが 登録されます。

1つ目は、毎日 apt update かけてくれるサービスで、2つ目は、毎日(特定リポジトリの) apt upgrade かけてくれるサービスです。

  1. apt-daily : Daily apt download activities
    • apt-daily.timer
    • apt-daily.service
  2. apt-daily-upgrade : Daily apt upgrade and clean activities
    • apt-daily-upgrade.timer
    • apt-daily-upgrade.service

いずれも Service に登録されてるは ExecStart=/usr/lib/apt/apt.systemd.daily で、この中から unattended-upgrades が呼び出されていました。


apt.systemd.daily

先程の2サービスから呼び出されて update と install を行ってくれる実体。

動作設定のための変数のデフォルト値などについてもコメントで説明がありました。

unattended-upgrades を実行間隔の設定 APT::Periodic::Unattended-Upgrade の説明は以下。

#!/bin/sh
#set -e
#
# This file understands the following apt configuration variables:
# Values here are the default.
# Create /etc/apt/apt.conf.d/10periodic file to set your preference.
#
#  Dir "/";
#  - RootDir for all configuration files
#
#  Dir::Cache "var/cache/apt/";
#  - Set apt package cache directory
#
...
#  APT::Periodic::Unattended-Upgrade "0";
#  - Run the "unattended-upgrade" security upgrade script 
#    every n-days (0=disabled)
#    Requires the package "unattended-upgrades" and will write
#    a log in /var/log/unattended-upgrades

確認した環境では、次の値が設定されていました。

1 なので、 1日ってことなので、毎日ということですね(停止は 0)。

$ apt-config dump | grep Periodic::Unattended
APT::Periodic::Unattended-Upgrade "1";

自動動作の設定を停止

先程の 毎日動作させる設定は、 /etc/apt/apt.conf.d/ 配下のファイルで設定されていました。

$ cat /etc/apt/apt.conf.d/20auto-upgrades 
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

設定変更は、上記の書き換え。

また、どこかにフォルダ無いのファイル名は数字の順番で評価されるとあったので99myconfig のようなものを作っても大丈夫。

$ cat <<EOF | sudo tee /etc/apt/apt.conf.d/99myconfig
APT::Periodic::Unattended-Upgrade "0";
EOF

$ cat 99myconfig 
APT::Periodic::Unattended-Upgrade "0";

コマンドで、無事設定の反映が確認できました。

$ apt-config dump | grep Periodic::Unattended
APT::Periodic::Unattended-Upgrade "0";

なおこの設定を停止しても、apt-daily-upgrade が停止するわけではないので、そちらは動作を続けます。

日次でキックもされないほうが良い・わかりやすいという方は、systemctlコマンドで disable されると良いかと。

コマンド動作の設定変更

unattended-upgrades コマンド、そのものの動作設定は同じフォルダの 50unattended-upgrades で実施。

$cat 50unattended-upgrades
// Automatically upgrade packages from these (origin:archive) pairs
//
// Note that in Ubuntu security updates may pull in new dependencies
// from non-security sources (e.g. chromium). By allowing the release
// pocket these get automatically pulled in.
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";
};

// Python regular expressions, matching packages to exclude from upgrading
Unattended-Upgrade::Package-Blacklist {
    // The following matches all packages starting with linux-
//  "linux-";

    // Use $ to explicitely define the end of a package name. Without
    // the $, "libc6" would match all of them.
//  "libc6$";
//  "libc6-dev$";
//  "libc6-i686$";

    // Special characters need escaping
//  "libstdc\+\+6$";

    // The following matches packages like xen-system-amd64, xen-utils-4.1,
    // xenstore-utils and libxenstore3.0
//  "(lib)?xen(store)?";

    // For more information about Python regular expressions, see
    // https://docs.python.org/3/howto/regex.html
};

// This option controls whether the development release of Ubuntu will be
// upgraded automatically. Valid values are "true", "false", and "auto".
Unattended-Upgrade::DevRelease "auto";
...

対象リポジトリを security に限定しない方法や、特定パッケージを対象から外す方法等、細かく説明されてました。

ログ

このコマンドは、次のフォルダに専用ログを残してくれます

  • /var/log/unattended-upgrades/
    • unattended-upgrades.log
    • unattended-upgrades-dpkg.log
    • unattended-upgrades-shutdown.log

前述のように APT::Periodic::Unattended-Upgrade "0" とすると、ログの記録もなくなります。


その他

関連

こちらを参考に、動作理解しました。わかりやすい記事、ありがとうございます!

hirose31.hatenablog.jp

公式

help.ubuntu.com


こちらのサイト、最近よく検索ひっかかるなー

www.web-dev-qa-db-ja.com


ふりかえり


パッケージを最新にするときには apt update して apt upgrade との手クセ?が身についてしまってました。

定期的な学習は大事との振り返りの日々です。

SoftwareDesignでも好きな Ubuntu Weekly Topics | gihyo.jp を、ちょいちょい読むのが良いですね。