问题描述

Github 可以访问拉取文件一直失败

显示ssh:连接到主机 github. com 端口 22:连接被拒绝

基本环境

代理:Watt Toolkit

系统:Windows 10 专业工作站版

Github SSh:已配置,ssh -T [email protected] 查看是否链接成功

问题触发

尝试拉取一个非空仓库,链接超时,再使用 ssh -T [email protected] 拉取失败

An image to describe post

An image to describe post

关闭代理,重启,再使用命令 ssh -T [email protected] 发现连接成功

An image to describe post

解决方法

方法一:基于 ssh: connect to host github.com port 22: Connection refused 的问题的解决方法

ssh: connect to host github.com port 22: Connection refused显示 ssh:连接到主机 github. com 端口 22:连接被拒绝

原理一

环境

使用命令 ssh -vT [email protected] 打印日志,发现其中一条结果时超时

An image to describe post

初步判断原因 网络延迟特别高 我所在位置在 XJ 距离 GitHub 的服务器很远并且没有 CND 加速,考虑到主要是拉取和推送仓库,通过镜像不一定能成,选择修改本地 hosts 文件的方法,在 B 站 UP 主御风大世界 于2024.11.17发布的视频 全平台、无视生态的隔空投送!超快不限速 feat.Github 热门项目 #7_哔哩哔哩_bilibili 提到的一个 GitHub 项目,作为本方法的主要围绕角色

方法

涉及工具

Github520

Github 地址:https://github.com/521xueweihan/GitHub520

Gitee 地址:https://gitee.com/klmahuaw/GitHub520

An image to describe post

操作方法

项目中也有具体涉及,在这里就不过多涉及了。

需要注意,拉取和推送需要关闭代理,代理和修改 hosts 文件不能共存

关于 PTY allocation request failed on channel 0的问题

ssh -T T 一定要大写

原理二

环境

ssh 的端口是22,有可能是被防火墙给屏蔽了,可以尝试链接 Github 的443端口。

[!注意]
需要注意当 ssh 链接其他网站比如 gitee 可以流畅连接,可以直接跳过本方法

方法

通过在配置 ~/.ssh/config 文件达到修改 ssh 访问 github 端口的目的。当 .ssh 文件下没有 config 文件时自行创建

# 添加下面部分
Host github.com
  Hostname ssh.github.com
  Port 443

修改保存完成以后,通过 Git Bash 输入 ssh -T -p 443 [email protected] 测试和 Github 的网络通信是否正常。

An image to describe post

方法二:配置Git代理

本方法主要因为代理软件只能够加速网络,对命令行加速不起任何作用,需要单独配置

http 代理有两个一个是 socks5 一个是 http ,具体使用那个需要查看代理软件

我使用的时 watt,查看是在网络加速功能区,加速设置,代理设置,二级代理(PS:在这里就需要开题二级代理了)

An image to describe post

查看到代理类型是 Socks5,地址为 127.0.0.1 ,端口为 7890,打开 Git Bash 输入命令

配置 Socks5 代理

git config --global http.proxy socks5 127.0.0.1:7890
git config --global https.proxy socks5 127.0.0.1:7890

配置 http 代理

git config --global http.proxy 127.0.0.1:7890
git config --global https.proxy 127.0.0.1:7890

查看代理命令

git config --global --get http.proxy
git config --global --get https.proxy

取消代理命令

git config --global --unset http.proxy
git config --global --unset https.proxy