0


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

文章目录

需求

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

运行环境是Ubuntu 20.04。

github使用ssh代理的方案

修改

  1. ~/.ssh/config

文件:

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

  1. ProxyCommand 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).)。

  1. nc

  1. /usr/bin/nc

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

  1. ServerAliveInterval 20

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

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

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

gitee无法实现ssh代理

仿照github的配置

  1. # gitee.com
  2. Host gitee.com
  3. Hostname gitee.com
  4. User git
  5. Port 443
  6. PreferredAuthentications publickey
  7. IdentityFile ~/.ssh/id_rsa
  8. ProxyCommand nc -X connect -x 127.0.0.1:3128 %h %p
  9. ServerAliveInterval 20

无法连接,使用

  1. ssh -Tv gitee.com

进行debug,返回

  1. 400 Bad Request

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

  1. 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

  1. git config --global credential.helper store

会在

  1. ~/.gitconfig

文件中生成下面的配置。

  1. [credential]
  2. helper = store

登录过后的账号密码,会记录在

  1. ~/.git-credentials

文件中

  1. 格式:
  2. 协议://username:password@git域名
  3. 如:
  4. https://nefu_ljw:xxx@gitee.com

参考

标签: git github ssh

本文转载自: https://blog.csdn.net/ljw_study_in_CSDN/article/details/131312147
版权归原作者 nefu-ljw 所有, 如有侵权,请联系我们删除。

“git通过ssh代理连接github(gitee不支持),并更改端口”的评论:

还没有评论