Skip to content

Instantly share code, notes, and snippets.

@chenshengzhi
Last active November 8, 2024 08:31
Show Gist options
  • Save chenshengzhi/07e5177b1d97587d5ca0acc0487ad677 to your computer and use it in GitHub Desktop.
Save chenshengzhi/07e5177b1d97587d5ca0acc0487ad677 to your computer and use it in GitHub Desktop.
git ssh 代理设置

仅为 GitHub 设置代理

git 代理

设置 git config --global http.https://git.521000.best.proxy socks5://127.0.0.1:1086
设置完成后, ~/.gitconfig 文件中会增加以下条目:

[http "https://github.com"]
    proxy = socks5://127.0.0.1:1086
ssh 代理

修改 ~/.ssh/config 文件

Host github.com
    User git
    ProxyCommand nc -v -x 127.0.0.1:1086 %h %p
@godwinnnnn
Copy link

$ ssh -T [email protected]
/ usr / bin / bash:第0行:exec:nc:找不到
kex_exchange_identification:连接被远程主机关闭
未知端口65535关闭了连接

不行

好像你的系统里没有nc这个命令

我找了网上很多教程,最后设置成功了,很简单,现在的git bash里面都包含了connect.exe这个软件,位置在bin下,你只要配置一个config可以了 vi ~/.ssh/config

ProxyCommand connect -S 127.0.0.1:10801 -a none %h %p

Host github.com
  User git
  Port 22
  Hostname github.com
  # 注意修改路径为你的路径
  IdentityFile "C:\Users\One\.ssh\id_rsa"
  TCPKeepAlive yes

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  # 注意修改路径为你的路径
  IdentityFile "C:\Users\One\.ssh\id_rsa"
  TCPKeepAlive yes

然后就可以使用ssh,测试方法是 ssh -T [email protected],返回successful之类的就成功了
有效,感谢

@JESVN
Copy link

JESVN commented Sep 3, 2023

有可能会和gitlab有冲突,我是这样配置的就不影响gitlab:

Host github.com
  ProxyCommand connect -S 127.0.0.1:10808 -a none %h %p
  User git
  Port 443
  Hostname ssh.github.com
  # 注意修改路径为你的路径
  IdentityFile ~/.ssh/id_rsa
  TCPKeepAlive yes
 

@FlameKm
Copy link

FlameKm commented Sep 15, 2023

ssh over https提到域名改成ssh.github.com了

Host github.com
Hostname ssh.github.com
Port 443
User git

十分感谢,这个方法有用,看了下给的链接,似乎是使用https的协议进行ssh连接,而刚好受代理服务器影响

@billchen2k
Copy link

帮忙了,之前还以为是 GitHub 的问题

@SheepTAO
Copy link

SheepTAO commented Oct 8, 2023

ssh over https提到域名改成ssh.github.com了

Host github.com
Hostname ssh.github.com
Port 443
User git

感谢,非常有用。

@zhufengning
Copy link

nc: invalid option -- 'x'

@zhufengning
Copy link

nc: invalid option -- 'x'

解决方法是:Looks like you have the "traditional" netcat (netcat-traditional) installed. The -x option is available in the OpenBSD netcat (netcat-openbsd). See also: What are the differences between netcat-traditional and netcat-openbsd? on Ask Ubuntu.

@songyule
Copy link

songyule commented Oct 18, 2023

ssh.github.com:443 seems to be faster. Work on Ubuntu 22.04. 7891 for Clash.

Host github.com
        Hostname ssh.github.com
        Port 443
        User git
        ProxyCommand nc -v -x  127.0.0.1:7891 %h %p

macos14.0 work!!! 😄

@pobing
Copy link

pobing commented Nov 17, 2023

ssh over https提到域名改成ssh.github.com了

Host github.com
Hostname ssh.github.com
Port 443
User git

nice, works for me, thanks !!!

@Deali-Axy
Copy link

$ ssh -T [email protected]
/ usr / bin / bash:第0行:exec:nc:找不到
kex_exchange_identification:连接被远程主机关闭
未知端口65535关闭了连接

不行

好像你的系统里没有nc这个命令

我找了网上很多教程,最后设置成功了,很简单,现在的git bash里面都包含了connect.exe这个软件,位置在bin下,你只要配置一个config可以了 vi ~/.ssh/config

ProxyCommand connect -S 127.0.0.1:10801 -a none %h %p

Host github.com
  User git
  Port 22
  Hostname github.com
  # 注意修改路径为你的路径
  IdentityFile "C:\Users\One\.ssh\id_rsa"
  TCPKeepAlive yes

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  # 注意修改路径为你的路径
  IdentityFile "C:\Users\One\.ssh\id_rsa"
  TCPKeepAlive yes

