大学网络下Windows机器SSH克隆GitHub私有仓库失败求助
问题
在大学网络环境的Windows机器上,使用SSH克隆GitHub私有仓库时,进程会卡住数分钟后失败,判断是连接断开导致。已确认IP地址完全可路由,防火墙未拦截任何流量,HTTPS克隆正常,但SSH克隆失败。
调试日志
C:\Users\[User]\Documents\test>set GIT_SSH_COMMAND=ssh -v C:\Users\[User]\Documents\test>git clone git@github.com:SvenDNA/homepage.git Cloning into 'homepage'... OpenSSH_9.9p1, OpenSSL 3.2.3 3 Sep 2024 debug1: Reading configuration data /etc/ssh/ssh_config debug1: Connecting to github.com [140.82.121.3] port 22. debug1: Connection established. debug1: identity file /c/Users/[User]/.ssh/id_rsa type 0 debug1: identity file /c/Users/[User]/.ssh/id_rsa-cert type -1 debug1: identity file /c/Users/[User]/.ssh/id_ecdsa type -1 debug1: identity file /c/Users/[User]/.ssh/id_ecdsa-cert type -1 debug1: identity file /c/Users/[User]/.ssh/id_ecdsa_sk type -1 debug1: identity file /c/Users/[User]/.ssh/id_ecdsa_sk-cert type -1 debug1: identity file /c/Users/[User]/.ssh/id_ed25519 type -1 debug1: identity file /c/Users/[User]/.ssh/id_ed25519-cert type -1 debug1: identity file /c/Users/[User]/.ssh/id_ed25519_sk type -1 debug1: identity file /c/Users/[User]/.ssh/id_ed25519_sk-cert type -1 debug1: identity file /c/Users/[User]/.ssh/id_xmss type -1 debug1: identity file /c/Users/[User]/.ssh/id_xmss-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_9.9 debug1: Remote protocol version 2.0, remote software version babeld-cd94d92b7 debug1: compat_banner: no match: babeld-cd94d92b7 debug1: Authenticating to github.com:22 as 'git' debug1: load_hostkeys: fopen /c/Users/[User]/.ssh/known_hosts2: No such file or directory debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: algorithm: curve25519-sha256 debug1: kex: host key algorithm: ssh-ed25519 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: expecting SSH2_MSG_KEX_ECDH_REPLY debug1: SSH2_MSG_KEX_ECDH_REPLY received debug1: Server host key: ssh-ed25519 SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU debug1: load_hostkeys: fopen /c/Users/[User]/.ssh/known_hosts2: No such file or directory debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory debug1: Host 'github.com' is known and matches the ED25519 host key. debug1: Found key in /c/Users/[User]/.ssh/known_hosts:1 debug1: ssh_packet_send2_wrapped: resetting send seqnr 3 debug1: rekey out after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: ssh_packet_read_poll2: resetting read seqnr 3 debug1: SSH2_MSG_NEWKEYS received debug1: rekey in after 134217728 blocks debug1: SSH2_MSG_EXT_INFO received debug1: kex_ext_info_client_parse: server-sig-algs=<ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa> debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey debug1: Next authentication method: publickey debug1: Will attempt key: /c/Users/[User]/.ssh/id_rsa RSA SHA256:zYCuDer+slWMsl+qHWyrRVWzZqcTSXjdf1KBobFsWGY debug1: Will attempt key: /c/Users/[User]/.ssh/id_ecdsa debug1: Will attempt key: /c/Users/[User]/.ssh/id_ecdsa_sk debug1: Will attempt key: /c/Users/[User]/.ssh/id_ed25519 debug1: Will attempt key: /c/Users/[User]/.ssh/id_ed25519_sk debug1: Will attempt key: /c/Users/[User]/.ssh/id_xmss debug1: Offering public key: /c/Users/[User]/.ssh/id_rsa RSA SHA256:zYCuDer+slWMsl+qHWyrRVWzZqcTSXjdf1KBobFsWGY debug1: Server accepts key: /c/Users/[User]/.ssh/id_rsa RSA SHA256:zYCuDer+slWMsl+qHWyrRVWzZqcTSXjdf1KBobFsWGY Authenticated to github.com ([140.82.121.3]:22) using "publickey". debug1: channel 0: new session [client-session] (inactive timeout: 0) debug1: Entering interactive session. debug1: pledge: filesystem debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0 debug1: client_input_hostkeys: searching /c/Users/[User]/.ssh/known_hosts for github.com / (none) debug1: client_input_hostkeys: searching /c/Users/[User]/.ssh/known_hosts2 for github.com / (none) debug1: client_input_hostkeys: hostkeys file /c/Users/[User]/.ssh/known_hosts2 does not exist debug1: client_input_hostkeys: no new or deprecated keys from server debug1: Sending environment. debug1: channel 0: setting env GIT_PROTOCOL = "version=2" debug1: Sending command: git-upload-pack 'SvenDNA/homepage.git' debug1: pledge: fork remote: Enumerating objects: 910, done. remote: Counting objects: 100% (910/910), done. remote: Compressing objects: 100% (702/702), done. debug1: client_input_channel_req: channel 0 rtype exit-status reply 0 debug1: client_input_global_request: rtype keepalive@openssh.com want_reply 1 Connection to github.com closed by remote host. client_loop: send disconnect: Broken pipe fetch-pack: unexpected disconnect while reading sideband packet fatal: early EOF fatal: fetch-pack: invalid index-pack output
可能的原因及解决办法
从日志来看,SSH认证已成功,但在传输压缩后的仓库对象时连接被远程主机断开,大概率是大学网络对SSH端口(22)的流量做了限流或QoS策略,也可能是长连接无数据交互被网关主动切断。以下是可行的解决办法:
改用GitHub的SSH 443端口连接
GitHub支持在443端口提供SSH服务,多数网络对443端口的限制远低于22端口。操作步骤:- 在Windows用户目录的
.ssh文件夹下,创建或编辑名为config的无后缀文件 - 添加以下配置内容:
Host github.com Hostname ssh.github.com Port 443 User git
保存后,直接使用原SSH克隆命令即可。
- 在Windows用户目录的
开启SSH连接心跳保活
网络长时间无数据传输时,网关可能主动断开连接。开启心跳包可维持连接:
在上述config文件中添加或补充:Host github.com ServerAliveInterval 30 ServerAliveCountMax 5该配置会每30秒发送一次心跳包,连续5次未收到回应才断开连接。
降低Git压缩级别或禁用压缩
高压缩的仓库数据传输可能触发网络限流,尝试调整Git压缩设置:git config --global core.compression 0或在克隆时临时禁用压缩:
git clone git@github.com:SvenDNA/homepage.git --no-compression使用浅克隆减少传输数据量
如果不需要仓库完整历史,可只克隆最近的提交:git clone git@github.com:SvenDNA/homepage.git --depth 1后续若需要完整历史,执行
git fetch --unshallow即可。
内容的提问来源于stack exchange,提问作者SvenDNA
相关产品推荐
相关产品推荐

