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

397 阅读3分钟

对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绕过限制。
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文件中存在旧端口密钥记录,与新连接冲突。

image.png

  • “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):

    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必做)​​:

    chmod 600 ~/.ssh/config  # 确保文件权限安全
    

    windows:

image.png

image.png

image.png

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

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

    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:企业网络严格封锁

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

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

    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):

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

    ipconfig /flushdns  # Windows
    sudo dscacheutil -flushcache # macOS
    

场景3:多账号密钥冲突

~/.ssh/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. 详细连接日志分析​:

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

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

    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 全球服务中断