代理
格式
# 設置http代理
export http_proxy=
# 設置https代理
export HTTPS_PROXY=
# 設置ftp代理
export FTP_PROXY=
# 同時設置http、https以及ftp代理
export ALL_PROXY=
在終端臨時生效
# 設置代理,只在當前終端有效
$ export http_proxy=http://<IP>:<PORT>
或是
$ export http_proxy=socks5://127.0.0.1:1080
$ export HTTPS_PROXY=socks5://127.0.0.1:1080
# 取消代理
$ unset http_proxy
$ unset https_proxy
寫入配置文件(如: .bashrc
)永久有效
$ vi ~/.bashrc
# 添加如下內容:
# set proxy
function setproxy() {
export http_proxy=socks5://127.0.0.1:1080
export HTTPS_PROXY=socks5://127.0.0.1:1080
export FTP_PROXY=socks5://127.0.0.1:1080
}
# unset proxy
function unsetproxy() {
unset http_proxy HTTPS_PROXY FTP_PROXY
}
保存退出,執行source ~/.bashrc
使得配置立即生效。或是關閉當前終端,重新打開,在終端中輸入:
# 設置代理
$ setproxy
# 取消代理
$ unsetproxy
socks5和socks5h的區別
In a proxy string, socks5h:// and socks4a:// mean that the hostname is resolved by the SOCKS server. socks5:// and socks4:// mean that the hostname is resolved locally
socks5(本地解析hostname)
$export HTTPS_PROXY=socks5://127.0.0.1:1080
$curl https://www.google.com
curl: (51) SSL: certificate subject name (www.beforeprom.com) does not match target host name 'www.google.com'
socks5h(由socks server解析hostname)
$ export HTTPS_PROXY=socks5h://127.0.0.1:1080
$ curl https://www.google.com
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for." name="description">
....
也就是說socks5適合本地能夠解析目標主機域名(比如github.com)但是訪問速度慢,來提高下載速度
socks5h用與本地不能解析目標主機域名(比如google),由代理服務器解析目標主機域名
curl通過-x
設置代理
curl -x socks5h://127.0.0.1:1080 www.google.com
curl、wget與pac的關系
pac是js文件,curl和wget不能解析js文件。
也就是說,默認情況下,系統代理模式設為pac模式
此時瀏覽器已經可以訪問被牆的網站,但是wget和curl卻不行,必須通過http_proxy
和HTTPS_PROXY
指定代理,才能訪問