WSL2添加网络代理,通过宿主机代理出国.
生活随笔
收集整理的這篇文章主要介紹了
WSL2添加网络代理,通过宿主机代理出国.
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目標是在WSL2使用安裝在Win10內的V2R來實現出國上網.
WSL2獲取Win10ip
在Win10 -> WSL2這個方向, 是可以直接通過Localhost來訪問的, 但是WSL2 -> Win10這個方向就不能直接訪問Localhost了, 需要指定Win10的ip, 想要查看WSL2中Win10的ip, 只需要查看 resolve.conf
cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.18.176.1
這個172.18.176.1就是在WSL2中Win10的ip.
想要方便獲取這個IP, 可以直接使用下方命令 :
cat /etc/resolv.conf|grep nameserver|awk '{print $2}'
WSL2設置環境變量
由于上面已經可以獲取到Win10的ip, 這個時候就可以很方便的對各種環境變量進行設置, 因為V2R的默認http端口為10809, socks5端口為10808, 所以可以直接設置環境變量 :
export windows_host=`cat /etc/resolv.conf|grep nameserver|awk '{print $2}'`
export ALL_PROXY=socks5://$windows_host:10808
export HTTP_PROXY=$ALL_PROXY
export http_proxy=$ALL_PROXY
export HTTPS_PROXY=$ALL_PROXY
export https_proxy=$ALL_PROXY
git也可以設置為使用代理 :
git config --global proxy.https socks5://$windows_host:10808
如果想要這些環境變量和設置每次啟動終端都生效, 可以在~/.bashrc文件最下方添加上述內容 :
export windows_host=`cat /etc/resolv.conf|grep nameserver|awk '{print $2}'`
export ALL_PROXY=socks5://$windows_host:10808
export HTTP_PROXY=$ALL_PROXY
export http_proxy=$ALL_PROXY
export HTTPS_PROXY=$ALL_PROXY
export https_proxy=$ALL_PROXY
if [ "`git config --global --get proxy.https`" != "socks5://$windows_host:10808" ]; then
git config --global proxy.https socks5://$windows_host:10808
fi
設置V2R允許局域網連接
設置Win10防火墻
這一步是最重要的一步, 也是最坑的一步, 需要在Win10防火墻中允許V2R進行公用和專用網絡的訪問! 只要設置這個就可以了, 不需要調整過其他設置!
找到所有帶V2R的應用的選項, 全勾上就可以了. 在WSL2中輸入curl -vv www.google.com驗證是否正確訪問谷歌.
參考文章: **https://jiayaoo3o.github.io/2020/06/23/記錄一次WSL2的網絡代理配置/ **
總結
以上是生活随笔為你收集整理的WSL2添加网络代理,通过宿主机代理出国.的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 配置之MySQL5Dialect
- 下一篇: 乐尚网络:小程序商城零售行业10大新赋能