以下のバージョンで動きを確認しました。
$ uname-v#71~20.04.1-Ubuntu SMP Thu Jul 15 17:46:08 UTC 2021
パッケージ情報です。
$ apt info-a unattended-upgradesPackage: unattended-upgradesVersion:2.3ubuntu0.1Priority: optionalSection: adminOrigin: UbuntuMaintainer: Ubuntu Developers<ubuntu-devel-discuss@lists.ubuntu.com>Original-Maintainer: Michael Vogt<mvo@debian.org>Bugs: https://bugs.launchpad.net/ubuntu/+filebugInstalled-Size:451 kBDepends: debconf(>=0.5) | debconf-2.0, debconf, python3, python3-apt(>=1.9.6~), python3-dbus, python3-distro-info, ucf, lsb-release, lsb-base, xz-utilsRecommends: systemd-sysv | cron | cron-daemon | anacronSuggests: bsd-mailx, default-mta | mail-transport-agent, needrestart, powermgmt-base, python3-giTask: server, ubuntu-desktop-minimal, ubuntu-desktop, cloud-image, kubuntu-desktop, xubuntu-core, xubuntu-desktop, lubuntu-desktop, ubuntustudio-desktop-core, ubuntustudio-desktop, ubuntukylin-desktop, ubuntu-mate-core, ubuntu-mate-desktop, ubuntu-budgie-desktopDownload-Size:48.7 kBAPT-Manual-Installed: yesAPT-Sources: http://jp.archive.ubuntu.com/ubuntu focal-updates/main amd64 PackagesDescription: セキュリティ更新の自動インストール 本パッケージによりセキュリティ更新を自動かつ無人でダウンロードおよび インストールすることができます。設定された APT ソースに由来するインストール 済みパッケージのみを取り扱い、設定ファイル変更に関する dpkg プロンプトを チェックします。 . 本スクリプトは APT:Periodic:Unattended-Upgrade オプションのバックエンド です。
WEBで調べているとanacronで実行されているように書かれている記事もあり、実際に以下のファイルが登録されています。
/etc/cron.daily/apt-compat
ただ、中身を見るとわかるのですが、最初にsystemd存在確認が走り、systemdのあるシステムの場合は空振りするようになっています。
$ cat /etc/cron.daily/apt-compat | head-n13#!/bin/shset-e# Systemd systems use a systemd timer unit which is preferable to# run. We want to randomize the apt update and unattended-upgrade# runs as much as possible to avoid hitting the mirrors all at the# same time. The systemd time is better at this than the fixed# cron.daily timeif [-d /run/systemd/system];thenexit0fi
anacronのコメントに書いてある通り、現在はSystemdでスケジュールされているようです。自動更新のスケジュールは以下のコマンドで確認できます。
systemctl list-timers
コマンドを使うとTimerの一覧を確認できます。unattended-upgrade
に関連するのはapt-daily
とapt-daily-upgrade
の二つです。
$ systemctl list-timers--allNEXT LEFT LAST PASSED UNIT ACTIVATES# 省略Tue 2021-07-27 04:59:09 JST 5h 56min left Mon 2021-07-26 07:49:08 JST 15h ago apt-daily.timer apt-daily.serviceTue 2021-07-27 06:28:11 JST 7h left Mon 2021-07-26 06:34:08 JST 16h ago apt-daily-upgrade.timer apt-daily-upgrade.service# 省略
スケジュールが設定されているtimerファイルの中身は以下のようになります。
$ cat /etc/systemd/system/timers.target.wants/apt-daily.timer[Unit]Description=Daily apt download activities[Timer]OnCalendar=*-*-*6,18:00RandomizedDelaySec=12hPersistent=true[Install]WantedBy=timers.target
$ cat /etc/systemd/system/timers.target.wants/apt-daily-upgrade.timer[Unit]Description=Daily apt upgrade and clean activitiesAfter=apt-daily.timer[Timer]OnCalendar=*-*-* 6:00RandomizedDelaySec=60mPersistent=true[Install]WantedBy=timers.target
スケジュール設定はsystemdのTimer定義から行います。
以下からOnCalendar
やRandomizedDelaySec
を変更してから、systemctl daemon-reload
を実行することで設定が反映されます。
systemdのサービスファイルです。
ExecStartPre=-/usr/lib/apt/apt-helper wait-online
にてオンラインになるのを待ってから、/usr/lib/apt/apt.systemd.daily
をそれぞれinstall
とupdate
の引数付きで呼んでいます。
$ cat /lib/systemd/system/apt-daily-upgrade.service[Unit]Description=Daily apt upgrade and clean activitiesDocumentation=man:apt(8)ConditionACPower=trueAfter=apt-daily.service network.target network-online.target systemd-networkd.service NetworkManager.service connman.service[Service]Type=oneshotExecStartPre=-/usr/lib/apt/apt-helper wait-onlineExecStart=/usr/lib/apt/apt.systemd.daily installKillMode=processTimeoutStopSec=900
$ cat /lib/systemd/system/apt-daily.service[Unit]Description=Daily apt download activitiesDocumentation=man:apt(8)ConditionACPower=trueAfter=network.target network-online.target systemd-networkd.service NetworkManager.service connman.service[Service]Type=oneshotExecStartPre=-/usr/lib/apt/apt-helper wait-onlineExecStart=/usr/lib/apt/apt.systemd.daily update
/usr/lib/apt/apt.systemd.daily
はシェルスクリプトです。サービスの名前や引数からは直感的に何しているかわからなかったので、主なコマンドを並べてみました。何となく何しているのかわかると思います。
主なコマンド以外だと/var/cache/apt
のディレクトリ下の/var/cache/apt/archive
と/var/cache/apt/backup
などを使った過去データのバックアップや世代管理なども行っています。
Service | Timer | ExecStart | 主なコマンド |
---|---|---|---|
apt-daily-upgrade.service | apt-daily-upgrade.timer | /usr/lib/apt/apt.systemd.daily install | -unattended-upgrade -apt-get clean -apt-get autoclean |
apt-daily.service | apt-daily.timer | /usr/lib/apt/apt.systemd.daily update | -apt-get update -apt-get dist-upgrade -d (-d: only download) -unattended-upgrade --download-only |
unattended-upgradeですがパラメータで挙動を調整することが可能です。
まずパラメータ値を確認するには以下のコマンドを実行します。
$ apt-config dump
以下にパラメータの設定が集まっています。
$ls-1 /etc/apt/apt.conf.d/00aptitude00trustcdrom01-vendor-ubuntu01autoremove01autoremove-kernels10periodic15update-stamp20archive20auto-upgrades20dbus20packagekit20snapd.conf50appstream50command-not-found50unattended-upgrades60icons60icons-hidpi70debconf99update-notifier
特に/etc/apt/apt.conf.d/50unattended-upgrades
はunattended-upgrades
自体の挙動を制御するパラメータ群となります。
/usr/bin/unattended-upgrade
が`Pythonスクリプトなので、中身見ながら説明書いてみます。
パラメータ | デフォルト | 説明 |
---|---|---|
Unattended-Upgrade::Allowed-Origins | - ${distro_id}:${distro_codename}-${distro_id}:${distro_codename}-security- ${distro_id}ESMApps:${distro_codename}-apps-security- ${distro_id}ESM:${distro_codename}-infra-security[マクロ変数に入る値の例] ${distro_id} = "ubuntu" ${distro_codename} = "focal" | 許可されるパッケージ取得元 |
Unattended-Upgrade::Package-Blacklist | - | 更新除外するパッケージ |
Unattended-Upgrade::DevRelease | auto | devel releaseを自動更新するか(true/false/auto) |
Unattended-Upgrade::AutoFixInterruptedDpkg | true | dpkg --force-confold --configure -a の自動実行を行うか |
Unattended-Upgrade::InstallOnShutdown | false | Shutdown時に更新のインストールを行うか |
Unattended-Upgrade::Mail | /usr/bin/mail や/usr/sbin/sendmail などで通知メールを送信するか | |
Unattended-Upgrade::MailReport | on-change | メールを送信する条件:"always", "only-on-error" or "on-change" |
Unattended-Upgrade::Remove-Unused-Kernel-Packages | true | 利用されていないカーネル関連パッケージの削除 |
Unattended-Upgrade::Remove-New-Unused-Dependencies | true | 更新後の利用されていないパッケージの削除 |
Unattended-Upgrade::Automatic-Reboot | false | /var/run/reboot-requiredが存在する場合に自動でOS再起動 |
Unattended-Upgrade::Automatic-Reboot-WithUsers | true | Unattended-Upgrade::Automatic-Reboot が有効で再起動を実施する際に、ログオンユーザチェックを行うか。チェック時にログオンユーザがいる場合は再起動を行わない。(falseの場合にチェックを行う) |
Unattended-Upgrade::Automatic-Reboot-Time | now | 02:00 など時間を設定することで、/sbin/shutdown -r pram として実行される |
Acquire::http::Dl-Limit | 70(kb/sec) | 利用帯域制限 |
Unattended-Upgrade::SyslogEnable | false | Syslogへの出力 |
Unattended-Upgrade::SyslogFacility | daemon | Syslogのファシリティ |
Unattended-Upgrade::OnlyOnACPower | true | /usr/sbin/on_ac_power コマンドの結果を元にAC Powerでない場合は処理を中断するか |
Unattended-Upgrade::Skip-Updates-On-Metered-Connections | true | Metered ConnectionはCellar等の従量課金などの接続を指すようで、以下のコードで判断されます。from gi.repository.Gio import NetworkMonitor NetworkMonitor.get_network_metered(NetworkMonitor.get_default()) |
Unattended-Upgrade::Verbose | false | ログレベルのVerboseモード |
Unattended-Upgrade::Debug | false | debugログの出力 |
Unattended-Upgrade::Allow-downgrade | false | パッケージのダウングレードの許可 |
systemdから起動時に呼ばれる/usr/lib/apt/apt.systemd.daily
に設定可能なパラメータを以下に整理してみました。
パラメータ | デフォルト | 説明 |
---|---|---|
Dir | / | ルートディレクトリ |
Dir::Cache | var/cache/apt/ | キャッシュディレクトリ |
Dir::Cache::Archives | archives/ | アーカイブディレクトリ |
Dir::Cache::Backup | backup/ | バックアップディレクトリ |
APT::Periodic::Enable | 1 | 0 に設定すると処理がスキップされる(自動更新が止まる) |
APT::Periodic::BackupArchiveInterval | 0 | backupフォルダにCacheのバックアップを行うか |
APT::Periodic::BackupLevel | 3 | バックアップ世代管理パラメータ |
APT::Periodic::MaxAgeAPT::Periodic::MaxAge(old, deprecated) | 0 | バックアップ世代管理パラメータ |
APT::Periodic::MaxSizeAPT::Periodic::MaxSize(old, deprecated) | 0 | バックアップ世代管理パラメータ |
APT::Periodic::MinAgeAPT::Periodic::MinAge(old, deprecated) | 2 | バックアップ世代管理パラメータ |
APT::Periodic::Update-Package-Lists | 0 | apt-get update を何日おきに実行するか(0は無効) |
APT::Periodic::Download-Upgradeable-Packages | 0 | apt-get upgrade --download-only を何日おきに実行するか(0は無効) |
APT::Periodic::Download-Upgradeable-Packages-Debdelta | 1 | debdelta-upgrade でupdateをダウンロードする(0は無効) |
APT::Periodic::Unattended-Upgrade | 0 | unattended-upgrade でセキュリティアップグレードを何日おきに実行するか(0は無効) |
APT::Periodic::AutocleanInterval | 0 | apt-get autoclean を何日おきに実行するか(0は無効) |
APT::Periodic::CleanInterval | 0 | apt-get clean を何日おきに実行するか(0は無効) |
APT::Periodic::Verbose | 0 | rootユーザへのメール通知 0 : no report 1: progress report 2: command output 3: trace on |
通常の使い方の場合の主な設定ファイルは以下の2つです。
$ cat /etc/apt/apt.conf.d/20auto-upgradesAPT::Periodic::Update-Package-Lists"1";APT::Periodic::Unattended-Upgrade"1";
/etc/apt/apt.conf.d/50unattended-upgrades
以下のように最後に読み込まれるファイルを作成して上書きすることもできます。
/etc/apt/apt.conf.d/99custom
設定後は思った通りの設定になっているかapt-config dump
で確認します。
unattended-upgradeが走っている時にシャットダウンのシグナルを検知した時などのハンドリングを行うサービスです。以下のようにSystemdで定義されています。
$ cat /etc/systemd/system/multi-user.target.wants/unattended-upgrades.service[Unit]Description=Unattended Upgrades ShutdownAfter=network.target local-fs.target systemd-logind.serviceRequiresMountsFor=/run /var/log /var/run /var/lib /bootDocumentation=man:unattended-upgrade(8)[Service]ExecStart=/usr/share/unattended-upgrades/unattended-upgrade-shutdown--wait-for-signalKillMode=processTimeoutStopSec=1800[Install]WantedBy=multi-user.target
ExecStart見るとわかる通り、実行されている/usr/share/unattended-upgrades/unattended-upgrade-shutdown
はPythonスクリプトなので中身を見ると何しているかわかりやすいです。
デフォルトでは以下にログが出力されます。
$ls-1 /var/log/unattended-upgradesunattended-upgrades-dpkg.logunattended-upgrades-dpkg.log.1.gzunattended-upgrades-shutdown.logunattended-upgrades-shutdown.log.1.gzunattended-upgrades.logunattended-upgrades.log.1.gz
# -d を付けてログ出力ありで実行$ sudo unattended-upgrade-d# --dry-runもあります$ sudo unattended-upgrade-d--dry-run
引用をストックしました
引用するにはまずログインしてください
引用をストックできませんでした。再度お試しください
限定公開記事のため引用できません。