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命令列使用SOCKS代理:tsocks輕鬆實現

編輯:Linux 教學

Linux圖形介面網路設定可直接配置SOCKS代理,但命令列工具(apt、wget、git、curl等)不認http_proxy環境變數的SOCKS協定。tsocks透過LD_PRELOAD攔截connect()系統呼叫,讓任意動態連結程式無感知走SOCKS代理。

安裝

# Debian/Ubuntu
sudo apt-get install tsocks
# RHEL/CentOS/Fedora
sudo yum install tsocks
# Arch
sudo pacman -S tsocks

配置 /etc/tsocks.conf

# 本地網段不走代理
local = 192.168.1.0/255.255.255.0
# local = 10.0.0.0/255.0.0.0

# 代理伺服器
server = 192.168.1.1
server_port = 1080
server_type = 5          # SOCKS v5 (預設),v4填4

使用方法

# 單次命令
tsocks apt-get update
tsocks git clone https://github.com/user/repo.git
tsocks wget https://example.com/file.zip

# 整個Shell會話
tsocks -on
# 現在所有命令自動走代理
apt-get update
git pull
tsocks -off

進階技巧

  • 多代理鏈:server = 127.0.0.1 配合proxychains 或 redsocks 實現透明代理鏈。
  • 驗證代理生效:tsocks curl -s https://api.ipify.org 應顯示代理出口IP。
  • 排除本地服務:local 可寫多行,支援 CIDR 與單 IP。
  • 非動態連結程式(Go、Rust靜態編譯二進位)tsocks 無效,需用 proxychains-ng 或 系統級透明代理(redsocks + iptables REDIRECT)。

替代方案對比

工具原理支援靜態編譯複雜度
tsocksLD_PRELOAD攔截connect否低
proxychains-ngLD_PRELOAD攔截connect/getaddrinfo否低
redsocks + iptables透明代理,TCP導向本地SOCKS轉換器是中
clash/tun2socksTUN裝置虛擬網卡,路由級透明代理是中高

tsocks 雖停止更新多年,但因極簡配置、零依賴、廣泛發佈版預裝,仍是命令列臨時走SOCKS代理的最佳選擇。

Copyright © Windows教程網 All Rights Reserved