然后就可以使用ssh,测试方法是 ssh -T [email protected],返回successful之类的就成功了

亲测可用,感谢!

@fastjien
Copy link

fastjien commented Jan 2, 2024

注意走 socks 和 https 是两个设置方案

Host github.com
  User git
  HostName ssh.github.com
  Port 443
  # 走 HTTP 代理, ubuntu 先 apt install -y socat
  ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=1080
  # 走 socks5 代理(如 Shadowsocks)
  # ProxyCommand nc -v -x 127.0.0.1:1080 %h %p

感谢,Mac实测有用!

@H-YWu
Copy link

H-YWu commented Jan 10, 2024

在 Windows 下使用 ProxyCommand connect 设置现在 Powershell 中用 ssh 连接 git 失效,pull 和 push 都失败,命令

ssh -T git@github.com

返回错误:

CreateProcessW failed error:2
posix_spawnp: No such file or directory

但是使用 git bash 进行连接却是正常的,请问这是什么原因呢?以前这个设置在 Powershell 中也能用

@Deali-Axy
Copy link

在 Windows 下使用 ProxyCommand connect 设置现在 Powershell 中用 ssh 连接 git 失效,pull 和 push 都失败,命令

ssh -T git@github.com

返回错误:

CreateProcessW failed error:2
posix_spawnp: No such file or directory

但是使用 git bash 进行连接却是正常的,请问这是什么原因呢?以前这个设置在 Powershell 中也能用

我的配置

Host github.com
  ProxyCommand connect -H 127.0.0.1:7890 -a none %h %p
  User git
  Port 22
  Hostname github.com
  IdentityFile "C:\Users\u\.ssh\id_rsa"
  TCPKeepAlive yes

Host ssh.github.com
  ProxyCommand connect -H 127.0.0.1:7890 -a none %h %p
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\u\.ssh\id_rsa"
  TCPKeepAlive yes

我遇到的问题

GitBash

Windows Powershell

无论输入哪个都提示

CreateProcessW failed error:2
posix_spawnp: No such file or directory

@TeslaChow
Copy link

ssh over https提到域名改成ssh.github.com了

Host github.com
Hostname ssh.github.com
Port 443
User git

使用win11+clash,配置在ssh的config中这样配置有用,在打开或关闭代理都可以使用git

@Deali-Axy
Copy link

经过不断的尝试,我找到找到了属于我的正确配置方法

Host github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

使用 ssh 命令测试没问题,实际使用也正常了

$ ssh -T [email protected]
Hi Deali-Axy! You've successfully authenticated, but GitHub does not provide shell access.

@TeslaChow
Copy link

经过不断的尝试,我找到找到了属于我的正确配置方法

Host github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

使用 ssh 命令测试没问题,实际使用也正常了

$ ssh -T [email protected]
Hi Deali-Axy! You've successfully authenticated, but GitHub does not provide shell access.

我一开始使用
Host github.com
User git
Hostname github.com
IdentityFile "C:\Users\用户名.ssh\id_rsa"
ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p
这个配置,期间还更换S为H,或者更换端口号7890到7891,都不成功,就很奇怪,不知道哪里的问题

@Deali-Axy
Copy link

经过不断的尝试,我找到找到了属于我的正确配置方法

Host github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

使用 ssh 命令测试没问题,实际使用也正常了

$ ssh -T [email protected]
Hi Deali-Axy! You've successfully authenticated, but GitHub does not provide shell access.

我一开始使用 Host github.com User git Hostname github.com IdentityFile "C:\Users\用户名.ssh\id_rsa" ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p 这个配置,期间还更换S为H,或者更换端口号7890到7891,都不成功,就很奇怪,不知道哪里的问题

就是得把 github.com 设置成走ssh.github.com和443端口才行,应该是后来才改的

@rickywu
Copy link

rickywu commented Jan 19, 2024

大多数防火墙规则应允许此操作,但代理服务器可能会干扰。就是你的木第子可能会阻止
只有改成443端口
另外我亲测ProxyCommand connect -H 127.0.0.01:10081 %h %p,这个才有效,改成-S则不行

@xiaotianhu
Copy link

mac 配合clashX的7890 socks5配置:

Host github.com
Hostname ssh.github.com
Port 443
User git
ProxyCommand nc -v -x 127.0.0.1:7890 %h %p

@lulinglingcufe
Copy link

@Deali-Axy 你的方法太好了,我搞好了,谢谢!!!!!!就是这个:
Host github.com
User git
Port 443
Hostname ssh.github.com
IdentityFile "C:\Users\用户名.ssh\id_rsa"
TCPKeepAlive yes
ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

