无法与服务器连接。连接被远程服务器关闭

use*_*141 2 linux ssh ubuntu-12.04

我正在尝试从本地服务器 ssh 远程服务器。但每当我运行 ssh 命令时:

ssh root@x.x.x.x
Run Code Online (Sandbox Code Playgroud)

我收到错误:

连接已由 xxxx 关闭

ssh -v -v -v -v root@xxxx 的输出是:

OpenSSH_5.9p1 Debian-5ubuntu1.1, OpenSSL 1.0.1 14 Mar 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to x.x.x.x [x.x.x.x] port 22.
debug1: Connection established.
debug3: Incorrect RSA1 identifier
debug3: Could not load "/home/mona/.ssh/id_rsa" as a RSA1 public key
debug1: identity file /home/mona/.ssh/id_rsa type 1
debug1: Checking blacklist file /usr/share/ssh/blacklist.RSA-2048
debug1: Checking blacklist file /etc/ssh/blacklist.RSA-2048

debug1: identity file /home/mona/.ssh/id_rsa-cert type -1
debug1: identity file /home/mona/.ssh/id_dsa type -1
debug1: identity file /home/mona/.ssh/id_dsa-cert type -1
debug1: identity file /home/mona/.ssh/id_ecdsa type -1
debug1: identity file /home/mona/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-5ubuntu1.1
debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1.1 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.1
debug2: fd 3 setting O_NONBLOCK
debug3: load_hostkeys: loading entries for host "151.236.220.15" from file "/home/mona/.ssh/known_hosts"
debug3: load_hostkeys: loaded 0 keys
debug1: SSH2_MSG_KEXINIT sent
Connection closed by x.x.x.x
Run Code Online (Sandbox Code Playgroud)

我已将 id_rsa.pub 的内容加载到known_hosts 键中。

我无法 ssh 登录。

有人可以帮我吗?真的会很感激。

谢谢。

Dra*_*oan 5

按照 Fred 在评论中的观点(并实际阅读错误消息),我错了,ssh正在连接。我将把我原来的回复留在底部,并另外回答无法连接到正在运行的 ssh 的问题。

当 sshd 服务器拒绝连接,并且如果 OP 正确,则没有任何内容登录auth.log或 时,诊断 ssh 问题的另一种好方法syslog是在启用调试的单独端口上运行它(我选择了 的任意端口44)。

/full/path/to/sshd -p 44 -d 
Run Code Online (Sandbox Code Playgroud)

然后,您可以连接 ssh 客户端并进一步调试问题:

ssh -p 44 root@x.x.x.x
Run Code Online (Sandbox Code Playgroud)

Root(正如 Fred 在他的回答中指出的那样)是一个可能通过PermitRootLogin您的sshd_config. 此外,您使用的身份验证方法类型sshd_config可以进一步限制您访问主机的方式:

RSAAuthentication
PubkeyAuthentication
RhostsRSAAuthentication
HostbasedAuthentication
ChallengeResponseAuthentication
PasswordAuthentication
KerberosAuthentication
GSSAPIAuthentication
Run Code Online (Sandbox Code Playgroud)

man 5 sshd_config有关这些选项的更多信息,请参阅 sshd_config () 的手册页。通常大多数 sshd 都有RSAAuthentication,PubkeyAuthentication有时也有PasswordAuthentication. RSAAuthentication是特定于Protocol 1并且大多数主机使用Protocol 2PubkeyAuthentication。两者都依赖于root拥有一个密钥文件(通常在 中找到/root/.ssh/authorized_keys),但该位置可以被该AuthorizedKeysFile选项覆盖。您的 sshd 上似乎PasswordAuthentication未启用。

对于 RSA 和 Pubkey 身份验证,您需要密钥对。您已生成它们,它们位于您的客户端计算机上的/home/mona/.ssh/id_rsa和中/home/mona/.ssh/id_rsa.pub。这两个文件的公共部分(/home/mona/.ssh/id_rsa.pub 中包含的密钥)需要放入上面提到的 root 文件中authorized_key

原始答案,指的是远程连接到 sshd 进程失败

这看起来像是 TCPWrappers 或防火墙关闭初始连接。

检查您的auth.logsyslog文件,/var/log因为这些文件可能会提供一些线索来确定哪些内容正在阻止连接。

TCPwrappers 通常是通过/etc/hosts.allow文件实现的,在某些 Unix 上是附加文件或仅/etc/hosts.deny文件(即没有hosts.allow 文件)。

条目通常采用以下形式:

<service> : <access list> : <allow|deny>
Run Code Online (Sandbox Code Playgroud)

或者

<service> : <access list>
Run Code Online (Sandbox Code Playgroud)

取决于所使用的 tcp 包装器的类型。这些文件的格式通常可以在hosts_access 手册页中找到man 5 hosts_access。您可能需要添加一个条目以允许您的远程 IP 访问。

sshd : my.ip.address.here : allow
Run Code Online (Sandbox Code Playgroud)

大多数具有 Linux 内核的发行版倾向于使用iptables作为主要防火墙,尽管有些发行版使用ipchains. (我知道 FreeBSD 使用的ipfw是从 NetBSD 移植的)。您的服务提供商可能也有防火墙,或者在您的服务前面有防火墙的路由器,它会阻止这些请求。至于您的主机使用哪个防火墙,需要进行一些调查。

iptables可以通过命令列出防火墙规则iptables -nvL(必须以 root 身份运行,或通过 sudo 运行)。该INPUT链是用于允许/禁止主机传入连接的规则集。您可能需要添加规则以允许 SSH 连接入站:

iptables -I INPUT -p tcp --dport 22 -j ACCEPT -m comment --comment "Allow SSH connections from all hosts"
Run Code Online (Sandbox Code Playgroud)

您可能想让它只允许来自特定 IP 的连接:

iptables -I INPUT -s 10.10.10.10 -p tcp --dport 22 -j ACCEPT -m comment --comment "Allow SSH connections from the 10.10.10.10 host"
Run Code Online (Sandbox Code Playgroud)

如果您的服务提供商阻止端口 22,那么您可能需要通过文件中的选项(通常位于)将服务置于不同的端口(端口2222非常流行)。Portsshd_config/etc/ssh