GIT报错:Connection to git. closed by remote host.

GIT报错:Connection to git. closed by remote host.

问题

在另一个系统(虚拟机)中git clone 一次后,在其他地方就不能git clone了;clone下来后,也可以正常操作,就是clone这一步有问题

git 版本和 ssh版本一样的

检索了很多都不对症

检查日志

bash 复制代码
ssh -T -p 28902 git@git.hub.com.cn -vv

查看日志文件:

日志中有一个非常隐蔽的 debug 信息: debug1: client_global_hostkeys_private_confirm: server used untrusted RSA signature algorithm ssh-rsa for key 0, disregarding

这说明虽然连接成功了,但物理机的 SSH 客户端对服务器使用的 ssh-rsa 签名算法表示了"怀疑"或"不完全信任",这在执行复杂的 Git 数据流传输时可能会导致连接被意外重置。

既然物理机和虚拟机软件版本一致,那么物理机可能受到了一些特定的系统安全策略影响。请尝试以下绝对路径规避法:

核心对策:避免使用 ssh:// 格式在 GitLab 这种非标准端口环境下,ssh:// 格式对路径的解析非常敏感。请尝试用这种方式执行(这是绕过路径解析问题的万能写法):

bash 复制代码
GIT_SSH_COMMAND='ssh -p 28902 -o HostKeyAlgorithms=+ssh-rsa' git clone git@git.hub.com.cn:orca/hub.git 1

这条命令做了三件事:

手动强制指定端口 28902。

通过 -o HostKeyAlgorithms=+ssh-rsa 解决了日志中提到的算法信任问题。

