无法使用macOS内置的ssh客户端连接远程主机除了22端口以外的任何端口

5

我最近将运行Dropbear的服务器上的ssh监听端口从22更改为一个随机端口,以防止有人通过暴力破解方式使系统日志被淹没。

一切正常,我可以使用Termux(一个可安装软件包的Android终端模拟器)从WAN侧连接到服务器。

但是当我尝试使用我的 MacBook ssh 到此服务器时(与我的手机处于同一网络下,并且之前在dropbear监听22端口时能够ssh到服务器),连接立即中断,ssh 输出以下信息:

kex_exchange_identification: write: Broken pipe

详细输出并没有展示任何有用的信息(而且还很突兀地被切断了,我可以补充一下)。
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/my_username/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Connecting to remote.yhaoquan.top port 2123.
debug1: Connection established.
debug1: identity file /Users/my_username/.ssh/id_rsa type 0
debug1: identity file /Users/my_username/.ssh/id_rsa-cert type -1
debug1: identity file /Users/my_username/.ssh/id_dsa type -1
debug1: identity file /Users/my_username/.ssh/id_dsa-cert type -1
debug1: identity file /Users/my_username/.ssh/id_ecdsa type -1
debug1: identity file /Users/my_username/.ssh/id_ecdsa-cert type -1
debug1: identity file /Users/my_username/.ssh/id_ed25519 type -1
debug1: identity file /Users/my_username/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/my_username/.ssh/id_xmss type -1
debug1: identity file /Users/my_username/.ssh/id_xmss-cert type -1
kex_exchange_identification: write: Broken pipe

通过 Telnet 连接到 2123 端口,表明我已经成功连接到服务器:

>telnet {hostname} 2123
Trying {host IP address}...
Connected to {hostname}
Escape character is '^]'.
SSH-2.0-dropbear
|

��]J044��d����curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,kexguess2@matt.ucc.asn.aursa-sha2-256,ssh-rsa3chacha20-poly1305@openssh.com,aes128-ctr,aes256-ctr3chacha20-poly1305@openssh.com,aes128-ctr,aes256-ctrhmac-sha1,hmac-sha2-256hmac-sha1,hmac-sha2-256nonenoneM�'�Rf

当我把服务器端口改回22时,一切正常。
有人知道问题可能在哪里或者我应该如何诊断问题吗?谢谢
编辑1: 对于那些遇到相同问题的人,一个临时解决方案是使用OpenSSH。最简单的方法(使其成为一个即插即用的解决方案)是使用Homebrew:
brew install openssh

这个命令将会把你使用的ssh客户端从/usr/bin/ssh改成一个别名,指向/usr/local/bin/ssh,该别名指向了使用brew刚刚安装的OpenSSH,这样症状似乎不会再出现。

然而,这并没有解决这篇文章所涉及的问题。我仍在寻找解决方案。


1
今天我的经验是所有的 ssh 连接都会失败并显示 kex_exchange 错误。我使用 Homebrew 安装了 openssh,而那个 ssh 实现则成功地建立了连接。但我不知道我今天做了什么导致 /usr/bin/ssh 内置的 ssh 出了问题。 - glenn jackman
1
@glennjackman 是的,我也完全这样做了。我没有回答这个问题,因为问题只是得到了缓解,而我仍然无法弄清楚为什么内置的ssh在读取与openssh相同的配置时无法工作。 - Aaron You
我的谷歌技能也无法克服这个问题。 - glenn jackman
它能在我的macOS Catalina 10.15.7上工作,无需安装来自homebrew的ssh: $ ssh -VOpenSSH_8.1p1,LibreSSL 2.7.3$ which ssh /usr/bin/ssh我已经尝试过ssh -p 22000 user@ip和通过更改配置文件。 - fsquirrel
1
同样的问题,macOS 11.1。Homebrew 已经修复了命令行 ssh 的问题,但是 Mac Mail 程序似乎仍在使用原始的 (/usr/bin/ssh) 来进行 SSH 连接。我还无法证明这一点,但是今天早上邮件和命令行 ssh 都开始出现故障。因此,我仍在寻找一个“真正”的解决方案,因为我认为这是修复邮件程序所必需的。 - Eric Moyer
显示剩余3条评论
4个回答

2

这解决了我在/usr/bin/ssh上遇到的问题。

背景:

  • 使用主机名进行ssh会失败,并出现kex_exchange_identification: write: Broken pipe
  • 使用IP地址进行ssh可以正常工作。
  • 安装新版本的SSH(使用Homebrew)可以正常工作(从终端,使用/usr/local/bin/ssh)。
  • Mac Mail仍然无法正常工作(它使用SSH连接我的IMAP帐户)。

我发现这篇帖子,其中有人(昨天)遇到了与我完全相同的症状,来自一款我不使用的Broadcom防病毒软件(WSS Agent):

https://community.broadcom.com/symantecenterprise/communities/community-home/digestviewer/viewthread?GroupId=2593&MessageKey=57c16f08-06e4-4c1d-adc7-bd3f51cc4516&CommunityKey=84278e0f-9c57-4f93-8cff-4530b03e3c07&tab=digestviewer&ReturnUrl=%2Fbrowse%2Fallrecentposts

