内网服务器通过SSH反向隧道访问GitHub

2025年8月20日 322点热度

问题背景

服务器网络限制

内网服务器计算资源强大但受安全策略限制,无法直接访问外部网络资源(如GitHub),导致代码运行环境与代码获取环境分离。

传统方案的不足

使用scp/rsync传输代码操作繁琐,且破坏了Git版本管理的连贯性。

解决方案

核心思路

通过SSH反向隧道,将服务器端口(1080)流量转发至本地PC代理端口(12334),使服务器借助本地网络能力访问GitHub。

image.png

建立隧道命令

ssh -N -R 1080:localhost:12334 user@192.168.1.100

参数说明

  • -N:仅建立连接,不执行远程命令
  • -R 1080:localhost:12334:反向端口转发(服务器1080→本地12334)
  • user@192.168.1.100:服务器连接地址

实施步骤

本地测试

  1. 测试服务器连接(需预先配置SSH密钥):
ssh user@192.168.1.100
  1. 验证本地代理连通性
curl -s -o /dev/null -w "%{http_code}" --socks5-hostname localhost:12334 https://github.com

image.png

服务器配置

  1. 检查隧道端口监听
netstat -tln | grep 1080

image.png

  1. 测试代理访问
export ALL_PROXY=socks5://127.0.0.1:1080
curl -s -o /dev/null -w "%{http_code}\n" https://github.com

image.png

  1. 配置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  # 建立带反向隧道的连接

xxs9331

这个人很懒,什么都没留下

文章评论