怎么修? "kex_exchange_identification: read: Connection reset by peer"

How to fix? "kex_exchange_identification: read: Connection reset by peer"

我想使用 PRIVATE_KEY 在 GitLab 管道中使用 scp 复制数据 错误是:

kex_exchange_identification: read: Connection reset by peer
Connection reset by x.x.x.x port 22
lost connection

管道日志:

$ mkdir -p ~/.ssh
$ echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
$ chmod 600 ~/.ssh/id_rsa
$ eval "$(ssh-agent -s)"
Agent pid 22

$ ssh-add ~/.ssh/id_rsa
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

$ ssh-keyscan -H $IP >> ~/.ssh/known_hosts
# x.x.x.x:22 SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.10
# x.x.x.x:22 SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.10

$ scp -rv api.yml root@$IP:/home/services/test/
Executing: program /usr/bin/ssh host x.x.x.x, user root, command scp -v -r -t /home/services/test/

OpenSSH_8.6p1, OpenSSL 1.1.1l  24 Aug 2021
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to x.x.x.x [x.x.x.x] port 22.
debug1: Connection established.
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: identity file /root/.ssh/id_ecdsa_sk type -1
debug1: identity file /root/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: identity file /root/.ssh/id_ed25519_sk type -1
debug1: identity file /root/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /root/.ssh/id_xmss type -1
debug1: identity file /root/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.6
kex_exchange_identification: read: Connection reset by peer
Connection reset by x.x.x.x port 22
lost connection
kex_exchange_identification: read: Connection reset by peer

当 ssh 客户端连接到 ssh 服务器时,服务器首先向客户端发送版本字符串。您得到的错误意味着客户端到服务器的 TCP 连接在客户端等待来自服务器的数据时“异常关闭”,换句话说,在 TCP 连接打开后立即关闭。

实际上,这可能意味着以下两种情况之一:

  1. ssh 服务器进程发生故障(崩溃),或者它可能检测到导致它立即退出的一些严重问题。
  2. 某些防火墙正在干扰与 ssh 服务器的连接。

看起来 ssh-keyscan 程序能够连接到服务器并获取版本字符串而没有错误。所以 ssh 服务器进程显然能够在不崩溃的情况下与客户端通信。

您应该与此 x.x.x.x 主机及其所连接的网络的管理员联系,看看他们是否可以从他们的角度找出问题所在。有可能某些东西(防火墙或 ssh 服务器进程本身)正在将多个连接视为入侵尝试,首先来自 ssh-keyscan 进程,然后是 scp 程序。它阻止了第二次连接尝试。

我建议检查路由 table 以寻找一种可能性。就我在 Ubuntu20 上的情况而言,当我使用 ssh 连接到服务器时收到相同的错误消息时,我添加了一个本地网络路由条目来恢复。没想到竟然消失了,只剩下默认路由

user@hostname:~$ route -n Kernel IP routing table Destination     Gateway  
Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 enp1s0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 enp1s0  # <= disappeared

虽然第一个 syn 通过了,但似乎 ack 被不完整的路由过滤了 table。

和我一样的问题: 我已通过执行以下步骤解决了问题

  1. 编辑文件"etc/hosts.allow"。这样做的命令 "sudo nano /etc/hosts.allow".
  2. 最后将 ALL 键的值更新为 ALL,如 ALL : ALL。保存文件并重试。

基本上 ALL 可能会设置为其他内容,因此在与主机建立 ssh 连接时,预计请求应来自从 10...* 开始的 IP如果全部设置为 ALL : 10.。因此,通过将 10. 替换为 ALL,您将允许来自任何地方的连接。

我在更改 Apple ID 密码后遇到了这个问题,所以我更新了我的 Apple ID 并重新启动了我的 Mac,现在可以使用了。

mac:release jianzhang$ git pull origin master
kex_exchange_identification: read: Connection reset by peer
Connection reset by 20.205.243.166 port 22
fatal: Could not read from remote repository.

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

类似于@naoki-ogawa我的路由有问题table在我的情况下我有一个额外的本地网络路由。

# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         RT-AX92U-3E20   0.0.0.0         UG    100    0        0 eno1
link-local      0.0.0.0         255.255.0.0     U     1000   0        0 virbr1
192.168.50.0    RT-AX92U-3E20   255.255.255.0   UG    10     0        0 eno1
192.168.50.0    0.0.0.0         255.255.255.0   U     100    0        0 eno1

我简单地删除了本地网络上的网关 (192.168.50.0)

route del 192.168.50.0/24 via 192.168.50.1

问题已解决。

我遇到了同样的问题。我重新启动了服务器,然后一切正常。

您可以尝试使用 VPN 或者如果您之前一直在使用它,请尝试将其关闭并重新连接 如果您没有 VPN 的预算,您可以尝试 ProtonVPN 这是自由。当我遇到同样的问题时,它对我有用。

尝试检查打开的 ssh 服务器是否已启动并在服务器端 运行。尝试检查 sshd 配置。它以这种方式为我工作。 祝你好运。