问题背景
服务器网络限制
内网服务器计算资源强大但受安全策略限制,无法直接访问外部网络资源(如GitHub),导致代码运行环境与代码获取环境分离。
传统方案的不足
使用scp
/rsync
传输代码操作繁琐,且破坏了Git版本管理的连贯性。
解决方案
核心思路
通过SSH反向隧道,将服务器端口(1080)流量转发至本地PC代理端口(12334),使服务器借助本地网络能力访问GitHub。
建立隧道命令
ssh -N -R 1080:localhost:12334 user@192.168.1.100
参数说明:
-N
:仅建立连接,不执行远程命令-R 1080:localhost:12334
:反向端口转发(服务器1080→本地12334)user@192.168.1.100
:服务器连接地址
实施步骤
本地测试
- 测试服务器连接(需预先配置SSH密钥):
ssh user@192.168.1.100
- 验证本地代理连通性:
curl -s -o /dev/null -w "%{http_code}" --socks5-hostname localhost:12334 https://github.com
服务器配置
- 检查隧道端口监听:
netstat -tln | grep 1080
- 测试代理访问:
export ALL_PROXY=socks5://127.0.0.1:1080
curl -s -o /dev/null -w "%{http_code}\n" https://github.com
- 配置Git代理(测试成功后):
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080
SSH客户端配置
将以下配置添加到~/.ssh/config
文件中,简化连接命令:
Host 4090
HostName 10.111.119.10
User shiwenlong
Port 22
IdentityFile ~/.ssh/id_rsa
ServerAliveInterval 60
ServerAliveCountMax 3
TCPKeepAlive yes
RemoteForward 127.0.0.1:1080 127.0.0.1:12334
ExitOnForwardFailure yes
配置后使用方式:
ssh -N 4090 # 建立带反向隧道的连接
文章评论