git通过ssh代理连接github(gitee不支持),并更改端口

需求

git clone github/gitee远程仓库,使用ssh协议,并且走本地的http代理(端口3128)。

运行环境是Ubuntu 20.04。

github使用ssh代理的方案

修改~/.ssh/config文件:

# github.com
Host github.com
    Hostname ssh.github.com
    User git
    Port 443
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa  # id_rsa改成你的ssh密钥的文件名
    ProxyCommand nc -X connect -x 127.0.0.1:3128 %h %p  # 127.0.0.1:3128改成你的代理的ip:port
    ServerAliveInterval 20

Hostname ssh.github.comProxyCommand nc -X connect -x 127.0.0.1:3128 %h %p 定义对服务器 github.com 的 ssh 调用应该重新路由到端口 3128 上的代理服务器 127.0.0.1,并且服务器不应该是 github.com 而是更改为ssh.github.com。这是 github 允许使用 git 或 ssh 协议通过 https(端口 443)连接到服务器(That is the server where github allows you to use the git or ssh protocol to connect to over https (port 443).)。

nc/usr/bin/nc是实用程序 Netcat,它为我们完成更改主机名和端口号的工作。在一些Linux服务器上,是默认安装的。

ServerAliveInterval 20通过每 20 秒发送一个数据包来确保连接保持活动状态,以防止“管道破裂”。用户 git 确保您不会以本地 Linux 用户身份连接到 github.com,而是以用户 git 身份连接。

注意,以上是走了代理,如果不需要走代理,只需要如下配置:

# github.com
Host github.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa  # id_rsa改成你的ssh密钥的文件名

gitee无法实现ssh代理

仿照github的配置

# gitee.com
Host gitee.com
    Hostname gitee.com
    User git
    Port 443
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa
    ProxyCommand nc -X connect -x 127.0.0.1:3128 %h %p
    ServerAliveInterval 20

无法连接,使用ssh -Tv gitee.com进行debug,返回400 Bad Request

> OpenSSH_8.2p1 Ubuntu-4ubuntu0.3, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /home/ljw/.ssh/config
debug1: /home/ljw/.ssh/config line 10: Applying options for gitee.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Executing proxy command: exec nc -X connect -x 127.0.0.1:3128 gitee.com 443
debug1: identity file /home/ljw/.ssh/id_rsa type 0
debug1: identity file /home/ljw/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.3
debug1: kex_exchange_identification: banner line 0: HTTP/1.1 400 Bad Request
debug1: kex_exchange_identification: banner line 1: Server: stgw/1.3.12_1.13.5
debug1: kex_exchange_identification: banner line 2: Date: Tue, 20 Jun 2023 09:44:28 GMT
debug1: kex_exchange_identification: banner line 3: Content-Type: text/html
debug1: kex_exchange_identification: banner line 4: Content-Length: 179
debug1: kex_exchange_identification: banner line 5: Connection: close
debug1: kex_exchange_identification: banner line 6:
debug1: kex_exchange_identification: banner line 7: <html>
debug1: kex_exchange_identification: banner line 8: <head><title>400 Bad Request</title></head>
debug1: kex_exchange_identification: banner line 9: <body bgcolor="white">
debug1: kex_exchange_identification: banner line 10: <center><h1>400 Bad Request</h1></center>
debug1: kex_exchange_identification: banner line 11: <hr><center>stgw/1.3.12_1.13.5</center>
debug1: kex_exchange_identification: banner line 12: </body>
debug1: kex_exchange_identification: banner line 13: </html>

原因是gitee不支持443端口的ssh服务。

官方issue查看,2年前就有人提问(https://gitee.com/oschina/git-osc/issues/I38LRU
在这里插入图片描述

但现在都还不支持非22端口的ssh服务:
在这里插入图片描述

gitee的暂时解决方案

使用https替代,更改git/ssh地址为https地址。

git config --global url."https://".insteadof "git://"
git config --global url."https://github.com/".insteadof "git@github.com:"

(可选)记住git账号密码
git https记住密码的方法 https://blog.csdn.net/AV_woaijava/article/details/100887704

git config --global credential.helper store

会在~/.gitconfig文件中生成下面的配置。

[credential]
	helper = store

登录过后的账号密码,会记录在~/.git-credentials文件中

格式:
协议://username:password@git域名

如:
https://nefu_ljw:xxx@gitee.com

参考

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nefu-ljw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值