Git 操作 Gitea 出现 kex_exchange_identification: Connection closed by remote host

1. 问题背景

我的部分代码储存在使用 Gitea 搭建的服务上: DevWiki - 首页 - DevWiki Gitea

之前都是使用网页在网站上进行 仓库管理, 今天在clone代码的时候出现异常: kex_exchange_identification: Connection closed by remote host, 具体以下提示:

$ git clone [email protected]:DevWiki/CefSharp.MinimalExample.git
Cloning into 'CefSharp.MinimalExample'...
kex_exchange_identification: Connection closed by remote host
Connection closed by 124.223.154.177 port 22
fatal: Could not read from remote repository.

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

2. 问题分析

由于之前没有遇到过此类问题, 随即 Google搜索 了下, 发现网上有类似的情况出现, 总结发现有几类原因:

  • ssh key 配置过期 需要重新配置
  • github DNS污染需要修改host

总之原因就是 客户端 与 服务器链接有问题, 随即使用 以下命令进行分析:

 $ ssh -vvvt [email protected]
OpenSSH_8.5p1, OpenSSL 1.1.1k  25 Mar 2021
debug1: Reading configuration data /c/Users/bozho/.ssh/config
debug1: /c/Users/bozho/.ssh/config line 6: Applying options for git.devwiki.net
debug1: Reading configuration data /etc/ssh/ssh_config
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts' -> '/c/Users/bozho/.ssh/known_hosts'
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts2' -> '/c/Users/bozho/.ssh/known_hosts2'
debug2: resolving "git.devwiki.net" port 22
debug3: ssh_connect_direct: entering
debug1: Connecting to git.devwiki.net [124.223.154.177] port 22.
debug3: set_sock_tos: set socket 3 IP_TOS 0x48
debug1: Connection established.
debug1: identity file C:\\Users\\bozho\\.ssh\\devwiki type 0
debug1: identity file C:\\Users\\bozho\\.ssh\\devwiki-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.5
kex_exchange_identification: Connection closed by remote host
Connection closed by 124.223.154.177 port 22

上面的结果显示,Connection established 但是随即 closed. 网上给出的建议可以更换 ssh 端口或者 使用 HTTP方式, 经测试 使用 HTTP clone代码正常:

$ git clone https://git.devwiki.net/DevWiki/CefSharp.MinimalExample.git
Cloning into 'CefSharp.MinimalExample'...
warning: You appear to have cloned an empty repository.

这么说明, 客户端和服务器的连接没问题, 问题应该出在服务器侧. 查看 Gitea的后台配置:

其中的 SSH是启用的, 端口什么都配置了, 我是按照 Gitea 官方的Docker 安装教程安装的, 正常来说这这配置应该没问题. 但是现在无法使用ssh, 则又去 Gitea 官方网站查询文档: 配置说明 - Docs

DISABLE_SSH: 是否禁用SSH。
START_SSH_SERVER: 是否启用内部SSH服务器。
SSH_PORT: SSH端口,默认为 22。

在对应的配置文件中添加了: START_SSH_SERVER=true, 然后重启 Gitea, 查看后台配置显示如下:

说明已经启用了 内置的 SSH 服务器, 然后再次测试 clone:

$ git clone [email protected]:DevWiki/CefSharp.MinimalExample.git
Cloning into 'CefSharp.MinimalExample'...
warning: You appear to have cloned an empty repository.

这说明已经正常了.

3. 后记

再次查看 Gitea 的Docker 安装文档 使用 Docker 安装 - Docs , 在环境变量中也没有提到 START_SSH_SERVER 这个配置项:

  • SSH_DOMAIN:localhost:该服务器的域名,用于 Gitea UI 中显示的 ssh 克隆 URL。如果启用了安装页面,则 SSH 域服务器将采用以下形式的 DOMAIN 值(保存时将覆盖此设置)。
  • SSH_PORT:22:克隆 URL 中显示的 SSH 端口。
  • SSH_LISTEN_PORT:%(SSH_PORT)s:内置 SSH 服务器的端口。
  • DISABLE_SSH:false:如果不可用,请禁用 SSH 功能。如果要禁用 SSH 功能,则在安装 Gitea 时应将 SSH 端口设置为 0。

但是提供了一个 SSH 直通的配置说明: SSH 容器直通, 可能官方更推荐使用此种方式来启用 SSH, 后续有时间再研究.

如果你也遇到了 kex_exchange_identification 问题, 希望本文对你有一定的启发帮助~

猜你喜欢

转载自blog.csdn.net/DevWiki/article/details/128228396