MacOS修复Github不能通过ssh使用的问题
问题背景
Github以安全为由,推荐以ssh方式替代https,项目遂换成ssh方式,但是由于22端口的连接性问题,经常会无法连接,提示连接超时
1 2 3 4 5 6
| % git push kex_exchange_identification: Connection closed by remote host fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
|
问题排查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| % ssh -Tv git@github.com OpenSSH_8.1p1, LibreSSL 2.7.3 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 47: Applying options for * debug1: Connecting to github.com port 22. debug1: Connection established. debug1: identity file /Users/xiaoxun/.ssh/id_rsa type 0 debug1: identity file /Users/xiaoxun/.ssh/id_rsa-cert type -1 debug1: identity file /Users/xiaoxun/.ssh/id_dsa type -1 debug1: identity file /Users/xiaoxun/.ssh/id_dsa-cert type -1 debug1: identity file /Users/xiaoxun/.ssh/id_ecdsa type -1 debug1: identity file /Users/xiaoxun/.ssh/id_ecdsa-cert type -1 debug1: identity file /Users/xiaoxun/.ssh/id_ed25519 type -1 debug1: identity file /Users/xiaoxun/.ssh/id_ed25519-cert type -1 debug1: identity file /Users/xiaoxun/.ssh/id_xmss type -1 debug1: identity file /Users/xiaoxun/.ssh/id_xmss-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_8.1 kex_exchange_identification: Connection closed by remote host
|
解决办法
既然22端口不能用,那现在就有两种思路:
- 1.设置ssh代理,把github的ssh连接代理到科学通道上;
- 2.换个没被封禁的端口
两个方案都有一个前置条件:你需要有一个稳定的科学上网工具
方案1
参考了macOS 给 Git(Github) 设置代理(HTTP/SSH)提供的方案:
修改~/.ssh/config
文件(不存在则新建):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| # macOS 给 Git(Github) 设置代理(HTTP/SSH) # https://gist.github.com/chuyik/02d0d37a49edc162546441092efae6a1 # 0x00 克隆 repo 的两种方式:https 和 ssh 方式 # https 方式:git clone https://github.com/owner/git.git # ssh 方式:git clone git@github.com:owner/git.git
# 0x01 https 方式克隆的 repo,走 http 或 sock5 代理,任选一个 # 0x0101 http 代理 # git config --global http.proxy "http://127.0.0.1:1087" # git config --global https.proxy "http://127.0.0.1:1087" # 0x0102 sock5 代理 # git config --global http.proxy "socks5://127.0.0.1:1086" # git config --global https.proxy "socks5://127.0.0.1:1086" # 0x0103 取消使用代理 # git config --global --unset http.proxy # git config --global --unset https.proxy
# 0x02 ssh 克隆方式的代理设置,直接在全局设置文件配置,即 ~/.ssh/config 文件 Host github.com HostName github.com User git # 走 HTTP 代理,需要 brew install socat # ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=1087 # 走 socks5 代理(如 Shadowsocks) ProxyCommand nc -v -x 127.0.0.1:1086 %h %p # 走 socks5 代理(如 Shadowsocks),Windows 平台没有 nc 命令 # ProxyCommand connect -S 127.0.0.1:1086 %h %p
|
走http代理需要socat
这个工具,运行brew install socat
命令安装它
方案2
查到有大佬通过github的443端口进行ssh操作的案例,原文github 的22端口无法连接的解决办法
修改~/.ssh/config
文件(不存在则新建):
1 2 3 4
| Host github.com HostName ssh.github.com User yourmail@xx.com Port 443
|
Windows下的解决方案
依然是~/.ssh/config
文件,在前述方案下,添加HostName
同级的配置,如下:
1 2 3
| # 注意修改路径为你的路径 IdentityFile "C:\Users\xiaoxun\.ssh\id_rsa" TCPKeepAlive yes
|