步步为营 —— Github Connection refused 分层诊断

对GitHub连接问题的深度解决方案总结

涵盖从ssh: connect to host github.com port 22: Connection refusedssh.github.com:443密钥验证失败的全流程分析与解决策略:


🔧 ​一、问题根源分析

  1. 端口封锁(最常见原因)​

    • 企业防火墙/ISP常封锁SSH默认端口22,导致Connection refused
    • 解决方案 :切换到HTTPS端口443(极少被封锁),通过SSH over HTTPS绕过限制。
bash 复制代码
PS D:\desk\code\flutter\getx_demo> git push
ssh: connect to host github.com port 22: Connection refused
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
  1. 主机密钥冲突

    • 切换端口后,新域名ssh.github.com与旧记录github.com密钥不匹配,触发can't be established警告。
    • 原因known_hosts文件中存在旧端口密钥记录,与新连接冲突。
  • "host key verification failed":主机密钥冲突(服务器指纹验证)
  • "Permission denied (publickey)":用户私钥验证失败

🛠️ ​二、核心解决方案

graph TD A["git push 报错"] --> B{"端口22是否可用?"} B -->|否| C["切换为 ssh.github.com:443"] B -->|是| D{"密钥验证通过?"} C -->|切换后连接| D D -->|否| E["清除 known_hosts 再连接"] D -->|是| F["连接成功"] E -->|重试验证| D

步骤1:强制SSH走443端口

  1. 创建/修改SSH配置文件(~/.ssh/config):

    config 复制代码
    Host github.com
      Hostname ssh.github.com  # 关键:使用GitHub备用域名
      Port 443                 # 切换到443端口
      User git
      IdentityFile ~/.ssh/id_rsa  # 指定私钥路径

    作用 ​:所有git@github.com请求自动重定向到ssh.github.com:443

  2. 权限设置(Linux/macOS必做)​​:

    bash 复制代码
    chmod 600 ~/.ssh/config  # 确保文件权限安全

    windows:

步骤2:修复密钥验证失败

  1. 清除冲突的旧密钥记录​:

    bash 复制代码
    bash
    复制
    ssh-keygen -R ssh.github.com  # 删除冲突记录
  2. 手动更新known_hosts文件​:

    • 打开C:\Users\zzy.ssh\known_hosts(Windows)或~/.ssh/known_hosts(macOS/Linux)。
    • 删除第一行 (含github.com的记录)。
    • 重新连接自动生成新记录:ssh -T git@github.com → 输入yes接受新密钥。
  3. 验证指纹合法性(安全必做)​ ​:

    对比GitHub官方ED25519指纹:SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU,防止中间人攻击。


⚡️ ​三、扩展解决方案

场景1:企业网络严格封锁

  • 代理配置​(适用于企业代理环境):

    config 复制代码
    Host github.com
      ProxyCommand connect -H <代理IP:端口> %h %p
  • 防火墙例外​(Windows管理员权限):

    bash 复制代码
    netsh advfirewall firewall add rule name="GitHub_SSH" dir=in action=allow protocol=TCP localport=443

场景2:DNS污染/解析失败

  1. 修改Hosts文件 ​(C:\Windows\System32\drivers\etc\hosts):

    markdown 复制代码
    140.82.114.36 ssh.github.com   # GitHub SSH服务IP
    140.82.113.4 github.com        # GitHub主站IP
  2. 刷新DNS缓存​:

    bash 复制代码
    ipconfig /flushdns  # Windows
    sudo dscacheutil -flushcache # macOS

场景3:多账号密钥冲突

~/.ssh/config中为不同账号分配密钥:

config 复制代码
# 个人账号
Host github.com
  IdentityFile ~/.ssh/id_rsa_personal

# 工作账号
Host github-work
  Hostname github.com
  IdentityFile ~/.ssh/id_rsa_work

克隆时使用:git clone git@github-work:username/repo.git


🔍 ​四、终极调试技巧

  1. 详细连接日志分析​:

    bash 复制代码
    ssh -Tvv git@github.com  # -vv参数输出全链路日志
    • 关注Connection establishedidentity file路径验证。
  2. 网络质量诊断​:

    bash 复制代码
    ping ssh.github.com -t      # 持续测试连通性
    telnet ssh.github.com 443   # 检查端口开放状态
  3. 时间同步校准​(解决证书验证失败):

    bash 复制代码
    w32tm /resync  # Windows时间同步
    sudo ntpdate pool.ntp.org  # Linux/macOS

💎 ​总结

问题阶段 核心策略 关键命令/操作
端口22被封锁 切换SSH到443端口 Hostname ssh.github.com; Port 443
主机密钥验证失败 清理known_hosts冲突记录 ssh-keygen -R ssh.github.com
企业代理/DNS污染 Hosts文件绑定IP+代理配置 140.82.114.36 ssh.github.com
多账号密钥冲突 分账号配置独立密钥 Host github-work; IdentityFile ...

通过以上步骤,可解决99%的GitHub连接问题

若仍失败,优先检查GitHub服务状态,或使用HTTPS临时替代(git clone https://github.com/...),或确认是否为 GitHub 全球服务中断

相关推荐
dong_junshuai44 分钟前
每天一个开源项目#46 World Monitor:6.6万星、56层地图的全球情报中枢
github
Xu_youyaxianshen4 小时前
Git 零基础常用指令手册(Gitee / GitHub 通用 )
git·gitee·github
阿里嘎多学长5 小时前
2026-07-22 GitHub 热点项目精选
开发语言·程序员·github·代码托管
小蠢驴打代码5 小时前
记忆库能通过测试,不等于回答值得信:Coding Agent Memory 的两层评估设计
github·ai编程
武子康5 小时前
Copilot Code Review 从固定 Reviewer 演进为可编程 Runtime,仓库控制面、Setup 供应链、Runner 资源和 MCP 工具同时被纳入审查决策
人工智能·github·aigc
wangruofeng16 小时前
opencodex 解锁 Codex 任意模型,一个本地代理打通 Claude/Kimi/GLM/DeepSeek
llm·github·openai
一点一木18 小时前
从60首歌到1个网站:输入你的故事,还你一首歌
前端·github
刘较瘦_1 天前
AI 开发中的 Git Submodule 父子仓库模式:前后端分仓管理与协作实践
前端·github
DeMinds1 天前
内容没有丢,我为什么总在重新整理?|DeMinds 如何让工作接着继续
ios·github·markdown