## 问题描述 在使用 Git 进行代码推送时,遇到了 SSH 连接 GitHub 22 端口超时的问题: ```bash ssh: connect to host github.com port 22: Connection timed out fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. ``` 通过 `ping github.com` 测试发现网络连接正常,说明问题出现在 SSH 的默认 22 端口上。 ## 解决方案 ### 1. 测试 SSH 连接 首先测试与 GitHub 的 SSH 连接: ```bash ssh -T git@github.com ``` 如果出现连接超时,说明无法通过默认 22 端口与 GitHub 进行 SSH 连接。 ### 2. 使用 443 端口测试连接 GitHub 提供了通过 443 端口进行 SSH 连接的替代方案: ```bash ssh -T -p 443 git@ssh.github.com ``` 如果看到类似以下信息,说明通过 443 端口连接成功: ``` Hi username! You've successfully authenticated, but GitHub does not provide shell access. ``` ### 3. 修改 SSH 配置文件 在用户目录下的 `.ssh` 文件夹中创建或编辑 `config` 文件: **Windows 路径:** `C:\Users\\.ssh\config` 添加以下配置内容: ``` # GitHub SSH over HTTPS Host github.com Hostname ssh.github.com Port 443 ``` ### 4. 验证配置 再次测试 SSH 连接: ```bash ssh -T git@github.com ``` 如果配置正确,应该能看到成功的身份验证信息。 ### 5. 正常使用 Git 命令 现在可以正常使用 Git 命令进行代码推送: ```bash git push ``` ## 原理说明 这个解决方案的核心是将 SSH 连接从默认的 22 端口切换到 443 端口(HTTPS 端口)。GitHub 在 `ssh.github.com` 域名上提供了通过 443 端口的 SSH 服务,这样可以绕过某些网络环境对 22 端口的限制。 通过修改 SSH 配置文件,我们告诉 SSH 客户端: - 当连接 `github.com` 时 - 实际连接到 `ssh.github.com` - 使用 443 端口而不是默认的 22 端口 ## 总结 这种方法适用于以下场景: - 公司网络或防火墙阻止了 22 端口 - ISP 限制了 SSH 默认端口 - 其他网络环境导致的 22 端口连接问题 配置完成后,所有的 Git 操作都会自动使用 443 端口,无需每次手动指定端口号。 Loading... ## 问题描述 在使用 Git 进行代码推送时,遇到了 SSH 连接 GitHub 22 端口超时的问题: ```bash ssh: connect to host github.com port 22: Connection timed out fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. ``` 通过 `ping github.com` 测试发现网络连接正常,说明问题出现在 SSH 的默认 22 端口上。 ## 解决方案 ### 1. 测试 SSH 连接 首先测试与 GitHub 的 SSH 连接: ```bash ssh -T git@github.com ``` 如果出现连接超时,说明无法通过默认 22 端口与 GitHub 进行 SSH 连接。 ### 2. 使用 443 端口测试连接 GitHub 提供了通过 443 端口进行 SSH 连接的替代方案: ```bash ssh -T -p 443 git@ssh.github.com ``` 如果看到类似以下信息,说明通过 443 端口连接成功: ``` Hi username! You've successfully authenticated, but GitHub does not provide shell access. ``` ### 3. 修改 SSH 配置文件 在用户目录下的 `.ssh` 文件夹中创建或编辑 `config` 文件: **Windows 路径:** `C:\Users\<username>\.ssh\config` 添加以下配置内容: ``` # GitHub SSH over HTTPS Host github.com Hostname ssh.github.com Port 443 ``` ### 4. 验证配置 再次测试 SSH 连接: ```bash ssh -T git@github.com ``` 如果配置正确,应该能看到成功的身份验证信息。 ### 5. 正常使用 Git 命令 现在可以正常使用 Git 命令进行代码推送: ```bash git push ``` ## 原理说明 这个解决方案的核心是将 SSH 连接从默认的 22 端口切换到 443 端口(HTTPS 端口)。GitHub 在 `ssh.github.com` 域名上提供了通过 443 端口的 SSH 服务,这样可以绕过某些网络环境对 22 端口的限制。 通过修改 SSH 配置文件,我们告诉 SSH 客户端: - 当连接 `github.com` 时 - 实际连接到 `ssh.github.com` - 使用 443 端口而不是默认的 22 端口 ## 总结 这种方法适用于以下场景: - 公司网络或防火墙阻止了 22 端口 - ISP 限制了 SSH 默认端口 - 其他网络环境导致的 22 端口连接问题 配置完成后,所有的 Git 操作都会自动使用 443 端口,无需每次手动指定端口号。 最后修改:2026 年 05 月 26 日 © 允许规范转载 打赏 赞赏作者 赞 4 如果觉得我的文章对你有用,请随意赞赏