Skip to content
VDS · セキュリティ

自動セキュリティアップデート

Debian と Ubuntu の unattended-upgrades、AlmaLinux の dnf-automatic を設定し、夜間にサービスを停止させずにパッチを自動インストールする方法について説明します。

更新日 2026年8月23日

ほとんどの侵害は、数か月前に修正済みの脆弱性を突いて発生します。手動更新は後回しにされがちですが、自動更新ならそうなりません。賢明な構成では、セキュリティ更新のみをインストールし、それ以外には手を付けません。

Debian と Ubuntu

apt install -y unattended-upgrades apt-listchanges
dpkg-reconfigure -plow unattended-upgrades

ファイル /etc/apt/apt.conf.d/50unattended-upgrades:

Unattended-Upgrade::Allowed-Origins {
    "${distro_id}:${distro_codename}-security";
};
Unattended-Upgrade::Package-Blacklist {
    "postgresql-.*";
    "docker-ce";
};
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "false";
Unattended-Upgrade::Mail "you@example.com";

スケジュールは /etc/apt/apt.conf.d/20auto-upgrades で設定します:

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

変更を加えずにドライランを実行します:

unattended-upgrade --dry-run --debug
systemctl status unattended-upgrades
cat /var/log/unattended-upgrades/unattended-upgrades.log

AlmaLinux と CentOS Stream

dnf install -y dnf-automatic

/etc/dnf/automatic.conf 内:

[commands]
upgrade_type = security
apply_updates = yes
reboot = never
systemctl enable --now dnf-automatic.timer
systemctl list-timers | grep dnf

除外すべきもの

バージョンが予期せず変わると壊れるものはすべて除外リストに追加してください。データベースサーバー、Docker、アプリケーションエンジン本体などです。これらは負荷の少ない時間帯に手動で更新します。詳しくはシステムとパッケージの更新をご覧ください。

カーネルのセキュリティ更新もインストールされますが、これらは再起動後にのみ有効になります:

test -f /var/run/reboot-required && cat /var/run/reboot-required.pkgs

再起動:必ず人の判断で

Automatic-Reboot "true" を設定したくなる気持ちは強いですが、毎晩の自動再起動は誰も見ていない状態でサービスが再起動することを意味します。何かが起動しなくなった場合、翌朝ユーザーから知ることになります。

賢明な妥協案はこうです。更新は自動でインストールし、再起動は月に一度、/hosting/vds/m/<id> パネルでコンソールを監視しながら手動で行います。ついでに、サービスが自動で起動するかも確認しておきましょう:

systemctl is-enabled nginx postgresql
systemctl list-units --state=failed

よくある間違い

間違い結果対処
セキュリティだけでなく全リポジトリの更新を有効化機能変更が届いてしまう-security ソースのみを残す
データベースが除外リストにないメジャーバージョンでデータが壊れるPackage-Blacklist に追加する
夜間の自動再起動サービスが起動せず、誰も気づかないAutomatic-Reboot "false" を設定する
設定して放置カーネルは更新されるが適用されない月に一度 reboot-required を確認する
ダウンロードしたパッケージでディスクが埋まる空き容量がなくなる自動キャッシュクリーンアップを有効にしておく

自動更新は保険であり、注意を払うことの代わりにはなりません。月に一度、マシンを開いて更新ログ、失敗したサービスの一覧、再起動が必要かどうかを確認してください。月に15分で、侵害の兆候の記事にあるほとんどのシナリオをカバーできます。