ssh连接失败,排错经验

一、场景描述

ssh连接服务器,发现连接失败,但是对应服务器的ip能够ping通。

场景:

[[email protected] ~]# ssh [email protected]
ssh_exchange_identification: read: Connection reset by peer
[[email protected]-web ~]# ping 10.1.101.35
PING 10.1.101.35 (10.1.101.35) 56(84) bytes of data.
64 bytes from 10.1.101.35: icmp_seq=1 ttl=64 time=0.587 ms
64 bytes from 10.1.101.35: icmp_seq=2 ttl=64 time=0.722 ms
64 bytes from 10.1.101.35: icmp_seq=3 ttl=64 time=0.475 ms

ping是一个网络层的协议,只是表面网络在3层是通的;

ssh是应用层协议,具体还是从主机上找原因。

二、排错

1、ssh -v

用ssh -v去连有问题的服务器,会有比较详细的调试信息在屏幕上输出,可以帮助判断是哪一步出了问题。

主要是看是客户端还是服务器的问题。如果是客户端的问题,应该log中有写。如果是没有什么有用信息,就可能是服务器端出问题了。

[[email protected] ~]# ssh -v [email protected]
OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: Connecting to 10.1.101.35 [10.1.101.35] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
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_ed25519 type -1
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1
ssh_exchange_identification: read: Connection reset by peer

2、连接服务器

现在看起来是服务器出问题了,虽然不能ssh到服务器,但一般来说主机会提供一些方法比去让你连接,比如通过物理终端连进去,具体情况具体对待了,总之就是要连接到服务器上。

3、写一个排错弯路,但也是有用的

如果有debug1: Connection refused by tcp wrapper之类的log可参考这一步。

就是说你的客户端ip可能被服务器给禁掉了,fail2ban或者其他的程序可能把你的客户端ip扔到/etc/hosts.deny中了。

通过vi /etc/hosts.allow查看

加上一行sshd: ALL。

然后重启ssh。

#service sshd restart

如果问题真的出在ip被禁,这样重启之后应该就ok了。

客户端重新ssh试一下:

[[email protected] ~]# ssh -v [email protected]
OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: Connecting to 10.1.101.35 [10.1.101.35] port 22.
debug1: connect to address 10.1.101.35 port 22: Connection refused
ssh: connect to host 10.1.101.35 port 22: Connection refused

说明我的问题不在这里。

4、两条有用的命令

在服务器上执行下面命令可以显示ssh的所有 log。

centos系统如下:

#service sshd stop
#/usr/sbin/sshd -d

如果是ubuntu可能命令是:sudo service ssh stopsudo /usr/sbin/sshd -d

问题出现了: /var/empty/sshd must be owned by root and not group or world-writable。

是权限的问题了,查看一下。

我的权限变成777了,而sshd这个目录

正常情况该目录的权限应该是:

[[email protected] ~]# ll /var/empty/
total 0
drwx--x--x. 2 root root 6 May 13 03:41 sshd

修改权限:

改好权限后重启sshd服务【service sshd restart】。客户端再ssh就ok,大功告成了。

5、命令简介

至此问题已解决,但是/usr/sbin/sshd -d又是什么意思呢?

# man sshd看一下这两个参数。

     -D      When this option is specified, sshd will not detach and does not become a daemon.  This allows easy monitoring of sshd.

     -d      Debug mode.  The server sends verbose debug output to standard error, and does not put itself in the background.  The server also will not fork and will only process one connection.  This
             option is only intended for debugging for the server.  Multiple -d options increase the debugging level.  Maximum is 3.

-d是debug模式,服务器会向屏幕输出详细的debug信息,服务器只能有一个ssh链接。

三、题外话

虽然问题解决了,回想一下为什么会出这个问题?因为我昨天在解决一个日志权限的问题时,暴力的将每层目录权限都设置成777,想试探一下是否是目录读写权限的问题,然后再缩小权限,结果影响到了ssh登录。

想想解决一个问题若不能知道原理,很容易放大问题,甚至出现新bug。写代码需谨慎,排错需谨慎,知其然不知其所以然很重要。

参考:

Connection Reset By Peer

本文作者starof,因知识本身在变化,作者也在不断学习成长,文章内容也不定时更新,为避免误导读者,方便追根溯源,请诸位转载注明出处:http://www.cnblogs.com/starof/p/4709805.html有问题欢迎与我讨论,共同进步。

时间: 2024-07-29 07:18:26

ssh连接失败,排错经验的相关文章

ssh连接失败

参考:http://www.cnblogs.com/starof/p/4709805.html https://www.chenyudong.com/archives/ssh-using-private-public-key-no-password.html http://www.jianshu.com/p/35feb4ff79c5 ----------------------------------------------------------------------------------

