1. 原理
本地电脑已经开启代理,例如 Clash 的端口是7890。通过 SSH 反向端口转发,把本地代理转发到服务器:
服务器 127.0.0.1:8888 → 本地电脑 127.0.0.1:7890
之后服务器访问 127.0.0.1:8888,就等于使用本地电脑的代理。
2. 建立转发
在本地电脑终端执行:
ssh -R 8888:127.0.0.1:7890 用户名@服务器IP
如果服务器 SSH 端口不是 22,例如是 2222:
ssh -p 2222 -R 8888:127.0.0.1:7890 用户名@服务器IP
连接成功后,会进入服务器终端。
注意:这个 SSH 窗口不能关闭。关闭后,代理转发会失效。
3. 在服务器测试代理
在服务器上执行:
curl -I --proxy http://127.0.0.1:8888 https://github.com
如果返回 HTTP/1.1 200 OK 或 HTTP/2 200,说明代理转发成功。
4. 服务器使用代理
在服务器上执行:
export http_proxy="http://127.0.0.1:8888"
export https_proxy="http://127.0.0.1:8888"
export HTTP_PROXY="http://127.0.0.1:8888"
export HTTPS_PROXY="http://127.0.0.1:8888"
配置 Git 使用代理:
git config --global http.proxy http://127.0.0.1:8888
git config --global https.proxy http://127.0.0.1:8888
测试 GitHub:
git ls-remote https://github.com/cobalamin/pysepm.git
如果能输出 commit 信息,说明 Git 可以正常访问 GitHub。
5. 退出代理
取消 Git 代理:
git config --global --unset http.proxy
git config --global --unset https.proxy
取消当前终端代理:
unset http_proxy
unset https_proxy
unset HTTP_PROXY
unset HTTPS_PROXY
最后关闭本地 SSH 窗口即可。
6. 常用命令示例
安装 GitHub 依赖:
pip install "git+https://github.com/cobalamin/pysepm.git@defc47219af705a7052d382717f9b34072e8fcc4#egg=pysepm"
下载 GitHub 文件:
wget https://github.com/cobalamin/pysepm/archive/defc47219af705a7052d382717f9b34072e8fcc4.zip





