ubuntu自動更新--unattended-upgrades
- 1 介紹
- 2 發展歷程
- 3 配置與使用
- 4 disable Auto update
- 服務命令
- 參考
1 介紹
Unattended-Upgrades 是一個用于自動更新 Debian 及其衍生系統(如 Ubuntu)的工具。它的主要功能是自動檢查、下載并安裝系統更新,特別是安全更新,從而確保系統的安全性和穩定性。該工具通過后臺運行,無需用戶干預,大大簡化了系統維護工作。
2 發展歷程
Unattended-Upgrades 最初是為了簡化 Debian 系統的安全更新流程而開發的。隨著 Ubuntu 的發展,它逐漸成為 Ubuntu 系統中默認安裝的組件之一。從 Ubuntu 16.04 開始,Unattended-Upgrades 的更新觸發機制從傳統的 cron 定時任務轉變為通過 systemd 的 timer unit 來實現。這種改進使得更新任務的調度更加靈活和高效。
3 配置與使用
安裝:可以通過命令 sudo apt-get install unattended-upgrades 安裝該工具。
配置:主要配置文件位于 /etc/apt/apt.conf.d/50unattended-upgrades,用戶可以在此文件中指定自動更新的規則,例如允許更新的軟件源、是否自動移除未使用的依賴包等。
自定義更新頻率:通過編輯 /etc/apt/apt.conf.d/20auto-upgrades 文件,可以設置更新檢查和安裝的頻率。
bot@u22:/etc/apt/apt.conf.d$ ls
00aptitude 15update-stamp 20packagekit 60icons
00trustcdrom 20apt-esm-hook.conf 20snapd.conf 60icons-hidpi
01autoremove 20archive 50appstream 70debconf
01-vendor-ubuntu 20auto-upgrades 50command-not-found 99update-notifier
10periodic 20dbus 50unattended-upgrades
bot@u22:/etc/apt/apt.conf.d$ cat 20auto-upgrades
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
4 disable Auto update
Ubuntu uses the unattended-upgrades package to handle automatic updates. You can disable this service to prevent automatic updates.
# 顯示狀態
bot@u22:~/sen$ systemctl status unattended-upgrades
● unattended-upgrades.service - Unattended Upgrades ShutdownLoaded: loaded (/lib/systemd/system/unattended-upgrades.service; enabled; >Active: active (running) since Wed 2025-04-06 06:25:39 CST; 12min agoDocs: man:unattended-upgrade(8)Main PID: 1003 (unattended-upgr)Tasks: 2 (limit: 4551)Memory: 11.7MCPU: 264msCGroup: /system.slice/unattended-upgrades.service└─1003 /usr/bin/python3 /usr/share/unattended-upgrades/unattended->4月 06 06:25:39 u22 systemd[1]: Started Unattended Upgrades Shutdown.
# 停止服務
bot@u22:~/sen$ sudo systemctl stop unattended-upgrades
[sudo] password for bot:
# 取消服務的開機啟動
bot@u22:~/sen$ sudo systemctl disable unattended-upgrades
Synchronizing state of unattended-upgrades.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable unattended-upgrades
Removed /etc/systemd/system/multi-user.target.wants/unattended-upgrades.service.
bot@u22:~/sen$ systemctl status unattended-upgrades
○ unattended-upgrades.service - Unattended Upgrades ShutdownLoaded: loaded (/lib/systemd/system/unattended-upgrades.service; disabled;>Active: inactive (dead)Docs: man:unattended-upgrade(8)4月 06 06:25:39 u22 systemd[1]: Started Unattended Upgrades Shutdown.
4月 06 06:52:48 u22 systemd[1]: Stopping Unattended Upgrades Shutdown...
4月 06 06:52:48 u22 systemd[1]: unattended-upgrades.service: Deactivated succes>
4月 06 06:52:48 u22 systemd[1]: Stopped Unattended Upgrades Shutdown.
bot@u22:~/sen$
服務命令
systemctl enable:除了激活服務以外,也可以置服務為開機啟動
systemctl disable:取消服務的開機啟動systemctl start:依次啟動定義在 Unit 文件中的 ExecStartPre、ExecStart 和 ExecStartPost 命令
systemctl stop:依次停止定義在 Unit 文件中的 ExecStopPre、ExecStop 和 ExecStopPost 命令
systemctl restart:重啟服務
systemctl kill:立即殺死服務service service_name status:查看服務狀態
參考
1、How to disable Auto update for my instance
2、Linux 服務管理 service systemd systemctl