LoginSignup
52
50

More than 5 years have passed since last update.

Debian 系で unattended-upgrades を有効にする場合の追加設定 (メール通知, autoremove, autoclean, 再起動)

Last updated at Posted at 2016-02-07

何台か趣味のサーバ用に Ubuntu Server を動かしている。
趣味のサーバは時間が取れないことがあるので、手動アップデートだと忘れてしまうことがある。
unattended-upgrades を有効にすると、セキュリティ上重要なアップデートを自動で実施してくれる。
非常に便利。

自動アップデートにはもちろん、失敗する可能性・依存関係が壊れる可能性のリスクが、全くないわけではない。
セキュリティ上の問題を抱えたまま放置するリスクとの兼ね合いで、どちらをどの程度優先するか判断し、決めるべきだと思う。

いくつか注意点があるので備忘録として書き留めておく。

インストール

$ sudo apt-get install -y unattended-upgrades
$ sudo dpkg-reconfigure -plow unattended-upgrades
(TUI が表示されるので <Yes> を選択する)

メール通知を有効にする

自動でアップデートしてくれるとはいえ、アップデート時には通知して欲しい。

/etc/apt/apt.conf.d/50unattended-upgrades に設定がある。

$ sudo editor /etc/apt/apt.conf.d/50unattended-upgrades
// Send email to this address for problems or packages upgrades
// If empty or unset then no email is sent, make sure that you
// have a working mail setup on your system. A package that provides
// 'mailx' must be installed. E.g. "user@example.com"
//Unattended-Upgrade::Mail "root";

この下に

Unattended-Upgrade::Mail "じぶんのメールアドレス";

のように通知先を指定しておけばいい。

更にその下に

/etc/apt/apt.conf.d/50unattended-upgrades
// Set this value to "true" to get emails only on errors. Default
// is to always send a mail if Unattended-Upgrade::Mail is set
//Unattended-Upgrade::MailOnlyOnError "true";

の項目が有る。
これは、エラーの時だけ知りたい場合には設定しておくと良い。
自分はエラーか否かにかかわらず全部通知が来るようにしてある。

なお、メール通知するにはもちろんメールが送信できる状態になっていなくてはならない。
この時点で未設定であれば

$ sudo apt-get install -y mailutils

などを実行しておこう。 (TUI で別ホストの SMTP サーバ・ Postfix など、いくつかの設定から選んで実行できる)

定期的な autoremove / autoclean を有効にする。

特に /boot のパーティションを分けているケースでは重要になる。
unattended-upgrades がカーネルのセキュリティアップデートを実施していくと、
やがて /boot のパーティション容量を使いきってアップデートに失敗することがある。
不要なパッケージは削除したい。 (要 autoremove )

場合によっては、この時にリブートすると操作不能に陥ることがある。
(リカバリーモードで起動する必要があるが、 VPS 環境でブートメニューに到達できない (SSH での操作しかできない) ようなケースなど。)
最悪である。

また、 /boot のパーティションが一緒の場合でも、
apt-get のアーカイブファイルが増大し、ディスクフルに陥る場合がある。
不要なアーカイブファイルは削除したい。 (要 autoclean)
最近の豊富なディスク容量で、まともに運用・監視しているサーバなら、滅多にはないだろうけど。

つまり sudo apt-get -y autoremovesudo apt-get -y autoclean コマンド相当の処理を定期的に実行したい。

$ sudo editor /etc/apt/apt.conf.d/50unattended-upgrades
/etc/apt/apt.conf.d/50unattended-upgrades
// Do automatic removal of new unused dependencies after the upgrade
// (equivalent to apt-get autoremove)
//Unattended-Upgrade::Remove-Unused-Dependencies "false";

/etc/apt/apt.conf.d/50unattended-upgrades
Unattended-Upgrade::Remove-Unused-Dependencies "true";

を追加。 unattended-upgrade 時に autoremove する。

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

/etc/apt/apt.conf.d/20auto-upgrades
APT::Periodic::AutocleanInterval "1";

を。これは 1 日おきに autoclean する。

再起動について

セキュリティ上のアップデートには、リブート (再起動) が必要なものがある。
メールで通知する運用にしているなら、そのタイミングで管理者がログインして手動リブートするという前提の運用でも良いとは思う。
一方で、完全に自動化したいならリブートも設定できる。

自分には、自宅に自分がいる間だけ動作していれば良いサーバがいくつかあるので、この設定を有効にしているサーバもある。

/etc/apt/apt.conf.d/50unattended-upgrades
// Automatically reboot *WITHOUT CONFIRMATION*
//  if the file /var/run/reboot-required is found after the upgrade
//Unattended-Upgrade::Automatic-Reboot "false";
/etc/apt/apt.conf.d/50unattended-upgrades
Unattended-Upgrade::Automatic-Reboot "true";
/etc/apt/apt.conf.d/50unattended-upgrades
// If automatic reboot is enabled and needed, reboot at the specific
// time instead of immediately
//  Default: "now"
//Unattended-Upgrade::Automatic-Reboot-Time "02:00";
/etc/apt/apt.conf.d/50unattended-upgrades
Unattended-Upgrade::Automatic-Reboot-Time "02:00";

時刻は利用形態にあわせて設定する。

52
50
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
52
50