关于Ubuntu远程SSH连接失败的问题

在做机器人项目的时候,用的是Ubuntu的linux,跟之前的CentOS的操作命令有一点差别,就比如防火墙的名字,在Ubuntu系统中叫什么ufw,真是有点不好接受. 为了能模拟环境,我又弄了一台电脑,但是老在两个电脑之间切换很费劲.于是想利用PieTTY这个软件进行远程的SSH连接.但是怎么都连接不上,老是denied.两个OS能相互的ping通,防火墙什么的都关闭了,怎么还能连不上呢?于是Google了一下. 网上有很多介绍在Ubuntu下开启SSH服务的文章,但大多数介绍的方法测试后都不

ssh密钥失败排错

环境说明: 1. centos 6.x 系统,使用root账户设置秘钥方式登录: 2. 在配置多台主机秘钥时,部分成功,部分不成功: 3. 安全日志 /var/log/secure 已被关闭,没有记录: 4. /etc/selinux/config中SELINUX=disabled. 这个问题困扰了比较长的时间,从网上搜,大部分是说权限的问题,但是我已经设置好了权限, 配置文件/etc/ssh/sshd_config也是一样的,没有理由部分主机可以登录,部分不能登录啊, 权限查看: ll -d

取消SSH连接中的StrictHostKeyChecking

默认配置的问题 SSH 连接远程主机时,会检查主机的公钥.如果是第一次该主机,会显示该主机的公钥摘要,提示用户是否信任该主机: The authenticity of host '192.168.0.110 (192.168.0.110)' can't be established. RSA key fingerprint is a3:ca:ad:95:a1:45:d2:57:3a:e9:e7:75:a8:4c:1f:9f. Are you sure you want to continue c

远程SSH连接服务与基本排错

为什么要远程连接Linux系统?? 在实际的工作场景中,虚拟机界面或物理服务器本地的窗口都是很少能够接触到的,因为服务器装完系统后,都要拉到IDC机房托管,如果是购买了云主机,更碰不到服务器本地显示器了,此时,只能通过远程连接的方式管理Linux系统.因此,在装好linux系统后,学习Linux运维的第一步应该是配置好客户端软件远程(通过ssh软件进行连接)连接Linux系统进行管理 telnet连接服务器是明文的,非加密的; 默认为23端口 SSH连接服务器是加密的连接; 默认为22端口 ss

xshell通过ssh连接Linux失败(非网络问题)

+++++++++++++++++++++++++++++++++++++++++++ 标题:xshell通过ssh连接Linux失败(非网络问题) 时间:2019年6月20日 +++++++++++++++++++++++++++++++++++++++++++ 在学习系统防火墙(iptables)时,个人在自己搭建的虚拟机上调整防火墙策略(有的策略是通过iptables命令调整,有的是通过直接修改iptables策略配置文件进行调整).再一次调整完防火墙后再通过xshell远程连接软件连接虚

ssh远程连接失败

使用ssh连接远程主机时,结果提示如下的错误: 大概意思就是说密钥权限的文件不能为777,不能被其他用户读取. 了解了问题后,修改密钥文件权限为700,即:(chmod 700 秘钥)就OK了.

Linux系统下的ssh使用(依据个人经验总结)

对于linux运维工作者而言,使用ssh远程远程服务器是再熟悉不过的了!对于ssh的一些严格设置也关系到服务器的安全维护,今天在此,就本人工作中使用ssh的经验而言,做一些总结记录来下. (1)ssh远程登陆后的提示信息,标题信息我们经常会使用中控机ssh信任跳转到其他机器上,但是不知道有没有运维朋友注意到ssh跳转成功后的终端显示的提示信息?这些提示信息,是为了方便我们在第一时间知道ssh跳转到哪台目标机上,也是为了避免长期频繁跳转后由于大意造成的误入机器操作的风险,我们通常会在ssh跳转到目

ECS云主机SSH连接提示“Connection reset by peer”的解决办法和解决思路

三周前刚从上家公司换到新的公司,这家公司与上家公司相比对阿里云的云计算环境更加的依赖,使用的ECS实例和其他服务如SLB.RDS.OSS等更多了一个数量级.这篇文章的背景就是为了解决阿里云ECS云主机SSH连接的一个问题,从故障发现到故障排除到最后反思的一个详细过程.文章比较长,图片众多,建议有时间仔细阅读,没时间就阅读文末的"总结和反思"部分即可. 故障发现: 2017-05-23 下午17:00点前同事报告称GitLab所在的服务器访问出现异常.经查发现在公司内无法正常通过SSH连