首页 > 其他分享 >ssh连接失败,排错经验

ssh连接失败,排错经验

时间:2024-08-09 17:49:20浏览次数:5  
标签:debug1 10.1 sshd 排错 101.35 ssh root 连接

一、场景描述

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

场景:

复制代码
[root@yl-web ~]# ssh [email protected]
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 [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试一下:

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

如果觉得本文对您有帮助~可以微信支持一下:

标签:debug1,10.1,sshd,排错,101.35,ssh,root,连接
From: https://www.cnblogs.com/gaoyanbing/p/18351220

相关文章

  • 零代码连接 OneNet 只需三分钟!一个安卓 APP 搞定 OneNet 物模型数据刷新与显示
    前言在物联网(IoT)开发中,快速连接设备与云平台、实现数据的实时刷新与显示,是开发者常常遇到的挑战。为此本文将展示如何在短短三分钟内,通过一个安卓APP轻松实现与OneNet的连接,并展示物模型数据。无论你是初学者还是有经验的开发者,这个简单的方法都能助你快速上手。什么......
  • 连接云数据库RDS for MySQL的全方位指南
    在云端高效管理数据,掌握连接RDSforMySQL的技巧一、应用程序访问VPC内RDSforMySQL实例的正确姿势确保您的应用程序所在的ECS与RDSforMySQL实例位于同一VPC。若不在,请调整VPC的路由表和网络ACL,以便ECS能够顺利访问RDSforMySQL实例。二、外部服务器访问云数据库RDSfor......
  • OpenSSH 信息泄漏漏洞 (CVE-2023-51385)【低可信】
    详细信息跳转此页面:https://blog.csdn.net/python10101/article/details/140083056?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7EPosition-3-140083056-blog-137541643.235%5Ev43%5Epc_blog_bottom_relevance_base3......
  • 使用两个连接的字符串调用变量 Python
    抱歉缺乏细节,因为我是python的初学者:c1=c2=c3=c4=c5=Falsex=int(input("Enteranumber1-5:"))ifx>5orx<1:print("Yournumbermustbebetween1and5")else:"c",x=True第8行是连接2个字符串的地方。我不确定......
  • MySQL4多表查询 内连接
    多表查询数据准备CREATEDATABASEdb4;USEdb4;--创建部门表createtableifnotexistsdept(deptnovarchar(20)primarykey,--部门号namevarchar(20)--部门名字);--创建员工表createtableifnotexistsemp(eidvarchar(20)primarykey......
  • Redis连接问题解决汇总
    Redis连接失败常见解决方案1.检查Redis命令行是否可以正常连接使用命令行客户端,输入:redis-cli-h虚拟机ip地址-p6379-aredis访问密码如若连接成功,输入ping,看控制台是否返回PONG此步骤若正常,则代表虚拟机可正常连接2.Redis命令行无法正常连接1)未打开Redis6379端口......
  • TCP 通信全流程分析:从连接建立到数据传输的深度探索
    目录一、TCP报头二、三次握手三、数据传输四、四次挥手本文通过一次TCP通信过程的分析来学习TCP协议一、TCP报头如图是一份TCP报文的报头,标准报头是20个字节,还可带有选项报头,也就是TCP报头的最小长度是20字节。以下是对报头的各个字段的分析:端口号:向上层交付时交付......
  • 【YashanDB数据库】PHP无法通过ODBC连接到数据库
    【问题分类】驱动使用【关键字】ODBC、驱动使用、PHP【问题描述】应用使用php-fpm+nginx架构,通过php的ODBC拓展连接YashanDB时出现报错:[unixODBC][DriverManager]Can'topenlib'/home/yashandb_odbc/libyas_odbc.so':filenotfound但是在应用所在的主机上使用isql连接Ya......
  • 构建包含 SSH 和 LAMP 服务的 Docker 镜像
    容器构建概述容器配置有SSHD服务,且root密码为启动容器是随机生成,加强安全性更新容器内软件源为国内镜像源,加快软件安装容器有安装LAMP服务(版本5.5.x),安装有各类php插件,可以用作安全靶场练习部分情况下,资源缺少,可把容器模拟为虚拟机entrypoint.sh文件内容#!/bin/bash#......
  • 关于java连接数据库时提示异常java.sql.SQLException: No suitable driver found for
    当我们测试一个新的数据库服务时,需要使用对方提供jdbc驱动来连接数据库,有时候简单的写个demo去连接,发现提示异常:java.sql.SQLException:Nosuitabledriverfoundforjdbc:jdbc:nuuv://10.1.8.99:8832/xxoo比如有以下程序连接数据库测试:publicstaticvoidmain(String[]a......