需求
git clone github/gitee遠程倉庫,使用ssh協(xié)議,并且走本地的http代理(端口3128)。
運行環(huán)境是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
定義對服務(wù)器 github.com 的 ssh 調(diào)用應(yīng)該重新路由到端口 3128 上的代理服務(wù)器 127.0.0.1,并且服務(wù)器不應(yīng)該是 github.com 而是更改為ssh.github.com。這是 github 允許使用 git 或 ssh 協(xié)議通過 https(端口 443)連接到服務(wù)器(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服務(wù)器上,是默認(rèn)安裝的。
ServerAliveInterval 20
通過每 20 秒發(fā)送一個數(shù)據(jù)包來確保連接保持活動狀態(tài),以防止“管道破裂”。用戶 git 確保您不會以本地 Linux 用戶身份連接到 github.com,而是以用戶 git 身份連接。
注意,以上是走了代理,如果不需要走代理,只需要如下配置:
# github.com
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa # id_rsa改成你的ssh密鑰的文件名
gitee無法實現(xiàn)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服務(wù)。
去官方issue查看,2年前就有人提問(https://gitee.com/oschina/git-osc/issues/I38LRU)
但現(xiàn)在都還不支持非22端口的ssh服務(wù):
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
文件中生成下面的配置。文章來源:http://www.zghlxwxcb.cn/news/detail-581337.html
[credential]
helper = store
登錄過后的賬號密碼,會記錄在~/.git-credentials
文件中文章來源地址http://www.zghlxwxcb.cn/news/detail-581337.html
格式:
協(xié)議://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/57133963
- https://adangel.org/2020/10/15/github-behind-proxy/
到了這里,關(guān)于git通過ssh代理連接github(gitee不支持),并更改端口的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!