使用了 git@host:path 这种格式(注意没有 ssh://),这会让服务器端的 GitLab 自动处理仓库的相对路径。

如果上述命令依然失败,请进行最后两项检查:

  1. 检查环境变量中的 Git 干扰
    如果你的物理机装过某些开发工具(如 Anaconda, VS Code 插件),它们可能会在后台修改 Git 的行为。 请运行:
bash 复制代码
env | grep GIT

如果看到 GIT_PROXY 或类似的变量,请用 unset 将其清除。

  1. 检查是否有两个 .ssh/config

有时 /etc/ssh/ssh_config 和 ~/.ssh/config 中的配置会产生冲突。 尝试临时重命名你的本地配置文件,看看是否是配置干扰:

Bash 复制代码
mv ~/.ssh/config ~/.ssh/config.bak
# 然后再次运行上面的 clone 命令
  1. MTU 网络包分片问题(仅限物理机是有线/特殊网卡时)
    由于 SSH 测试(几个字节)能成功,但 Clone(大量数据)失败,极有可能是网络包太大被路由器丢弃。 尝试强制减小包大小重试:
Bash 复制代码
# 修改网卡 MTU (假设你的网卡是 eth0 或 en0,请根据 ifconfig 结果修改)
sudo ifconfig eth0 mtu 1400

测试

正常的

bash 复制代码
git clone ssh://git@git.hub.com.cn:28902/orca/abc.git 3
Cloning into '3'...
remote: Enumerating objects: 46968, done.
remote: Counting objects: 100% (68/68), done.
remote: Compressing objects: 100% (51/51), done.
remote: Total 46968 (delta 19), reused 59 (delta 15), pack-reused 46900
Receiving objects: 100% (46968/46968), 413.39 MiB | 31.37 MiB/s, done.
Resolving deltas: 100% (34923/34923), done.
Updating files: 100% (2700/2700), done.

特殊方式,可能是端口问题

bash 复制代码
[13:36:43  ~]
 $ GIT_SSH_COMMAND='ssh -p 28902 -o HostKeyAlgorithms=+ssh-rsa' git clone git@git.hub.com.cn:orca/abc.git 1
Cloning into '1'...
remote: Enumerating objects: 46968, done.
remote: Counting objects: 100% (68/68), done.
remote: Compressing objects: 100% (51/51), done.
remote: Total 46968 (delta 19), reused 59 (delta 15), pack-reused 46900
Receiving objects: 100% (46968/46968), 413.39 MiB | 13.56 MiB/s, done.
Resolving deltas: 100% (34923/34923), done.
Updating files: 100% (2700/2700), done.


[13:59:26  ~]
 $ git clone git@git.hub.com.cn:orca/abc.git  2
Cloning into '2'...
The authenticity of host 'git.hub.com.cn (172.16.20.113)' can't be established.
ED25519 key fingerprint is SHA256:O7HGjmND2eopq20zxlLxGgW4Pf9E2FQ4qyRADR15fDU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'git.hub.com.cn' (ED25519) to the list of known hosts.
git@git.hub.com.cn's password:
Permission denied, please try again.
git@git.hub.com.cn's password:


[14:00:54  ~]
 $ GIT_SSH_COMMAND='ssh -p 28902 ' git clone git@git.hub.com.cn:orca/abc.git 2
Cloning into '2'...
remote: Enumerating objects: 46968, done.
remote: Counting objects: 100% (68/68), done.
remote: Compressing objects: 100% (51/51), done.
remote: Total 46968 (delta 19), reused 59 (delta 15), pack-reused 46900
Receiving objects: 100% (46968/46968), 413.39 MiB | 24.63 MiB/s, done.
Resolving deltas: 100% (34923/34923), done.
Updating files: 100% (2700/2700), done.


[14:02:32  ~]
 $ GIT_SSH_COMMAND='ssh -p 28902 ' git clone ssh://git@git.hub.com.cn:orca/abc.git 2
fatal: destination path '2' already exists and is not an empty directory.


[14:02:50  ~]
 $ GIT_SSH_COMMAND='ssh -p 28902 ' git clone ssh://git@git.hub.com.cn:orca/abc.git 3
Cloning into '3'...
ssh: Could not resolve hostname git.hub.com.cn:orca: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.


[14:02:53  ~]
 $ GIT_SSH_COMMAND='ssh  ' git clone git@git.hub.com.cn:orca/abc.git 3
Cloning into '3'...
git@git.hub.com.cn's password:


[14:03:40  ~]
 $ vim ~/.ssh/config

[14:07:55  ~]
 $ cat ~/.ssh/config
Host hub
    HostName git.hub.com.cn
    User git
    Port 28902
    IdentityFile ~/.ssh/id_rsa

[14:08:13  ~]
 $ git clone hub:orca/abc.git 3
Cloning into '3'...
remote: Enumerating objects: 46968, done.
remote: Counting objects: 100% (68/68), done.
remote: Compressing objects: 100% (51/51), done.
remote: Total 46968 (delta 19), reused 59 (delta 15), pack-reused 46900
Receiving objects: 100% (46968/46968), 413.39 MiB | 24.08 MiB/s, done.
Resolving deltas: 100% (34923/34923), done.
Updating files: 100% (2700/2700), done.
相关推荐
CodexDave10 小时前
数据库连接池耗尽:排查顺序与三层兜底
服务器·前端·数据库·git·云原生·容器·kubernetes
乐观的Terry11 小时前
5、发布系统-Git 集成
大数据·git·elasticsearch
不怕犯错,就怕不做12 小时前
GIT的简单打patch应用format-patch and git am
linux·git·全文检索
Byron Loong12 小时前
【Git】如何检查 Ubuntu 系统上 gitLab 是否开启
git·ubuntu·gitlab
lar_slw13 小时前
git删除上一次提交
git
leoZ2311 天前
Git 集成实战完全指南(四):Git 冲突解决
大数据·git·elasticsearch
枫荷1 天前
Git LFS 大文件优化说明
git
techdashen1 天前
不用再反复 stash:用 Git Worktree 同时开发多个分支
大数据·git·elasticsearch
leoZ2312 天前
Git 集成实战完全指南(八):团队协作最佳实践
git
晴雨天️2 天前
Git工具使用指南
笔记·git