Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Ubuntu/Debian apt-get 出現「407 Proxy Authentication Required」錯誤的解決方法

編輯:關於伺服器

企業內網環境下,Ubuntu/Debian 伺服器需透過**需要帳號密碼驗證的 HTTP/HTTPS Proxy** 連網。若僅在環境變數 http_proxy/https_proxy 設定代理,apt-get update/install 仍會報錯:

Err:1 https://archive.ubuntu.com/ubuntu focal InRelease
  407  Proxy Authentication Required
Reading package lists... Done
W: Failed to fetch https://archive.ubuntu.com/ubuntu/dists/focal/InRelease  407  Proxy Authentication Required

原因:**APT 不直接讀取環境變數**,需在 /etc/apt/apt.conf (或 /etc/apt/apt.conf.d/99proxy) 明確配置含認證資訊的代理 URL。

解決步驟

  1. 建立/編輯 APT 代理配置檔 (建議使用 .d 目錄分離管理):
cat > /etc/apt/apt.conf.d/99proxy <<'EOF'
Acquire::http::Proxy "https://username:password@proxy.server:port/";
Acquire::https::Proxy "https://username:password@proxy.server:port/";
Acquire::ftp::Proxy "ftp://username:password@proxy.server:port/";
EOF

關鍵點:

  • username:password 為代理驗證帳密 (特殊字符需 URL 編碼,如 @ → %40)
  • proxy.server:port 為代理伺服器位址與埠 (如 10.0.0.5:3128)
  • HTTPS 代理通常也用 https:// 協定 (CONNECT 隧道),除非代理明確要求 https://
  • 結尾斜線 / 建議保留

  1. 若代理支援 NTLM/Kerberos 整合驗證 (Windows 域環境),可嘗試:
Acquire::http::Proxy "https://DOMAIN\\username:password@proxy.server:port/";
# 雙反斜線轉義
  1. 立即測試:
apt-get update
# 應無 407 錯誤,正常抓取套件清單

進階:分場景配置 (內網源不走代理、外網源走代理)

若內部有私有 APT 源 (如 repo.company.local) 不需走代理,可用 Acquire::http::Proxy:: 針對主機名設定例外:

cat > /etc/apt/apt.conf.d/99proxy <<'EOF'
# 預設走代理
Acquire::http::Proxy "https://user:pass@proxy:3128/";
Acquire::https::Proxy "https://user:pass@proxy:3128/";

# 內網源直連 (不走代理)
Acquire::http::Proxy::repo.company.local "DIRECT";
Acquire::https::Proxy::repo.company.local "DIRECT";

# 特定外網源強制直連 (如內網鏡像站)
Acquire::http::Proxy::mirrors.aliyun.com "DIRECT";
Acquire::https::Proxy::mirrors.aliyun.com "DIRECT";
EOF

常見坑點與驗證

現象原因解決
仍報 407帳密錯誤、特殊字符未編碼、代理埠錯誤用 curl -x https://user:pass@proxy:port https://httpbin.org/ip 驗證代理本身可用
HTTPS 源失敗HTTPS 代理協定寫錯 (需用 https:// 建立 CONNECT 隧道)統一用 https:// 協定配置 https::Proxy
配置不生效檔案權限、語法錯誤 (少分號、引號未閉合)apt-config dump | grep -i proxy 驗證實際生效配置
DNS 解析失敗代理需代理端解析 DNS,但代理無法解析外網域名確認代理 DNS 設定,或在 /etc/hosts 加入常用源 IP

安全性提醒

  • /etc/apt/apt.conf.d/99proxy 含明文密碼,**權限務必設為 600**:chmod 600 /etc/apt/apt.conf.d/99proxy
  • 生產環境建議使用**專用只讀代理帳號**,定期輪換密碼。
  • 若使用配置管理工具 (Ansible/Salt/Puppet),請用 Vault/加密變數管理密碼,勿明文入庫。

APT 代理認證配置核心:**/etc/apt/apt.conf.d/99proxy 明文指定含帳密的代理 URL、HTTPS 也用 https:// 協定、內網源例外用 DIRECT、檔案權限 600**。配置正確後,apt-get update/install/upgrade 即可在受限網路環境下正常運作。

Copyright © Windows教程網 All Rights Reserved