Ubuntu 與 Debian Linux 代理伺服器配置完整指南
更新於:
在 Ubuntu 與 Debian Linux 系統中,代理伺服器設定會依據執行上下文區分作用域:終端機 Session 讀取 /etc/environment 或 ~/.bashrc,APT 套件管理員讀取 /etc/apt/apt.conf.d/,systemd 系統服務讀取 drop-in 單元檔,而 GNOME 桌面環境則使用 gsettings。本指南為每個 Ubuntu 子系統提供搭配 VinaProxy HTTP(S) 代理的精準配置範例。
export http_proxy=http://user:pass@ip:port https_proxy=http://user:pass@ip:port- 1 個IP₫40,000
- 5+ 個IP₫35,000
- 10+ 個IP₫25,000
- 20+ 個IP₫25,000
- 50+ 個IP₫25,000
- 100+ 個IP₫25,000
- 200+ 個IP₫25,000
- 500+ 個IP₫25,000
- 1000+ 個IP₫22,000
- 2000+ 個IP₫20,000
- 1 個IP₫40,000
- 5+ 個IP₫35,000
- 10+ 個IP₫25,000
- 20+ 個IP₫25,000
- 50+ 個IP₫25,000
- 100+ 個IP₫25,000
- 200+ 個IP₫25,000
- 500+ 個IP₫25,000
- 1000+ 個IP₫22,000
- 2000+ 個IP₫20,000
Ubuntu 代理伺服器配置作用範圍一覽
預設情況下,sudo 會清除使用者的環境變數,而 systemd 服務也不會讀取 ~/.bashrc。請參考下表為 Ubuntu 選擇正確的設定檔:
| Ubuntu 子系統 | 設定檔 / 指令 | 適用範圍 | 持久性 |
|---|---|---|---|
| 互動式 Shell | ~/.bashrc 或 /etc/profile.d/proxy.sh | curl, wget, python, git, node | 每次登入 Shell 生效 |
| 系統全域 PAM 環境 | /etc/environment | 所有 PAM 使用者 Session | 重開機後依然有效 |
| APT 套件管理員 | /etc/apt/apt.conf.d/80proxy | sudo apt update 與 apt install | APT 獨立專用永久生效 |
| systemd 系統服務 | /etc/systemd/system/<svc>.service.d/proxy.conf | Docker 守護程序、自訂服務 | daemon-reload 後生效 |
Ubuntu Server 與 Desktop 逐步設定教學
bash
# 1. /etc/profile.d/proxy.sh(Shell 與 CLI 指令列工具)
sudo tee /etc/profile.d/proxy.sh > /dev/null << 'EOF'
export http_proxy="http://USER:PASS@IP:PORT"
export https_proxy="http://USER:PASS@IP:PORT"
export HTTP_PROXY="http://USER:PASS@IP:PORT"
export HTTPS_PROXY="http://USER:PASS@IP:PORT"
export no_proxy="localhost,127.0.0.1,::1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"
export NO_PROXY="$no_proxy"
EOF
source /etc/profile.d/proxy.sh
# 2. /etc/apt/apt.conf.d/80proxy(APT 套件管理員)
sudo tee /etc/apt/apt.conf.d/80proxy > /dev/null << 'EOF'
Acquire::http::Proxy "http://USER:PASS@IP:PORT/";
Acquire::https::Proxy "http://USER:PASS@IP:PORT/";
EOF
在 sudo 下保留代理變數與憑證存取權限控管
- 當透過
sudo curl ...或sudo pip ...執行指令時,請加上sudo -E參數以保留環境變數,或利用sudo visudo新增Defaults env_keep += "http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy NO_PROXY"。 - 在多使用者共享的 Linux 伺服器上,請嚴格限制設定檔權限(
sudo chmod 600 /etc/apt/apt.conf.d/80proxy),防止本機其他使用者窺探代理伺服器帳號密碼。
操作步驟
- 1
匯出 Shell 環境變數(/etc/profile.d/proxy.sh)
建立 /etc/profile.d/proxy.sh,同時定義小寫(http_proxy, https_proxy, no_proxy)與大寫(HTTP_PROXY, HTTPS_PROXY, NO_PROXY)變數,確保所有 GNU 及 Go/Rust 指令列工具都能識別。
- 2
配置 APT 套件管理員專用代理
由於 sudo apt update 會忽略一般使用者的變數,請建立 /etc/apt/apt.conf.d/80proxy 並填入 Acquire::http::Proxy 與 Acquire::https::Proxy。
- 3
使用 curl 測試外網連線
載入設定檔(
source /etc/profile.d/proxy.sh)並執行curl -s https://api.ipify.org,驗證 Ubuntu 機器是否已正確經由 VinaProxy 連線。
常見問題
為什麼在 Linux 上需要同時設定小寫 http_proxy 與大寫 HTTP_PROXY?
部分 Linux 工具(如 curl、wget)讀取小寫 http_proxy,而其他語言模組或 CLI 可能只辨識大寫 HTTP_PROXY。同時宣告兩者可確保所有軟體環境行為一致。
為什麼執行 `sudo apt update` 時會忽略已匯出的 `http_proxy` 變數?
sudo 出於安全考量預設會清空使用者環境變數。透過設定 /etc/apt/apt.conf.d/80proxy,可確保 APT 隨時乾淨穩定地連線代理。
如何在 Ubuntu 桌面版(GNOME)設定圖形化代理?
前往 設定 → 網路 → 網路代理 → 手動,或使用指令 gsettings set org.gnome.system.proxy mode 'manual' 並指定相應的主機與連接埠。
取得適用於 Ubuntu 的低延遲 HTTP(S) 代理
即時開通涵蓋 7 國的專屬 IPv4 與靜態 ISP 住宅代理憑據。
查看代理價格