我在线设置了一个可供任何人公开访问的 SSH 服务器。因此,我从世界各地的 IP 获得了很多连接。奇怪的是,实际上没有人尝试进行身份验证以打开会话。我可以自己连接和验证没有任何问题。
有时,我会error: kex_exchange_identification: Connection closed by remote host
在服务器日志中获取信息。是什么原因造成的?
这是 30 分钟的 SSH 日志(公共 IP 已被编辑):
# journalctl SYSLOG_IDENTIFIER=sshd -S "03:30:00" -U "04:00:00"
-- Logs begin at Fri 2020-01-31 09:26:25 UTC, end at Mon 2020-04-20 08:01:15 UTC. --
Apr 20 03:39:48 myhostname sshd[18438]: Connection from x.x.x.207 port 39332 on 10.0.0.11 port 22 rdomain ""
Apr 20 03:39:48 myhostname sshd[18439]: Connection from x.x.x.207 port 39334 on 10.0.0.11 port 22 rdomain ""
Apr 20 03:39:48 myhostname sshd[18438]: Connection closed by x.x.x.207 port 39332 [preauth]
Apr 20 03:39:48 myhostname sshd[18439]: Connection closed by x.x.x.207 port 39334 [preauth]
Apr 20 03:59:36 myhostname sshd[22186]: Connection from x.x.x.83 port 34876 on 10.0.0.11 port 22 rdomain ""
Apr 20 03:59:36 myhostname sshd[22186]: error: kex_exchange_identification: Connection closed by remote host
这是我的 SSH 配置:
# ssh -V
OpenSSH_8.2p1, OpenSSL 1.1.1d 10 Sep 2019
# cat /etc/ssh/sshd_config
UsePAM yes
AddressFamily any
Port 22
X11Forwarding no
PermitRootLogin prohibit-password
GatewayPorts no
PasswordAuthentication no
ChallengeResponseAuthentication no
PrintMotd no # handled by pam_motd
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
KexAlgorithms [email protected],diffie-hellman-group-exchange-sha256
Ciphers [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr
MACs [email protected],[email protected],[email protected],hmac-sha2-512,hmac-sha2-256,[email protected]
LogLevel VERBOSE
UseDNS no
AllowUsers root
AuthenticationMethods publickey
MaxStartups 3:100:60
在网上搜索后,我看到了MaxStartups
表明这可能是导致此错误的原因的参考资料,但是在更改了我的默认值sshd_config
并尝试超过 3 个连接后,服务器明确指出了问题
Apr 20 07:26:59 myhostname sshd[31468]: drop connection #3 from [x.x.x.226]:54986 on [10.0.0.11]:22 past MaxStartups
那么,是什么原因造成的error: kex_exchange_identification: Connection closed by remote host
呢?
一些蜘蛛程序和服务(如Shodan)会扫描公共 ipv4 地址以查找开放服务,例如 salt master、ftp 服务器、RDP 以及 SSH 服务。这些蜘蛛通常只连接到服务而不执行任何有效的身份验证步骤。
我还没有找到关于这个的结论性答案,所以……是时候浏览源代码了。
在 OpenSSH 源代码中,
kex_exchange_identification
是一个交换服务器和客户端标识的函数(duh),如果 OpenSSH 服务器和客户端之间的套接字连接中断(参见),即客户端已经关闭其连接,则会发生指定的错误。EPIPE
我刚刚遇到了这个确切的问题,原因是我在负载均衡器内部发生了端口转换,这意味着我的
ssh
连接到达了 port80
而不是 port上的主机22
。主机是否正确终止了连接,返回到我的终端的错误消息如下;
修复了内部端口翻译,现在问题已经消失了。
当我注意到我本应使用私有 IP 时尝试使用服务器 IP 进行连接时,我用“kex_exchange_identification:远程主机关闭连接”解决了我的问题。
我的设置可能与你们所有人都不同,只是想传递我自己的发现。
编辑:
对于某些托管服务提供商,您将拥有两个 IP,一个是公共的,一个是私有的,私有是您应该在这种情况下使用的一个。
你知道或不知道,我很感激这不适用于每个人,这就是为什么我说它可能是一个不同的设置。
在我使用私钥之前,没有其他答案对我有用。
就我而言,我使用手动 /etc/hosts 条目并通过堡垒进行代理。堡垒没有相同的 /etc/hosts 条目,因此它拒绝了隧道。
在我今天安装和配置 fail2ban 之后,我注意到这发生在我身上(在 auth.log 文件中)。所以这可能是原因。
在我的例子中,是从 Gitlab CI 中的受保护变量创建 ssh 密钥,我必须删除对该变量的保护才能使其正常工作。
您可能只是连接到
wrong port
.验证确切的端口!
您可以使用指定自定义端口
ssh -p port user@host
就我而言,我试图通过 ssh 连接到在 VirtualBox 上运行的 Ubuntu VM。我忽略了
openssh-server
在虚拟机上安装。解决它。
断开 VPN 连接。
就我而言,这解决了它。
我用 指定的非标准端口
-p
,将密钥添加到服务器和我的凭据管理器,我收到了这个错误。我发现我的具体问题是,我需要在托管服务的服务器控制面板中将我的 IP 列入白名单,以获得我的 SSH 登录 ID。