这让我想起Sophos(防病毒软件)最近(可能是昨天,也可能是前一天)安装了“SophosWebNetworkExtentsion”。他们在2021年1月19日发布了这个视频(关于如何接受安装):

https://www.youtube.com/watch?v=z-qWdv1ynMs

解决方法:

  1. 我登录到Sophos,在“Web Protection”下禁用了Web Protection。 enter image description here enter image description here
  2. 我需要重新启动电脑才能使更改影响SSH。 重新启动后,usr/bin/ssh再次正常工作并且Mac Mail也可以正常工作。

修复之前:

$ /usr/bin/ssh s0-backend -v
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/eric/.ssh/config
debug1: /Users/eric/.ssh/config line 62: Applying options for s0-backend
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Connecting to s0-backend port 61917.
debug1: Connection established.
debug1: identity file /Users/eric/.ssh/id_rsa type 0
debug1: identity file /Users/eric/.ssh/id_rsa-cert type -1
debug1: identity file /Users/eric/.ssh/id_dsa type -1
debug1: identity file /Users/eric/.ssh/id_dsa-cert type -1
debug1: identity file /Users/eric/.ssh/id_ecdsa type -1
debug1: identity file /Users/eric/.ssh/id_ecdsa-cert type -1
debug1: identity file /Users/eric/.ssh/id_ed25519 type -1
debug1: identity file /Users/eric/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/eric/.ssh/id_xmss type -1
debug1: identity file /Users/eric/.ssh/id_xmss-cert type -1
kex_exchange_identification: write: Broken pipe
$

修复后(成功SSH登录):

usr/bin/ssh s0-backend -v
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/eric/.ssh/config
debug1: /Users/eric/.ssh/config line 62: Applying options for s0-backend
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Connecting to s0-backend port {redacted}.
debug1: Connection established.
debug1: identity file /Users/eric/.ssh/id_rsa type 0
debug1: identity file /Users/eric/.ssh/id_rsa-cert type -1
debug1: identity file /Users/eric/.ssh/id_dsa type -1
debug1: identity file /Users/eric/.ssh/id_dsa-cert type -1
debug1: identity file /Users/eric/.ssh/id_ecdsa type -1
debug1: identity file /Users/eric/.ssh/id_ecdsa-cert type -1
debug1: identity file /Users/eric/.ssh/id_ed25519 type -1
debug1: identity file /Users/eric/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/eric/.ssh/id_xmss type -1
debug1: identity file /Users/eric/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.2p1 Ubuntu-4ubuntu0.1
debug1: match: OpenSSH_8.2p1 Ubuntu-4ubuntu0.1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to s0-backend:61917 as '{redacted}'
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: Server host key: ssh-ed25519 SHA256: {redacted}
debug1: Host '[s0-backend]:{redacted}' is known and matches the ED25519 host key.
debug1: Found key in /Users/eric/.ssh/known_hosts:40
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
...
{redacted}
...
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending env LC_TERMINAL = iTerm2
debug1: Sending env LC_TERMINAL_VERSION = 3.4.3
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-62-generic x86_64)
...
{redacted}
...
$ 

我敢打赌这也是我的问题。我的笔记本电脑运行Sophos,但由IT管理,所以我无法关闭它。这似乎解释了“突然停止工作”的频繁问题描述 - Sophos在后台更新。很高兴homebrew openssh正在工作。 - glenn jackman

2

我遇到了同样的问题:ssh在没有网络活动的情况下返回"kex_exchange_identification: write: Broken pipe"。

在我的情况下,罪魁祸首是AdGuard。

我在高级配置network.extension.exclude.ports中添加了我用于ssh的端口,现在它可以正常工作了。根据您的配置,network.extension.exclude.domains可能是更好的选择。


谢谢。这也解决了我的问题。没想到AdGuard采取这种激进的方法来确保你不会请求广告,做得很好。 - Aaron You

0

你的服务器上有端口22.ssh/known_hosts文件里吗?

如果有的话,也许你可以删除这一行,尝试使用新的端口重新登录。


known_hosts 中的条目是针对主机和端口组合特定的,因此这不会改变任何内容。 - jeb

-1
根据您的日志,内置的ssh命令从/Users/my_username/.ssh/config加载配置。
debug1: Reading configuration data /Users/my_username/.ssh/config

请检查此文件内部,可能有一些配置阻止默认客户端使用22号端口以外的任何其他端口。由于OpenSSH客户端不使用此配置文件,因此连接到2123号端口没有问题。

这里有一个类似的问题https://github.com/hashicorp/vagrant/issues/410#issuecomment-207278540


3
你认为 OpenSSH 不使用 ~/.ssh/config 吗?实际上它是会使用的。 - glenn jackman
2
在详细输出中,您可以清楚地看到仅对来自/etc/ssh/ssh_config的此连接应用了一个*角色。因此,即使我在/Users/my_username/.ssh/config中有什么也无关紧要... - Aaron You

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接