---
title: "Mac 上的 ssh: connect to host port 22: Operation timed out · Kilobat 指南"
url: "https://kilobat.app/zh-hans/guides/ssh-port-22-operation-timed-out.html"
description: "Mac 上的 ssh 提示 connect to host port 22: Operation timed out。先确认网络是否屏蔽了 SSH，然后为 GitHub 改用端口 443，或修复 IPv6。"
---

错误信息

# Mac 上出现 ssh: connect to host port 22: Operation timed out

ssh 向端口 22 发出了连接请求，但没有收到应答。在 Mac 上，系统给出的错误文字是“Operation timed out”；Linux 上则是“Connection timed out”。

适用于：macOS 自带的 OpenSSH，所有当前版本。文中的输出已在 macOS 27.2 上的 OpenSSH 10.5 中核实。 · 核查日期：2026年10月29日

## 症状

- “终端”显示 `ssh: connect to host github.com port 22: Operation timed out`。
- 随后 Git 显示 `fatal: Could not read from remote repository.`
- 在其他网络（例如家里的网络）上，SSH 能正常使用。

## 可能的原因

- **网络屏蔽了端口 22。**公司、学校、酒店和公共网络常常屏蔽 SSH。GitHub 表示，防火墙有时会拒绝 SSH 连接。
- **IPv6 路径不通。**Mac 会先尝试 IPv6，而 IPv6 无法使用。
- **VPN 或代理。**流量经过的 VPN 不放行 SSH。
- **服务器宕机或被过滤。**服务器没有在端口 22 上应答。

## 如何检查

1. 单独测试 TCP 端口，超时时间设为 5 秒。

   ```
   nc -vz -G 5 github.com 22
   ```
2. 只用 IPv4 试试。

   ```
   ssh -4 -T git@github.com
   ```
3. 对于 GitHub，测试端口 443 上的 SSH。

   ```
   ssh -T -p 443 git@ssh.github.com
   ```
4. 以详细模式连接。最后几行会显示连接停在哪一步。

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

## 如何修复

1. 如果端口 443 可用，就为 GitHub 使用它。将以下配置块添加到 `~/.ssh/config`。

   ```
   Host github.com
       Hostname ssh.github.com
       Port 443
       User git
   ```
2. 如果只有 IPv4 可用，在 `~/.ssh/config` 中该主机的配置块里添加 `AddressFamily inet`。
3. 作为测试，改用仓库的 HTTPS URL。
4. 如果是你自己的服务器，请网络管理员放行它的 SSH 端口。

## 使用 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.html)。

## 常见问题

**为什么 Mac 显示 Operation timed out，而不是 Connection timed out？**

ssh 打印的是系统对该错误的描述文字。在 macOS 上，ETIMEDOUT 对应的文字是“Operation timed out”。

## 参考资料

- [GitHub 文档：在 HTTPS 端口使用 SSH](https://docs.github.com/en/authentication/troubleshooting-ssh/using-ssh-over-the-https-port)
- ssh(1)、ssh_config(5) 和 nc(1)，macOS 27.2 自带的 man 手册页
- intro(2)，ETIMEDOUT，macOS 27.2 自带的 man 手册页

参考资料核查于 2026年10月29日。man 手册页为 macOS 自带版本；在“终端”中用 `man <name>` 打开。

## 相关指南

- [Mac 上 SSH 和 Git 超时](https://kilobat.app/zh-hans/guides/ssh-git-timeouts.html): SSH 和 Git 需要与服务器之间有可用的 TCP 连接。
- [Mac 上 Git 提示“Could not resolve host: github.com”](https://kilobat.app/zh-hans/guides/git-could-not-resolve-host-github.html): Git 无法获取 github.com 的地址。
- [Mac 上的 IPv6 问题](https://kilobat.app/zh-hans/guides/ipv6-problems-mac.html): 大多数网络会同时为 Mac 分配 IPv4 和 IPv6 地址。
- [Mac 上公司代理证书导致“终端”工具出错](https://kilobat.app/zh-hans/guides/corporate-proxy-certificates.html): 一些公司网络会检查 HTTPS 流量。
