---
title: "Mac 上的“ssh: connect to host github.com port 22: Connection refused”· Kilobat 指南"
url: "https://kilobat.app/zh-hans/guides/ssh-github-port-22-connection-refused"
description: "Mac 上的 git 或 ssh 以 connect to host github.com port 22: Connection refused 停止。网络阻止了 22 端口。请改用 443 端口上的 SSH。"
---

# Mac 上的“ssh: connect to host github.com port 22: Connection refused”

到 22 端口的连接被拒绝，很可能是防火墙或网络策略阻止了 SSH。GitHub 的文档提供了通过 HTTPS 端口使用 SSH 的变通办法。

**让你的智能体来解决**拷贝一条提示词，适用于 Claude Code、Codex 或任何能使用终端的智能体。

我的 Mac 出现了这个问题：Mac 上的“ssh: connect to host github.com port 22: Connection refused”。请阅读 https://kilobat.app/zh-hans/guides/ssh-github-port-22-connection-refused.md，并和我一起逐步处理。如果缺少 Kilobat CLI，请安装：brew install seifip/kilobat/kilobat。请你自己运行指南中的终端和 kilobat 检查，并告诉我发现了什么。列出只有我才能完成的步骤。更改任何设置之前先问我。

适用于：所有当前 macOS 版本上的 git 和 OpenSSH。 · 核查日期：2026年10月29日

## 症状

- `git clone`、`git pull` 或 `git push` 打印出 `ssh: connect to host github.com port 22: Connection refused`。
- 同一个仓库的 HTTPS URL 可能能用。
- 常见于酒店、学校和工作网络。

## 可能的原因

- **防火墙阻止了 22 端口。**有些网络会拒绝 SSH。GitHub 指出，有些防火墙会完全拒绝 SSH 连接。
- **代理或 VPN。**VPN 或代理可能会阻止出站的 22 端口。
- **主机名不对。**远程 URL 中的输入错误指向了一个没有 SSH 的主机。
- **本地安全类 App。**安全类 App 可能会阻止 SSH。

## 如何检查

1. 显示远程 URL。

   ```
   git remote -v
   ```
2. 测试 22 端口。

   ```
   nc -vz github.com 22
   ```
3. 测试 443 端口上的 SSH。

   ```
   ssh -T -p 443 git@ssh.github.com
   ```
4. 查看这次失败的 SSH 日志。

   ```
   ssh -vT git@github.com
   ```

## 如何修复

1. 把这段内容添加到 `~/.ssh/config`。它会让 GitHub 的流量走 443 端口。

   ```
   Host github.com
       Hostname ssh.github.com
       Port 443
       User git
   ```
2. 再测试一次。

   ```
   ssh -T git@github.com
   ```
3. 或者把远程改为 HTTPS。

   ```
   git remote set-url origin https://github.com/OWNER/REPO.git
   ```
4. 如果 443 端口也失败，请暂时关闭 VPN 测试一下，之后再打开。

## 使用 Kilobat

1. Kilobat 的连接诊断会逐层测试这个地址：网络、代理、DNS、规则、IPv4、IPv6、TLS 和服务器，并指出失败的那一层。

   ```
   kilobat why github.com
   ```
2. Kilobat 的这些检查都是免费的。AI 智能体可以通过 MCP 用 `diagnose_connection` 和 `diagnose_network` 运行它们。请参阅[找出某个网站或 API 连不上的原因](https://kilobat.app/zh-hans/guides/recipes/why-cant-it-connect)。

## 常见问题

**成功的测试应该打印什么？**

GitHub 说明，你会看到“Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.”

**这与“Operation timed out”有什么不同？**

这里连接是立即被拒绝的。超时则表示没有任何回复。参见[Mac 上的 ssh: connect to host port 22: Operation timed out](https://kilobat.app/zh-hans/guides/ssh-port-22-operation-timed-out)。

## 参考资料

- [GitHub 文档：在 HTTPS 端口使用 SSH](https://docs.github.com/en/authentication/troubleshooting-ssh/using-ssh-over-the-https-port)

核查日期：2026 年 10 月 29 日。

## 相关指南

- [Mac 上出现 ssh: connect to host port 22: Operation timed out](https://kilobat.app/zh-hans/guides/ssh-port-22-operation-timed-out): ssh 向端口 22 发送了连接请求，但没有收到应答。
- [Mac 上 SSH 和 Git 超时](https://kilobat.app/zh-hans/guides/ssh-git-timeouts): SSH 和 Git 需要与服务器之间有可用的 TCP 连接。
- [Mac 上 Git 提示“Could not resolve host: github.com”](https://kilobat.app/zh-hans/guides/git-could-not-resolve-host-github): Git 无法获取 github.com 的地址。
- [Mac 上 Chrome 中的 ERR_CONNECTION_REFUSED](https://kilobat.app/zh-hans/guides/err-connection-refused): ERR_CONNECTION_REFUSED 是 Chrome 的网络错误。
