一、场景描述

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

场景:

[root@yl-web ~]# ssh root@10.1.101.35
ssh_exchange_identification: read: Connection reset by peer
[root@yl-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中有写。如果是没有什么有用信息,就可能是服务器端出问题了。

[root@yl-web ~]# ssh -v root@10.1.101.35
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试一下:

[root@yl-web ~]# ssh -v root@10.1.101.35
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这个目录

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

[root@yl-web ~]# 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有问题欢迎与我讨论,共同进步。

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

  1. ssh连接失败,提示 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    [root@iZ2ze4kh1rvftq4cevdfjwZ ~]# ssh IP @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  2. ssh连接失败,排错经验(转)

    一.场景描述 ssh连接服务器,发现连接失败,但是对应服务器的ip能够ping通. 场景: [root@yl-web ~]# ssh root@10.1.101.35 ssh_exchange_ide ...

  3. 关于Ubuntu远程ssh连接失败的问题

    在做机器人项目的时候,用的是Ubuntu的linux,跟之前的CentOS的操作命令有一点差别,就比如防火墙的名字,在Ubuntu系统中叫什么ufw,真是有点不好接受. 为了能模拟环境,我又弄了一台电 ...

  4. ssh连接失败

    参考:http://www.cnblogs.com/starof/p/4709805.html https://www.chenyudong.com/archives/ssh-using-privat ...

  5. ssh连接失败, 记下来原因和解决方案

    mac下使用secureCRT发现连接不了虚拟机上的linux 运行 ps -e | grep ssh,查看是否有sshd进程 如果没有,说明server没启动,通过 /etc/init.d/sshd ...

  6. CentOS7主机SSH连接失败

    说来话长,之前20刀一年买bandwagon的廉价VPS,由于做了一些违法的事情,导致ip被封了. 检测ip被封的方法:进入ping.chinaz.com:输入IP地址,如果国外节点能够Ping通而国 ...

  7. ssh连接失败解决方法

    执行如下命令: ssh-keygen -t dsa -P '' -f /etc/ssh/ssh_host_dsa_key ssh-keygen -t rsa -P '' -f /etc/ssh/ssh ...

  8. ssh 连接失败 sz rz 安装

    sz 下载命令, rz上传命令的安装 sudo apt-get install lrzsz 1. 检查sshd服务的状态以及端口是否正常, 如下为正常状态 sudo netstat -nlp | gr ...

  9. Linux实战教学笔记05:远程SSH连接服务与基本排错(新手扫盲篇)

    第五节 远程SSH连接服务与基本排错 标签(空格分隔):Linux实战教学笔记-陈思齐 第1章 远程连接LInux系统管理 1.1 为什么要远程连接Linux系统 在实际的工作场景中,虚拟机界面或物理 ...

随机推荐

  1. 使用 WordPress 主题制作的20个精美网页

    WordPress 是一款个人博客系统,并逐步演化成一款内容管理系统软件,它是使用 PHP 语言和 MySQL 数据库开发的.用户可以在支持 PHP 和 MySQL 数据库的服务器上使用自己的博客.这 ...

  2. [deviceone开发]-do_QRCode的简单示例

    一.简介 do_QRCode组件可以用来生成二维码,识别二维码图片文件,这个示例直观的展示组件基本的使用方式. 二.效果图 三.相关下载 https://github.com/do-project/c ...

  3. 常让人误解的一道js小题

    一道小题引发的深思 今天无意中看到一个js笔试题,不由得想起初学js那会被各种题目狂虐的心酸,虽说现在也会被笔试题所虐,但毕竟比之前好了很多,下面就是我的个人理解,欢迎拍砖.指正: var x = 1 ...

  4. MS14-064 漏洞测试入侵win7

    Microsoft Windows OLE远程代码执行漏洞,OLE(对象链接与嵌入)是一种允许应用程序共享数据和功能的技术, 远程攻击者利用此漏洞通过构造的网站执行任意代码,影响Win95+IE3 – ...

  5. ubuntu制作本地源

    背景 平时apt-get install安装软件时,下载的deb文件都会存放在/var/cache/apt/archives/下,为了今后重装或给别人用,特别是没有网络时,这些deb文件实际上是可以派 ...

  6. CP强制覆盖

    发现在Fedora 10 /ubutun 里面用cp -fr src dest,即使加了-f也是不能强行覆盖的,这时怎么回事的呢?一两个文件还好说,就输几个yes吧,但是要是n多文件怎么办,那还不输死 ...

  7. 操作系统开发系列—10.内核HelloWorld ●

    a.我们先来体验一下在Linux下用汇编编程的感觉,见代码 [section .data] ; 数据在此 strHello db "Hello, world!", 0Ah STRL ...

  8. <转>最新版SDWebImage的使用

    我之前写过一篇博客,介绍缓存处理的三种方式,其中最难,最麻烦,最占内存资源的还是图片缓存,最近做的项目有大量的图片处理,还是采用了SDWebImage来处理,但是发现之前封装好的代码报错了.研究发现, ...

  9. 【CoreData】分页查询和模糊查询

    在CoreData实际使用中,分页查询和模糊查询是必不可少的,接下来演示一下: 首先 // 1.创建模型文件 (相当于一个数据库里的表) // New File ———— CoreData ———— ...

  10. iOS cookie问题

    获取cookie时汉字应转换为UTF8格式