如何为已有Git仓库添加需SSH跳转代理的远程仓库?
问题描述
本地Git仓库已配置origin远程仓库,想要添加另一台名为remote1的机器作为新远程仓库,执行了以下命令:
git remote add remote1 git remote set-url remote1 ssh://sshUser@remote1Address:path/to/.git
但执行git push remote1 branchName时出现连接超时错误:
ssh: connect to host remote1Address port 22: Connection timed out.
由于remote1的22端口关闭,必须通过跳转代理才能SSH访问,.ssh/config配置如下:
Host jumpProxy Hostname jumpProxyAddress User jumpUser Host remote1 Hostname remote1Address User sshUser ProxyJump jumpUser@jumpProxyAddress
推测Git未读取.ssh/config配置,未调用跳转代理,寻求解决办法。
解决办法
修改远程仓库URL为
.ssh/config中的Host别名
把remote1的远程URL改成使用配置里定义的remote1别名,而非直接写IP/域名:git remote set-url remote1 ssh://remote1/path/to/.git这样Git调用SSH时,SSH会自动读取
.ssh/config里的remote1配置,走跳转代理连接。先验证SSH连接是否正常
单独测试SSH连接remote1,确认配置生效:ssh remote1如果能成功登录,说明SSH配置没问题,再执行Git推送即可。
检查Git使用的SSH路径
部分环境中Git可能未使用系统默认SSH,可查看当前Git的SSH命令:git config --get core.sshCommand若输出为空或路径不对,设置为系统默认SSH路径:
# Linux/macOS git config --global core.sshCommand /usr/bin/ssh # Windows git config --global core.sshCommand C:\Windows\System32\OpenSSH\ssh.exe
内容的提问来源于stack exchange,提问作者Naghi
相关产品推荐
相关产品推荐

