修复 Clash 环境下 GitHub SSH Clone 连接被关闭的问题
本文最后更新于 2 天前
前言
在中国网络环境下访问 GitHub,很多人会使用 Clash、Mihomo、Clash Verge 或 Clash for Windows 这类代理工具。
这种方式通常可以解决 GitHub 网页访问慢、资源加载失败、仓库页面打不开等问题。但是,网页能打开并不代表 Git 命令一定能正常使用。尤其是使用 SSH 克隆仓库时,经常会遇到连接失败的问题。
例如执行:
git clone [email protected]:Seazer-x/hexo-blog.git可能会出现如下错误:
Cloning into 'hexo-blog'...
Connection closed by 198.18.0.65 port 22
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.这类错误容易被误判为 GitHub 仓库权限问题,但实际关键并不在最后两行,而在这一行:
Connection closed by 198.18.0.65 port 22这篇文章记录该问题的原因和解决方案。
问题现象
当前环境大致如下:
- 位于中国网络环境;
- 使用 Clash 访问 GitHub 网页;
- 浏览器可以正常打开 GitHub;
- 使用 SSH 地址执行
git clone失败; - 报错中出现
198.18.x.x和port 22。
典型错误如下:
Connection closed by 198.18.0.65 port 22
fatal: Could not read from remote repository.其中最重要的判断点是:
198.18.0.65这个地址通常不是 GitHub 的真实公网地址,而是 Clash fake-ip 模式下生成的虚拟地址。
原因分析
GitHub 网页访问和 Git SSH 访问不是一回事
访问 GitHub 网页时,浏览器主要使用的是:
HTTPS 443而使用下面这种 SSH 地址克隆仓库时:
[email protected]:Seazer-x/hexo-blog.git默认使用的是:
github.com:22也就是说,浏览器访问 GitHub 成功,只能说明 HTTPS 443 流量基本正常,并不能说明 SSH 22 端口也正常。
在复杂网络环境下,SSH 22 端口更容易出现连接被阻断、连接被重置、超时或被代理工具错误接管的问题。
198.18.x.x 通常来自 fake-ip
198.18.0.0/15 是特殊用途地址段,常被一些代理工具用于 fake-ip 地址池。Clash 开启 fake-ip 模式后,域名解析结果可能会被替换成类似下面的地址:
198.18.0.65这类地址并不是 GitHub 的真实服务器地址。它的作用是让代理工具能够接管连接,并根据规则决定流量如何转发。
因此,当 SSH 报错中出现:
Connection closed by 198.18.0.65 port 22基本可以判断:当前 SSH 请求被 Clash 的 fake-ip / DNS / TUN 网络栈影响了,而且默认 SSH 22 端口没有正常连通。
解决思路
最稳定的方案不是反复修改 GitHub SSH Key,也不是盲目重装 Git,而是:
将 GitHub SSH 从默认的 22 端口改为 GitHub 官方支持的 443 端口。
GitHub 支持通过以下地址进行 SSH 连接:
ssh.github.com:443这样可以绕过很多网络环境中对 SSH 22 端口的限制。
修改 SSH 配置
Windows
Windows 下 SSH 配置文件一般位于:
C:\Users\你的用户名\.ssh\config可以通过命令打开:
notepad %USERPROFILE%\.ssh\config如果 config 文件不存在,直接新建即可。
macOS / Linux
macOS 和 Linux 下 SSH 配置文件一般位于:
~/.ssh/config可以使用以下命令编辑:
vim ~/.ssh/config或者:
nano ~/.ssh/config写入 GitHub SSH 443 配置
在 SSH 配置文件中加入以下内容:
Host github.com
HostName ssh.github.com
User git
Port 443
IdentitiesOnly yes如果你明确使用的是 id_ed25519 私钥,建议写成:
Host github.com
HostName ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes如果你使用的是 RSA 私钥,则可以写成:
Host github.com
HostName ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes这段配置的含义是:当你访问 [email protected] 时,SSH 实际连接的是:
ssh.github.com:443而不是默认的:
github.com:22测试 SSH 连接
配置完成后,执行:
ssh -T [email protected]如果配置正确,通常会看到类似输出:
Hi Seazer-x! You've successfully authenticated, but GitHub does not provide shell access.这不是错误。
这句话表示 SSH 认证已经成功,只是 GitHub 不提供普通 shell 登录能力。对于 Git clone、pull、push 来说,这个状态是正常的。
验证是否真正走了 443 端口
如果需要进一步确认 SSH 配置是否生效,可以执行:
ssh -vT [email protected]重点查看输出中是否出现:
Connecting to ssh.github.com port 443如果看到的是:
Connecting to github.com port 22说明 SSH 配置没有生效。
如果看到的是:
Connecting to 198.18.0.65 port 22说明 SSH 仍然在走默认的 22 端口,并且还在受到 Clash fake-ip / DNS 接管的影响。
重新克隆仓库
SSH 测试成功后,再重新执行:
git clone [email protected]:Seazer-x/hexo-blog.git正常情况下即可成功克隆。
Clash 规则建议
如果你使用 Clash、Mihomo、Clash Verge 或 Clash for Windows,建议为 GitHub 相关域名添加明确规则。
示例:
rules:
- DOMAIN,github.com,PROXY
- DOMAIN,ssh.github.com,PROXY
- DOMAIN-SUFFIX,github.com,PROXY
- DOMAIN-SUFFIX,githubusercontent.com,PROXY
- DOMAIN-SUFFIX,githubassets.com,PROXY其中 PROXY 需要替换成你 Clash 配置里的代理组名称。
例如你的代理组名称是:
🚀 节点选择则应该写成:
rules:
- DOMAIN,github.com,🚀 节点选择
- DOMAIN,ssh.github.com,🚀 节点选择
- DOMAIN-SUFFIX,github.com,🚀 节点选择
- DOMAIN-SUFFIX,githubusercontent.com,🚀 节点选择
- DOMAIN-SUFFIX,githubassets.com,🚀 节点选择fake-ip 配置建议
如果 SSH 日志里出现了 198.18.x.x,可以考虑将 GitHub 相关域名加入 fake-ip 过滤规则。
示例:
fake-ip-filter:
- github.com
- '*.github.com'
- ssh.github.com修改后需要重启 Clash。
然后刷新系统 DNS 缓存。
Windows
ipconfig /flushdnsmacOS
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder如果没有开启 TUN 模式
如果 Clash 只是开启了系统代理,而没有开启 TUN 模式,那么浏览器可以访问 GitHub,但 ssh 命令不一定会自动走代理。
这种情况下有两个选择。
第一,开启 Clash 的 TUN 模式,然后继续使用前面的 ssh.github.com:443 配置。
这是更推荐的方案。
第二,让 SSH 显式走 Clash 的 SOCKS5 代理。假设 Clash 的 SOCKS5 端口是:
127.0.0.1:7890可以配置:
Host github.com
HostName ssh.github.com
User git
Port 443
ProxyCommand nc -X 5 -x 127.0.0.1:7890 %h %p
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes不过 Windows 默认不一定自带 nc,所以该方案不如 TUN 模式通用。
更推荐的组合是:
Clash TUN 模式 + GitHub SSH 443 端口临时方案:使用 HTTPS 克隆
如果只是临时拉取代码,也可以直接使用 HTTPS:
git clone https://github.com/Seazer-x/hexo-blog.git这个方案适合临时下载公开仓库。
如果后续需要频繁执行 push、pull,或者需要操作私有仓库,长期还是建议修好 SSH 配置。
如何判断是否真的是权限问题
只有当下面命令能够连接到 GitHub,但返回 Permission denied (publickey). 时,才应该优先检查 SSH Key:
ssh -T [email protected]如果返回:
Permission denied (publickey).再检查本地公钥:
cat ~/.ssh/id_ed25519.pub然后确认该公钥是否已经添加到 GitHub:
GitHub -> Settings -> SSH and GPG keys但如果错误仍然是:
Connection closed by 198.18.x.x port 22那就不要优先怀疑仓库权限,而应该先处理代理、DNS、fake-ip 和 SSH 端口问题。
最终推荐配置
在中国网络环境下,如果使用 Clash 访问 GitHub,推荐使用下面这份最小稳定配置:
Host github.com
HostName ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes然后执行:
ssh -T [email protected]确认认证成功后,再执行:
git clone [email protected]:Seazer-x/hexo-blog.git总结
这个问题的重点不是:
Please make sure you have the correct access rights而是:
Connection closed by 198.18.0.65 port 22它说明 SSH 请求没有正常连接到 GitHub 的 SSH 服务,而是受到了 Clash fake-ip、DNS、TUN 或默认 SSH 22 端口的影响。
解决这个问题的核心方法是:
GitHub SSH 改走 ssh.github.com:443也就是在 ~/.ssh/config 中配置:
Host github.com
HostName ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes这个方案清晰、稳定、可长期使用,适合在中国网络环境下通过 Clash 访问 GitHub 并进行 Git SSH 操作。




















