目次

【Ubuntu Server】apt update とapt upgradeを自動実行

Ubuntuサーバで、apt updateapt upgradeを手動で都度実行するのも手間なので、自動実行できるようにしたので備忘録を残す。

unattended-upgradesをインストールと設定

下記コマンドで、自動アップデートパッケージをインストール。

$ sudo apt update
$ sudo apt install unattended-upgrades

次に、以下のコマンドで自動アップデートを有効化する。

$ sudo dpkg-reconfigure unattended-upgrades

実行スケジュール設定

自動アップデートが行われる間隔を、/etc/apt/apt.conf.d/20auto-upgradesファイルで調整する。
デフォルトでは“1”日の設定になっているので“5”日に変更する。

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

 ↓

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

実行アップデート対象の確認

apt-cache policyコマンドで確認できる。

$ apt-cache policy
Package files:
 100 /var/lib/dpkg/status
     release a=now
 500 http://security.ubuntu.com/ubuntu resolute-security/universe amd64 Packages
     release v=26.04,o=Ubuntu,a=resolute-security,n=resolute,l=Ubuntu,c=universe,b=amd64
     origin security.ubuntu.com
 500 http://security.ubuntu.com/ubuntu resolute-security/restricted amd64 Packages
     release v=26.04,o=Ubuntu,a=resolute-security,n=resolute,l=Ubuntu,c=restricted,b=amd64
     origin security.ubuntu.com
 500 http://security.ubuntu.com/ubuntu resolute-security/main amd64 Packages
     release v=26.04,o=Ubuntu,a=resolute-security,n=resolute,l=Ubuntu,c=main,b=amd64
     origin security.ubuntu.com
 500 http://archive.ubuntu.com/ubuntu resolute-updates/multiverse amd64 Packages
     release v=26.04,o=Ubuntu,a=resolute-updates,n=resolute,l=Ubuntu,c=multiverse,b=amd64
     origin archive.ubuntu.com
 500 http://archive.ubuntu.com/ubuntu resolute-updates/universe amd64 Packages
     release v=26.04,o=Ubuntu,a=resolute-updates,n=resolute,l=Ubuntu,c=universe,b=amd64
     origin archive.ubuntu.com
 500 http://archive.ubuntu.com/ubuntu resolute-updates/restricted amd64 Packages
     release v=26.04,o=Ubuntu,a=resolute-updates,n=resolute,l=Ubuntu,c=restricted,b=amd64
     origin archive.ubuntu.com
 500 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 Packages
     release v=26.04,o=Ubuntu,a=resolute-updates,n=resolute,l=Ubuntu,c=main,b=amd64
     origin archive.ubuntu.com
 500 http://archive.ubuntu.com/ubuntu resolute/multiverse amd64 Packages
     release v=26.04,o=Ubuntu,a=resolute,n=resolute,l=Ubuntu,c=multiverse,b=amd64
     origin archive.ubuntu.com
 500 http://archive.ubuntu.com/ubuntu resolute/universe amd64 Packages
     release v=26.04,o=Ubuntu,a=resolute,n=resolute,l=Ubuntu,c=universe,b=amd64
     origin archive.ubuntu.com
 500 http://archive.ubuntu.com/ubuntu resolute/restricted amd64 Packages
     release v=26.04,o=Ubuntu,a=resolute,n=resolute,l=Ubuntu,c=restricted,b=amd64
     origin archive.ubuntu.com
 500 http://archive.ubuntu.com/ubuntu resolute/main amd64 Packages
     release v=26.04,o=Ubuntu,a=resolute,n=resolute,l=Ubuntu,c=main,b=amd64
     origin archive.ubuntu.com
Pinned packages:

自動再起動の設定

パッケージを更新した時に、再起動を要求される場合がある。
そのような場合に対応する為に、/etc/apt/apt.conf.d/50unattended-upgradesファイルを設定する事で、再起動要求のアップグレードがあった場合に特定の時間に再起動をしてくれる。

// Automatically reboot *WITHOUT CONFIRMATION* if
//  the file /var/run/reboot-required is found after the upgrade
// 以下をtrueに変更
Unattended-Upgrade::Automatic-Reboot "true";
 
// Automatically reboot even if there are users currently logged in
// when Unattended-Upgrade::Automatic-Reboot is set to true
//Unattended-Upgrade::Automatic-Reboot-WithUsers "true";
 
// If automatic reboot is enabled and needed, reboot at the specific
// time instead of immediately
//  Default: "now"
// 以下の時間を変更(即時の場合はnowに設定する)
Unattended-Upgrade::Automatic-Reboot-Time "02:00";

上記のようにコメントアウトしてあるところを書き換える。

ログの確認

自動更新のログは/var/log/unattended-upgrades/に保存される。

又、以下のコマンドを実行すると、実際の動作前に設定した挙動をドライランで確認することができる。

$ sudo unattended-upgrade --debug --dry-run

ドライランでは実際にはアップグレードは行われませんが、対象のパッケージ名を確認することができる。

参考