3

解决 push 到 GitHub 异常 - 无法连接22端口

 1 year ago
source link: https://www.wyr.me/post/742
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在尝试将代码 push 到 GitHub 时,有时候可能会遇到类似于以下的错误信息:

ssh: connect to host xx.xx.xx.xx port 22: Connection timed out

这个错误表明 SSH 连接在尝试通过 22 端口连接到远程服务器时超时。这可能是由于网络环境、防火墙设置或代理配置等原因导致的。为了解决此问题,我们可以尝试将 SSH 连接切换到 443 端口。

修改 SSH 配置以使用端口 443

要将 SSH 连接切换到 443 端口,我们需要在 ~/.ssh/config 文件中添加以下内容:

Host github.com
  Hostname ssh.github.com
  Port 443

这将使你的 SSH 连接通过 443 端口与 ssh.github.com 进行通信,而不是默认的 22 端口。

如果 ~/.ssh/config 文件不存在,可以手动创建一个。确保在添加配置信息后保存并关闭文件。

测试修改后的 SSH 连接

在修改 SSH 配置后,我们需要测试新的连接设置是否有效。在终端中运行以下命令:

ssh -T [email protected]

如果连接成功,你应该能看到类似于以下的提示信息:

Hi <your-username>! You've successfully authenticated, but GitHub does not provide shell access.

这表明你已经成功通过端口 443 进行 SSH 连接。

验证 push 操作

现在我们已经修改了 SSH 配置并测试了新的连接设置,可以尝试再次将代码 push 到 GitHub。在本地仓库中运行以下命令:

git push origin <branch-name>

<branch-name> 替换为你要推送的分支名称。如果一切顺利,你应该能够成功将代码 push 到 GitHub。

总结: 通过修改 SSH 配置并使用 443 端口进行连接,我们成功解决了 "ssh: connect to host xx.xx.xx.xx port 22: Connection timed out" 错误。如果在将来遇到类似问题,可以尝试使用本文提供的方法进行解决。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK