Debian 9 (Stretch) - セキュリティ・アップデートの自動化!

Updated:


Debian GNU/Linux 9 (Stretch) のセキュリティ・アップデートを自動化する方法についての記録です。

以前古いバージョンでの作業時に残していた記録を参考に作業を行い、今回更新した作業記録を貼付する形式の内容となっています。
(当然ながら、興味がなければスルーしてください)

0. 前提条件

  • Debian GNU/Linux 9 (Stretch) サーバでの作業を想定。
  • cron-apt でもパッケージアップデートの自動化は可能であるが、今回はセキュリティ・アップデートのみなので unattended-upgrades を使用する。
    (ちなみに、インストール済みの各種パッケージを自動アップデートするのは(依存パッケージの整合性等の問題があるため)危険であり、当方はセキュリティ・アップデート以外は自動でアップデートしない方針)
  • root ユーザでの作業を想定。

1. unattended-upgrades のインストール

unattended-upgrades はデフォルトでインストールされているはずだが、インストールされていなければインストールする。
また、パッケージ変更履歴ツール apt-listchanges もインストールする。

# apt -y install unattended-upgrades apt-listchanges

2. “50unattended-upgrades” の編集

メール送信先を編集(コメント解除)する。(当然、ユーザ名のみならず外部のメールアドレスも設定可)

File: /etc/apt/apt.conf.d/50unattended-upgrades

1
Unattended-Upgrade::Mail "root";

3. “20auto-upgrades” の作成

以下のコマンドを実行して自動アップデートに関する質問に「はい」と回答すると、自動アップグレードのための設定ファイル “20auto-upgrades” が作成される。

# dpkg-reconfigure -plow unattended-upgrades

もしくは、以下のような内容で “20auto-upgrades” 作成してもよい。
(”/etc/cron.daily/apt” に記述があるとおりデフォルトでは無効になっているので、有効にするための記述)

File: /etc/apt/apt.conf.d/20auto-upgrades

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

4. “02periodic” の作成

cron ジョブのための設定ファイルを作成する。
“/etc/cron.daily/apt” に記述があるとおりデフォルトで設定されている項目もあるが、以下のように「作成例」をそのまま流用している。
(各設定項目の説明はコメントに記述されているとおりなので、あらためて説明しない)

File: /etc/apt/apt.conf.d/02periodic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Control parameters for cron jobs by /etc/cron.daily/apt //

// Enable the update/upgrade script (0=disable)
APT::Periodic::Enable "1";

// Do "apt update" automatically every n-days (0=disable)
APT::Periodic::Update-Package-Lists "1";

// Do "apt upgrade --download-only" every n-days (0=disable)
APT::Periodic::Download-Upgradeable-Packages "1";

// 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
APT::Periodic::Unattended-Upgrade "1";

// Do "apt autoclean" every n-days (0=disable)
APT::Periodic::AutocleanInterval "21";

// Send report mail to root
//     0:  no report             (or null string)
//     1:  progress report       (actually any string)
//     2:  + command outputs     (remove -qq, remove 2>/dev/null, add -d)
//     3:  + trace on
APT::Periodic::Verbose "2";

5. “listchanges.conf” の編集

パッケージ変更履歴ツール apt-listchanges のための設定を行う。(Debian 8 までとは若干異なる)

File: /etc/apt/listchanges.conf

1
2
3
4
5
6
[apt]
frontend=pager
confirm=false
email_address=root
save_seen=/var/lib/apt/listchanges.db
which=both          # <= news から変更

6. 動作確認

“/etc/cron.daily/apt-compat” により毎日実行されるので、後日メールやログを確認してみる。

  • メールは設定したユーザ(or メールアドレス)宛に送信される。
  • dpkg 関連のログは “/var/log/dpkg.log”
  • unattended-upgrades 関連のログは “/var/log/unattended-upgrades/” ディレクトリ配下。

7. 参考サイト


以上。





 

Sponsored Link

 

Comments