Ubuntu 26.04 LTS で、セキュリティアップデートと再起動を自動化する

Ubuntu 26.04 LTSでは、unattended-upgradesで毎日の自動更新と、必要時だけの自動再起動を設定できます。通常の更新も対象にするため、-updatesを追加します。Ubuntu Server documentation

以下は対象のUbuntuサーバーで実行してください。

1. 必要なパッケージをインストール

sudo apt-get update
sudo apt-get install -y unattended-upgrades update-notifier-common

2. 自動更新と再起動を設定

下記は「毎日更新を確認し、再起動が必要なら午前4時に再起動」する例です。

sudo tee /etc/apt/apt.conf.d/99-local-auto-upgrades >/dev/null <<'EOF'
APT::Periodic::Enable "1";
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

Unattended-Upgrade::Allowed-Origins {
    "${distro_id}:${distro_codename}";
    "${distro_id}:${distro_codename}-security";
    "${distro_id}:${distro_codename}-updates";
};

Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-WithUsers "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";
EOF

sudo systemctl enable --now apt-daily.timer apt-daily-upgrade.timer

04:00はサーバーのタイムゾーン基準です。日本時間にする場合は次を実行します。ログイン中でも再起動する設定なので、SSH接続も切断されます。Ubuntu Server documentation

sudo timedatectl set-timezone Asia/Tokyo

3. 設定を確認

実際の更新や再起動をせずに動作確認できます。

sudo unattended-upgrade --dry-run --debug
systemctl list-timers 'apt-daily*'

更新履歴は次で確認できます。

sudo tail -n 100 /var/log/unattended-upgrades/unattended-upgrades.log

なお、午前4時は再起動の時刻で、更新自体は標準タイマーの時刻に実行されます。更新時にサービスが再起動する場合もあります。また、この設定はUbuntu公式の更新が対象で、追加した外部リポジトリも含めた apt-get upgrade と完全に同じ対象にはなりません。

© 2025 Hiroe Tech Notes. All rights reserved.

コメント

まだコメントはありません。