跳转到内容

Proxy 配置

当前局域网代理

当前 Windows 笔记本提供 HTTP/SOCKS5 混合代理:

text
代理地址:192.168.0.4:12450
代理程序:XSUS core
监听地址:0.0.0.0:12450
Linux 服务器:192.168.0.100

已验证 HTTP Proxy 和 SOCKS5 均可用。笔记本必须保持开机,XSUS 必须保持运行;不要将该端口映射到公网。

curl 测试

HTTP/HTTPS 代理:

bash
curl -I \
  --proxy http://192.168.0.4:12450 \
  --connect-timeout 10 \
  https://www.baidu.com/

SOCKS5 代理:

bash
# socks5h 让域名也由代理端解析
curl -I \
  --proxy socks5h://192.168.0.4:12450 \
  --connect-timeout 10 \
  https://www.baidu.com/

Shell 临时代理

bash
export http_proxy="http://192.168.0.4:12450"
export https_proxy="http://192.168.0.4:12450"
export HTTP_PROXY="$http_proxy"
export HTTPS_PROXY="$https_proxy"

# 局域网和公司内网保持直连
export no_proxy="localhost,127.0.0.1,192.168.0.0/24,10.0.0.0/8,172.16.0.0/16"
export NO_PROXY="$no_proxy"

取消:

bash
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy NO_PROXY

Snap 配置代理

Snap/snapd 不一定继承当前 Shell 的代理变量,优先使用 Snap 系统配置:

bash
sudo snap set system \
  proxy.http="http://192.168.0.4:12450" \
  proxy.https="http://192.168.0.4:12450"

即使下载目标是 HTTPS,proxy.https 也写 http://,表示通过 HTTP CONNECT 代理。

查看配置:

bash
sudo snap get system proxy
sudo snap get system proxy.http
sudo snap get system proxy.https

预期:

text
Key          Value
proxy.http   http://192.168.0.4:12450
proxy.https  http://192.168.0.4:12450

重启并验证:

bash
sudo systemctl restart snapd
systemctl is-active snapd
nc -vz -w 5 192.168.0.4 12450
sudo snap debug connectivity
sudo snap refresh

Snap 配置不生效时

为 snapd 添加 systemd 服务代理:

bash
sudo mkdir -p /etc/systemd/system/snapd.service.d
sudo tee /etc/systemd/system/snapd.service.d/proxy.conf >/dev/null <<'EOF'
[Service]
Environment="HTTP_PROXY=http://192.168.0.4:12450"
Environment="HTTPS_PROXY=http://192.168.0.4:12450"
Environment="NO_PROXY=localhost,127.0.0.1,192.168.0.0/24,10.0.0.0/8,172.16.0.0/16"
EOF

sudo systemctl daemon-reload
sudo systemctl restart snapd
systemctl show snapd --property=Environment

优先只使用 snap set system proxy.*;确认不生效时再添加 systemd 配置,避免维护两份设置。

取消 Snap 代理

bash
sudo snap unset system proxy.http
sudo snap unset system proxy.https
sudo systemctl restart snapd

如果配置过 systemd:

bash
sudo rm -f /etc/systemd/system/snapd.service.d/proxy.conf
sudo systemctl daemon-reload
sudo systemctl restart snapd

历史配置示例

  • curl brew配置代理
bash
export http_proxy="http://192.168.1.123:7895"
export https_proxy="http://192.168.1.123:7895"
export ALL_PROXY="socks5://192.168.1.123:7895"

cat > ~/.bash.profile << EOF
export http_proxy="http://192.168.1.123:7897"
export https_proxy="https://192.168.1.123:7897"
export ALL_PROXY="socks5://192.168.1.123:7897"
EOF
  • apt 配置代理
bash
sudo touch  /etc/apt/apt.conf.d/proxy.conf
cat > /etc/apt/apt.conf.d/proxy.conf << EOF
Acquire {
  http::Proxy "http://192.168.1.123:7895";
  https::Proxy "http://192.168.1.123:7895";
}
EOF
  • opkg 配置代理
bash
vi /etc/opkg.conf
# 文件末尾添加。 option http_proxy https://dockerproxy.com

opkg update
  • fedora-dnf 配置代理
bash
vim /etc/dnf.conf
# 文件末尾添加: proxy=http://192.168.1.42:7897
  • docker 配置代理
bash
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo touch /etc/systemd/system/docker.service.d/http-proxy.conf
sudo cat > /etc/systemd/system/docker.service.d/http-proxy.conf << EOF
[Service]
Environment="HTTP_PROXY=http://192.168.1.123:7895"
Environment="HTTPS_PROXY=http://192.168.1.123:7895"
EOF
sudo systemctl show --property=Environment docker
sudo systemctl daemon-reload
sudo systemctl restart docker
  • pip
bash
docker compose exec jupyter-notebook pip install jupyterlab-language-pack-zh-cn -i https://pypi.tuna.tsinghua.edu.cn/simple
  • git
bash
git config --global http.https://github.com.proxy http://192.168.1.123:7895
git config --global https.https://github.com.proxy https://192.168.1.123:7895

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

git config --global -l
  • npm
bash
npm config set proxy http://192.168.1.123:7895
npm config set https-proxy http://192.168.1.123:7895

npm config delete proxy
npm config delete https-proxy

npm config get proxy
npm config get https-proxy

基于 MIT 许可发布