解决git nc: connect to 127.0.0.1 port 1080 (tcp) failed

2,266 阅读1分钟

问题

git clone git@github.com:openjdk/jmh.git发现报错了。

完整报错如下:

nc: connect to 127.0.0.1 port 1080 (tcp) failed: Connection refused
kex_exchange_identification: Connection closed by remote host
Connection closed by UNKNOWN port 65535
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

情况只出现在ssh模式,即git地址中是git@github.com,而不是https模式中的https://github.com

解决方法

使用ssh -v git@github.com,输出里会显示对github.com的ssh配置从哪里读取,并且该命令也会被nc: connect to 127.0.0.1 port 1080 (tcp) failed所中断。我的输出如下

OpenSSH_8.6p1, LibreSSL 3.3.6
debug1: Reading configuration data /Users/xxx/.ssh/config
# 从这里读取的配置
debug1: /Users/xxx/.ssh/config line 4: Applying options for github.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: include /etc/ssh/ssh_config.d/* matched no files
debug1: /etc/ssh/ssh_config line 54: Applying options for *
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Executing proxy command: exec nc -v -x 127.0.0.1:1080 github.com 22
debug1: identity file /Users/xxx/.ssh/xxxx type 0
debug1: identity file /Users/xxx/.ssh/xxxx-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.6
nc: connect to 127.0.0.1 port 1080 (tcp) failed: Connection refused
debug1: kex_exchange_identification: banner line 0: error = 0 61
kex_exchange_identification: Connection closed by remote host
Connection closed by UNKNOWN port 65535

再打开/Users/xxx/.ssh/config文件,xxx是我的用户名,这里隐去了。

里面果然有一行对github.com使用127.0.0.1:1080代理的配置,去掉之后,git clone的ssh模式就可以正常工作了。