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

Linux wget 設定 HTTP/HTTPS Proxy 三種方式:命令列參數、環境變數、配置檔

編輯:關於 Linux

在需經由 Proxy 存取外網的環境中,wget 預設不使用 Proxy,需顯式設定。本文提供三種設定方式,涵蓋單次指令、當前 Shell 會話、永久生效三個層級,並區分 HTTP/HTTPS 協定差異。

wget Proxy 設定示意

方式一:單次指令帶參數(臨時、精確控制)

# HTTP
wget https://example.com/file -e use_proxy=yes -e http_proxy=proxy.host:3128

# HTTPS (需額外參數)
wget --no-check-certificate https://example.com/file \
     -e use_proxy=yes -e https_proxy=proxy.host:3128
  • --no-check-certificate:跳過 SSL 憑證驗證(測試環境常用,生產慎用)。
  • https_proxy 參數專門處理 HTTPS 請求,不可混用 http_proxy。
  • 單次指令不影響其他程式或後續指令。

方式二:環境變數(當前 Shell 會話生效,影響所有支援 Proxy 的程式)

export http_proxy=https://proxy.host:3128
export https_proxy=https://proxy.host:3128
# 或帶認證
export http_proxy=https://user:pass@proxy.host:3128

# 取消 Proxy
unset http_proxy https_proxy
  • 設定後,wget、curl、apt、yum、git、pip 等皆自動使用。
  • 寫入 ~/.bashrc 或 /etc/profile 可永久生效。

方式三:~/.wgetrc 配置檔(wget 專用、永久生效)

cat > ~/.wgetrc <<'EOF'
http_proxy = https://proxy.host:3128/
https_proxy = https://proxy.host:3128/
ftp_proxy = https://proxy.host:3128/
use_proxy = on
wait = 15
EOF
  • 優先級:命令列參數 > 環境變數 > .wgetrc > 系統預設。
  • 系統級配置:/etc/wgetrc(影響所有用戶)。

特殊情況處理

情況指令/設定
單次指令強制不使用 Proxywget --no-proxy https://example.com
Proxy 需認證(帳號密碼含特殊字元)http_proxy=https://user%40domain:pass%23word@proxy.host:3128 (URL 編碼)
SOCKS5 Proxyexport http_proxy=socks5://proxy.host:1080 (wget 1.18+ 支援)
透明 Proxy/攔截式 (無需客戶端設定)無需設定,網路層自動導流

驗證 Proxy 生效

wget -q -O - https://httpbin.org/ip
# 輸出應為 Proxy 出口 IP,而非本機公網 IP

依據使用場景選擇方式:腳本自動化用環境變數、單次下載用命令列參數、個人習慣用 .wgetrc,三種方式靈活組合。

Copyright © Windows教程網 All Rights Reserved