文章目录

需求github使用ssh代理的方案gitee无法实现ssh代理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.com和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).)。

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:

debug1: kex_exchange_identification: banner line 8: 400 Bad Request

debug1: kex_exchange_identification: banner line 9:

debug1: kex_exchange_identification: banner line 10:

400 Bad Request

debug1: kex_exchange_identification: banner line 11:


stgw/1.3.12_1.13.5

debug1: kex_exchange_identification: banner line 12:

debug1: kex_exchange_identification: banner line 13:

原因是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

参考

https://technology.amis.nl/platform/getting-started-with-git-behind-a-company-proxy/https://stackoverflow.com/a/57133963https://adangel.org/2020/10/15/github-behind-proxy/

精彩链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: