0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

apt update とapt upgradeを自動実行する

Posted at

はじめに

この記事はUdon Advent Calendar 2025 - Adventarの15日目の記事です。

apt update及びapt upgradeの自動実行

Ubuntuサーバーを使っていると、apt updateapt upgradeを実行してパッケージリストの更新やパッケージのアップグレードをすることがよくあります。

ただ、これを毎回やるのは面倒ですし、長い間やり忘れる可能性があるのはセキュリティ的にもよろしくありません。

ということで、今回はこれらを自動で実行する方法を調べてみました。

検証環境

  • Ubuntu 24.04.3 LTS

unattended-upgradesの設定

インストール・有効化

このパッケージを用いることで、自動アップデートが有効化できるようです。まず、以下のコマンドでこのパッケージをインストールしましょう。

sudo apt update
sudo apt install unattended-upgrades

その後、以下のコマンドで自動アップデートを有効化します。

sudo dpkg-reconfigure unattended-upgrades

コマンドを実行すると設定ウィンドウが開くので、「yes」を選択します。

これによって自動アップデートが有効化されました。思ったより簡単ですね。

スケジュール設定

自動アップデートが行われる間隔を調整してみましょう。

以下のコマンドで設定ファイルを編集します。

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

設定ファイルには最初以下の内容が書いてあります。

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

上のUpdate-Package-Listsがパッケージリストの更新、下のUnattended-Upgradeがパッケージのアップグレードになります。

この1というのは更新する間隔で、単位は「日」です。ここを変更することで更新間隔を調整することができます。

0とすると自動更新が無効化されます。

自動アップデートの対象パッケージを指定する

特定のパッケージを更新対象外としたい場合は/etc/apt/apt.conf.d/50unattended-upgradesで以下のようなUnattended-Upgrade::Package-Blacklistを指定します。

指定の際には正規表現を用いることができます。+などの特殊文字には\でのエスケープが必要です。

Unattended-Upgrade::Package-Blacklist {
    // 特定の名前
    "python3.12";
    // linux- から始まるすべてのパッケージ
    "linux-";
};

また、以下のようにUnattended-Upgrade::Origins-Patternを設定すると、特定のソースから取得されているパッケージを自動更新対象から除外できます。

Unattended-Upgrade::Origins-Pattern {
    # origin が Google, Inc.、suite が contrib のパッケージ
    "origin=Google\, Inc.,suite=contrib";
    # www.example.com から取得され、component が main のパッケージ
    "site=www.example.com,component=main";
};

ちなみに、これらの情報はapt-cache policyコマンドで確認できます。

$ apt-cache policy
Package files:
 100 /var/lib/dpkg/status
     release a=now
 500 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Packages
     release v=24.04,o=Ubuntu,a=noble-security,n=noble,l=Ubuntu,c=multiverse,b=amd64
     origin security.ubuntu.com
 500 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Packages
     release v=24.04,o=Ubuntu,a=noble-security,n=noble,l=Ubuntu,c=universe,b=amd64
     origin security.ubuntu.com

例えば、URLがsiteooriginccomponent、といった感じで対応しているようです。

自動再起動の設定

パッケージをアップグレードしたあと反映に再起動が必要な場合があります。

そんな時は、/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

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

おわりに

やらないならやらないで問題があり、かといって自発的に実行するのは面倒な2つのコマンドを自動で実行する方法が見つかりました。

これからは安心してサーバを管理できそうで良かったです。

それではまた、明日の記事でお会いしましょう!

参考文献

Linux – apt の自動アップデートと停止方法 | pystyle

Ubuntuで自動的にapt upgradeするには? - @ledsun blog

大切なことはすべてUbuntuが教えてくれた 無人アップグレードを知りましょう #Linux - Qiita

Ubuntu 20.04 の自動アップデートの設定|pt

unattended-upgradeでUbuntuのパッケージを自動更新する #ubuntu20.04 - Qiita

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?