Host ssh.github.com
User git
Port 443
Hostname ssh.github.com
IdentityFile "C:\Users\用户名.ssh\id_rsa"
TCPKeepAlive yes
ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

@migumigu112
Copy link

$ ssh -T [email protected]
/ usr / bin / bash:第0行:exec:nc:找不到
kex_exchange_identification:连接被远程主机关闭
未知端口65535关闭了连接

不行

好像你的系统里没有nc这个命令

我找了网上很多教程,最后设置成功了,很简单,现在的git bash里面都包含了connect.exe这个软件,位置在bin下,你只要配置一个config可以了 vi ~/.ssh/config

ProxyCommand connect -S 127.0.0.1:10801 -a none %h %p

Host github.com
  User git
  Port 22
  Hostname github.com
  # 注意修改路径为你的路径
  IdentityFile "C:\Users\One\.ssh\id_rsa"
  TCPKeepAlive yes

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  # 注意修改路径为你的路径
  IdentityFile "C:\Users\One\.ssh\id_rsa"
  TCPKeepAlive yes

然后就可以使用ssh,测试方法是 ssh -T [email protected],返回successful之类的就成功了

有效,感谢

成功了 感谢 十分感谢

@SLKun
Copy link

SLKun commented Feb 23, 2024

ssh也可以给shell加这个环境变量, nc需要是openbsd的netcat, 而不是gnu的

export GIT_SSH_COMMAND='ssh -o ProxyCommand="nc -X 5 -x 127.0.0.1:1080 %h %p"'

@Iridescent-zhang
Copy link

经过不断的尝试,我找到找到了属于我的正确配置方法

Host github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

使用 ssh 命令测试没问题,实际使用也正常了

$ ssh -T [email protected]
Hi Deali-Axy! You've successfully authenticated, but GitHub does not provide shell access.

you are my hero!

@Iridescent-zhang
Copy link

经过不断的尝试,我找到找到了属于我的正确配置方法

Host github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

使用 ssh 命令测试没问题,实际使用也正常了

$ ssh -T [email protected]
Hi Deali-Axy! You've successfully authenticated, but GitHub does not provide shell access.

我一开始使用 Host github.com User git Hostname github.com IdentityFile "C:\Users\用户名.ssh\id_rsa" ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p 这个配置,期间还更换S为H,或者更换端口号7890到7891,都不成功,就很奇怪,不知道哪里的问题

就是得把 github.com 设置成走ssh.github.com和443端口才行,应该是后来才改的

感觉关键不是ssh.github.com,而是443

@UshioA
Copy link

UshioA commented Apr 13, 2024

为gitlab配置类似的代理:

Host gitlab.com
	User git
	Port 443
	Hostname altssh.gitlab.com
	IdentityFile ~/.ssh/id_ed25519
	IdentitiesOnly yes
	ProxyCommand nc -X connect -x localhost:1080 %h %p

@luoyinong
Copy link

经过不断的尝试,我找到找到了属于我的正确配置方法

Host github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

使用 ssh 命令测试没问题,实际使用也正常了

$ ssh -T [email protected]
Hi Deali-Axy! You've successfully authenticated, but GitHub does not provide shell access.

我一开始使用 Host github.com User git Hostname github.com IdentityFile "C:\Users\用户名.ssh\id_rsa" ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p 这个配置,期间还更换S为H,或者更换端口号7890到7891,都不成功,就很奇怪,不知道哪里的问题

这个有用, 之前一直报
CreateProcessW failed error:2
posix_spawnp: No such file or directory

@evan4C
Copy link

evan4C commented Jul 22, 2024

mac 配合clashX的7890 socks5配置:

Host github.com Hostname ssh.github.com Port 443 User git ProxyCommand nc -v -x 127.0.0.1:7890 %h %p

这个方法对我有用,感谢

@WenHv587
Copy link

经过不断的尝试,我找到找到了属于我的正确配置方法

Host github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Users\用户名\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

使用 ssh 命令测试没问题,实际使用也正常了

$ ssh -T [email protected]
Hi Deali-Axy! You've successfully authenticated, but GitHub does not provide shell access.

这个对我有用。Windows10

@jon9527
Copy link

jon9527 commented Jul 31, 2024

ssh over https提到域名改成ssh.github.com了

Host github.com
Hostname ssh.github.com
Port 443
User git

感谢,非常有用。

确实有用,mac+clash+tun模式成功测试。谢谢

@suyanhj
Copy link

suyanhj commented Oct 8, 2024

ssh -T [email protected]

好用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment