Ubuntu「一鍵」設置代理
sonictl note: the DNS problem may be still there. Except proxychains
.
WSL (Windows Subsystem for Linux) or Ubuntu is OK for this tutorial. Other linux can also try this.
1. Windows first
-
Proxifier --> Clash
-
Walls on UWP apps: Use Fiddler
WinConfig
tool to disable it. -
WSL:
- 使用 SSTap 直接接管 Windows 除了 Windows 系統進程以外的網絡層,當然也包括 WSL
- Proxifier 並不能實現對 WSL 啟用代理
- 相對最完美的方法是使用 Proxychains, 但不方便
- 直接在終端配置 proxy, 如果在WSL上使用的app不多。
2. Linux, WSL: Configure proxy in Terminal
two lines of cmds:
export ALL_PROXY="socks5://127.0.0.1080"
export all_proxy="socks5://127.0.0.1080"
這樣 curl
wget
是都走代理了,包括 git
npm
yarn
.
如果需要設置 APT 走代理,需要修改 /etc/apt/apt.conf
:
echo -e "Acquire::http::Proxy \"http://127.0.0.1:1090\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
echo -e "Acquire::https::Proxy \"http://127.0.0.1:1090\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
3. Linux, WSL:【一鍵】解除、設置代理:
在 ~/.bashrc
or ~/.zshrc
里把解除代理和啟用代理寫成兩個 function:proxy()
和 noproxy()
,每次只要執行 proxy
就可以啟用代理、執行 noproxy
解除代理,近似實現「一鍵操作」。可在 function 中加上 curl ip.gs
來測試當前 IP。
here are the codes:
proxy () {
export ALL_PROXY="socks5://127.0.0.1:1080"
export all_proxy="socks5://127.0.0.1:1080"
echo -e "Acquire::http::Proxy \"http://127.0.0.1:1090\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
echo -e "Acquire::https::Proxy \"http://127.0.0.1:1090\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
curl https://ip.gs
}
noproxy () {
unset ALL_PROXY
unset all_proxy
sudo sed -i -e '/Acquire::http::Proxy/d' /etc/apt/apt.conf
sudo sed -i -e '/Acquire::https::Proxy/d' /etc/apt/apt.conf
curl https://ip.gs
}
ps: zsh是一個終端環境,和bash類似。~/.zshrc就是zsh的配置文件,你可以自行更改。
read more: Mac OSX終端走代理
原文:
2018-11-29 筆記本
首先雖然題目包含 Ubuntu 但是實際上蘇卡卡解決的是 WSL 的代理問題的 WSL 是最好的 Linux 發行版;而且 Ubuntu 超級棒的 不出內部錯誤的話!蘇卡卡的方案也是通用於幾乎所有 Linux 的。
在 Windows 上使用全局代理亦或是透明代理真是太困難了。我之前在用 SSTap 實現全局代理;后來 Clash 和 Clash for Windows 出來以后就直接去用 Clash 了 界面漂亮才是第一生產力! 我就直接把 難看的 SSTap 丟掉了。但是 Clash 只是一個代理程序,並不能提供全局代理的方案。
好在 Windows 上實現全局代理或者分應用代理還有一個常用的方案——Proxifier,我可以通過 Proxifier 把絕大部分應用的流量轉發到 Clash 上面去,Windows 給 UWP 應用做的應用隔離則可以使用 Fiddler 的 WinConfig
工具解除掉。
但關鍵是 WSL。之前使用 SSTap 直接接管 Windows 除了 Windows 系統進程以外的網絡層,當然也包括 WSL。Proxifier 並不能實現對 WSL 啟用代理了,所以用 Proxifier 就得解決 WSL 也走代理的方法。
在 Google 上找遍了「Linux 全局代理」,相對最完美的方法是使用 Proxychains,但每個指令前面都要帶個 proxychains
真的太不方便了;使用 iptables
的透明代理方案在 WSL 上面也並不適用(WSL 和 Windows 兩者是相互依賴的、我不想在 Ubuntu 上設置 iptables
結果把宿主 Windows 炸掉)。
既然沒有,那就干脆不要了!直接在終端配置 proxy,反正目前在 WSL 上使用的應用和環境並不多。
給終端設置代理超簡單的,直接一句命令搞定:
export ALL_PROXY="socks5://127.0.0.1080"
這樣 curl
wget
是都走代理了,但是 git
npm
yarn
這些不知為何不跟隨終端的環境變量設置。一種解決方案是分別設置:
git config --global http.proxy "socks5://127.0.0.1080"
git config --global https.proxy "socks5://127.0.0.1080"
npm config set proxy "http://127.0.0.1090"
npm config set https-proxy "http://127.0.0.1090"
yarn config set proxy "http://127.0.0.1090"
yarn config set https-proxy "http://127.0.0.1090"
但是蘇卡卡發現了一個奇怪的解決方法,就是除了設置 ALL_RPOXY
以外再添加一個 all_proxy
:
export all_proxy="socks5://127.0.0.1080"
這樣不僅 git
npm
yarn
這些讀取環境變量不支持大小寫的辣雞,其它絕大部分終端應用程序不走代理的問題就都解決了~
不過包管理器 APT 就更麻煩了。如果需要設置 APT 走代理,需要修改 /etc/apt/apt.conf
:
echo -e "Acquire::http::Proxy \"http://127.0.0.1:1090\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
echo -e "Acquire::https::Proxy \"http://127.0.0.1:1090\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
敲了好一會鍵盤以后蘇卡卡想起了一個問題——如果要解除代理應該怎么辦。。。
於是蘇卡卡在 .zshrc
里面把解除代理和啟用代理寫成了兩個 function:proxy()
和 noproxy()
,每次只要執行 proxy
就可以啟用代理、執行 noproxy
解除代理,近似實現「一鍵操作」。而且,還可以在 function 中加上 curl ip.gs
來測試當前 IP。
Talk is cheap, here are the codes:
proxy () {
export ALL_PROXY="socks5://127.0.0.1:1080"
export all_proxy="socks5://127.0.0.1:1080"
echo -e "Acquire::http::Proxy \"http://127.0.0.1:1090\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
echo -e "Acquire::https::Proxy \"http://127.0.0.1:1090\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
curl https://ip.gs
}
noproxy () {
unset ALL_PROXY
unset all_proxy
sudo sed -i -e '/Acquire::http::Proxy/d' /etc/apt/apt.conf
sudo sed -i -e '/Acquire::https::Proxy/d' /etc/apt/apt.conf
curl https://ip.gs
}
稍加修改也可以寫進
.bashrc
之中
蘇卡卡最終的版本用 read
實現了讀取輸入來獲取代理地址、而不是直接使用預置 127.0.0.1:1080
的方案。代碼開在 這里。如果各位路過的大佬有什么更高明的 Linux 全局代理或者 WSL 由 Windows 接管代理的方法,請務必點醒蘇卡卡。
本文作者 : Sukka
本文采用 CC BY-NC-SA 4.0 許可協議。轉載和引用時請注意遵守協議!
本文鏈接 : https://blog.skk.moe/post/enable-proxy-on-ubuntu/