apt-get 是 Debian/Ubuntu 系統的經典命令列包管理工具,專為腳本自動化、非互動環境設計。雖 apt (新版統一介面) 更適合人類互動,但 apt-get 輸出穩定、返回碼標準、選項精確,**是 CI/CD、Ansible、Dockerfile、自動化腳本的首選**。
| 階段 | 命令 | 關鍵參數/說明 |
|---|---|---|
| 索引更新 | apt-get update | **必做第一步**。下載倉庫元數據。修改 sources.list 後、定期執行、腳本開頭必執行。 |
apt-get update -o Dir::Etc::sourcelist=custom.list | 僅更新指定源文件 (加速 CI)。 | |
| 包安裝 | apt-get install nginx | 自動解決依賴。 |
apt-get install -y nginx | -y 自動回答 Yes,**腳本必加**。 | |
apt-get install --no-install-recommends nginx | 不安裝 Recommends 依賴 (精簡容器鏡像)。 | |
apt-get install nginx=1.18.0-0ubuntu1 | 指定版本安裝 (釘版)。 | |
| 包移除 | apt-get remove nginx | 移除二進制,保留配置文件 (/etc/nginx)。 |
apt-get purge nginx | 徹底清除 (含配置)。等同 remove --purge。 | |
| 系統升級 | apt-get upgrade | 升級所有可升級包,**不移除**已安裝包 (安全、保守)。 |
apt-get dist-upgrade | 智能升級,**可移除/安裝**包以解決依賴變更 (核心升級、大版本遷移必用)。 | |
| 查詢與資訊 | apt-cache search keyword | 搜尋包名/描述 (apt search 同)。 |
apt-cache show nginx | 顯示完整控制文件信息 (版本、依賴、描述、大小、SHA256)。 | |
apt-cache policy nginx | 顯示所有可用版本、優先級、來源倉庫 (釘版/調試必備)。 | |
| 緩存維護 | apt-get clean | 刪除 /var/cache/apt/archives/ 下所有 .deb,**釋放磁碟空間**。 |
apt-get autoclean | 僅刪除**已無法下載** (舊版本) 的 .deb,保留當前版本緩存。 | |
| 源碼獲取 | apt-get source nginx | 下載源碼包 (.dsc, .orig.tar.gz, .debian.tar.xz) 到當前目錄,**需啟用 deb-src 源**。 |
| 構建依賴 | apt-get build-dep nginx | 安裝編譯該包所需的所有構建依賴 (-dev 包)。 |
# 格式: 類型 URI 發行版 組件
# 類型: deb (二進制), deb-src (源碼)
# URI: http/https/ftp/file/cdrom
# 發行版: focal, jammy, noble / stable, testing, sid / bookworm
# 組件: main(自由), restricted(非自由驅動), universe(社區), multiverse(非自由軟體)
# 範例: Ubuntu 22.04 (Jammy) 阿里雲鏡像
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src ... (需要源碼時啟用)
第三方倉庫最佳實踐:放入 /etc/apt/sources.list.d/ 獨立文件 (如 docker.list, kubernetes.list),便於管理與禁用。
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
apt-get install -y --no-install-recommends \
nginx \
curl \
ca-certificates && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
apt-get download nginx # 僅下載 nginx.deb
apt-get install --download-only nginx # 下載 nginx 及所有依賴到緩存
# /etc/apt/preferences.d/nginx-pin
Package: nginx
Pin: version 1.18.0*
Pin-Priority: 1001 # 1001 > 1000, 強制鎖定此版本,即使有更新也不升級
# 檢查生效
apt-cache policy nginx
# E: Could not get lock /var/lib/dpkg/lock-frontend
# 原因: 另一 apt/dpkg/aptitude 正在運行。勿強制刪除 lock 文件!
# 正確: 等待或找出進程
ps aux | grep -E 'apt|dpkg|unattended'
# 若確定無殭屍進程 (如掛起的 SSH), 才可刪除:
# rm /var/lib/dpkg/lock-frontend /var/cache/apt/archives/lock
| 錯誤現象 | 原因 | 解決 |
|---|---|---|
| 404 Not Found / Hash Sum mismatch | 鏡像源不同步/損壞 | 換源、apt-get clean、apt-get update | GPG 錯誤: NO_PUBKEY | 缺少倉庫簽名公鑰 | apt-key adv --keyserver keyserver.ubuntu.com --recv-keys (舊) 或下載 .asc 放入 /etc/apt/trusted.gpg.d/ |
| Unmet dependencies / Held broken packages | 依賴衝突、版本不匹配、hold 包 | apt-get install -f, apt-get dist-upgrade, apt-mark showhold, 檢查 Pin-Priority |
| dpkg: error processing package | 安裝/移除腳本失敗 | dpkg -C, dpkg --configure -a, 查看 /var/lib/dpkg/info/pkg.postinst |
apt-get 以**確定性、可腳本化、穩定輸出**著稱。掌握 update → install -y --no-install-recommends → clean → rm -rf /var/lib/apt/lists/* 黃金鏈路,配合 apt-cache policy 版本審計、Pinning 釘版、build-dep 構建環境準備,即可在自動化流水線中穩健管理 Debian/Ubuntu 軟體生命週期。