配置代理

系统代理配置

大部分应用或文件,会自动检测环境变量http_proxy(用于HTTP代理)、https_proxy(用于HTTPS代理)、ALL_PROXY(通用代理设置,支持SOCKS5格式),如果存在这些环境变量,则会自动使用系统代理。

设置http、https代理:

1
2
export http_proxy=http://127.0.0.1:xxxx
export https_proxy=http://127.0.0.1:xxxx

设置socks5代理:

1
export ALL_PROXY=socks5://[用户名:密码@]代理服务器IP:端口

比如:

1
export ALL_PROXY=socks5://127.0.0.1:xxxx

注意:这样设置的代理只在当前shell生效,如果切换了shell或者断开连接,环境变量都会消失。如果需要使得代理长期生效,参考环境变量章节。

设置好环境变量后,wgetcurlgit等命令和python等工具都会使用该代理进行网络访问。

特定域名或IP不使用代理:

1
export no_proxy='localhost,127.0.0.1,*.example.com'

清空代理:

1
2
unset http_proxy
unset https_proxy

单次命令跳过代理:在执行命令时,临时指定代理变量为空,仅对当前这条命令生效,不影响后续操作

1
http_proxy="" https_proxy="" wget https://example.com

配置git代理

配置代理

配置httphttps代理:

1
2
git config --global http.proxy 'http://代理服务器IP:端口'
git config --global https.proxy 'http://代理服务器IP:端口'

配置socks5代理:(与httphttps相同)

1
2
git config --global http.proxy 'socks5://代理服务器IP:端口'
git config --global https.proxy 'socks5://代理服务器IP:端口'

也可以使用socks5h

1
2
git config --global http.proxy 'socks5h://代理服务器IP;端口'
git config --global https.proxy 'socks5h://代理服务器IP:端口'

socks5socks5h的区别:socks5是本地解析域名(由电脑先解析域名,再将IP发给代理服务器),socks5h是由代理服务器解析域名,可以避免本地解析失败。

使用 git config --global 设置的全局代理配置是持久化的,不受重启系统、更换 Shell或关闭终端的影响,会一直生效。

取消代理

1
2
git config --global --unset http.proxy
git config --global --unset https.proxy

查看当前代理配置

1
2
git config --global --get http.proxy
git config --global --get https.proxy

配置wget代理

通过-e临时设置仅对该条命令生效的环境变量:

1
wget -e 'http_proxy=http://127.0.0.1:xxxx' -e 'https_proxy=http://127.0.0.1:xxxx' 目标URL

配置代理
https://blog.shinebook.net/2025/11/19/Ubuntu/配置代理/
作者
X
发布于
2025年11月19日
许可协议