关于ssh无法登陆的排查思路


  • 青云

    SSH密码登陆
    使用场景:标准的 Linux 服务器连接方式。您可以通过各种 Linux SSH 客户端连接、登录 虚拟主机,比如putty、xshell等,进行服务器的运维和管理工作。
    SSH 密钥登陆
    使用场景:基于密钥对的免密码登录,降低了密码泄露的风险,提高了操作的安全性,同时也便于服务器的批量运维。
    故障原因:网络、服务器配置、SSH 服务配置等多种因素均可能会导致 SSH 连接或登录过程出现异常,原因不限于以下:
    1、 客户端软件或软件配置问题
    2、 客户端网络问题
    3、 中间链路问题
    4、 防火墙或系统内 iptables 等安全配置问题
    5、 Linux 系统 PAM 安全模块配置问题
    6、 Linux 系统环境配置问题
    7、 SSH 服务及参数配置问题
    8、 SSH 服务关联的目录或文件的属性配置问题
    9、 SSH 服务密钥配置问题
    10、私有网络的主机需要配置vpc/路由器的端口转发
    排除思路如下:
    如果是基础网络的主机
    1、 确认是否绑定了公网ip,绑定方式是否为外部绑定
    2、 通过本地ping 公网ip是否可以ping通(测试公网健康状态)
    3、 主机绑定的防火墙是否放行了下行22号端口
    4、 通过netstat -nptl命令查看sshd服务端口是否正常监听在0.0.0.0,如果监听的是本地,修改sshd_config配置并重启sshd
    5、 从本地telnet 公网ip的22号端口,测试是否可以通讯,如果不通,可能是内置防火墙的因素
    6、 通过命令iptables -L -n查看是否内置的iptables drop了sshd服务端口,如果是这个原因,可以放行端口或者关闭内置的iptables试下
    7、 如果上述登录测试正常,则说明 SSH 服务本身运行与监听是正常的。则需要通过具体的提示或者打印ssh debug日志去分析,在另一台linux主机使用ssh root@公网 -vvv ,贴一下debug日志

    (1)如果debug调试,有以下提示
    debug1: Trying private key: /root/.ssh/id_dsa
    debug3: no such identity: /root/.ssh/id_dsa
    debug1: Trying private key: /root/.ssh/id_ecdsa
    debug3: no such identity: /root/.ssh/id_ecdsa
    debug2: we did not send a packet, disable method
    debug3: authmethod_lookup password
    1、 有可能是ssh 密码不对,让用户重新确认一下密码
    2、 有可能是禁止root用户ssh登陆,通过web console登陆到主机,修改sshd_config文件PermitRootLogin yes
    并重启sshd
    (2)如果debug调试,有以下提示
    debug1: Trying private key: /root/.ssh/id_rsa
    debug3: no such identity: /root/.ssh/id_rsa
    debug1: Trying private key: /root/.ssh/id_dsa
    debug3: no such identity: /root/.ssh/id_dsa
    debug1: Trying private key: /root/.ssh/id_ecdsa
    debug3: no such identity: /root/.ssh/id_ecdsa
    debug2: we did not send a packet, disable method
    debug1: No more authentication methods to try.
    Permission denied (publickey)
    这个就可以判断用户绑定了ssh秘钥,禁止了ssh 密码登陆,让用户修改sshd_config的配置文件,修改
    PasswordAuthentication yes,并重启sshd服务
    或者让用户使用ssh 秘钥登陆

    (3)如果debug日志显示
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: Applying options for *
    debug2: ssh_connect: needpriv 0
    debug1: Connecting to 139.198.176.22 [139.198.176.22] port 2200.
    debug1: connect to address 139.198.176.22 port 2200: Connection refused
    ssh: connect to host 139.198.176.22 port 2200: Connection refused
    这个很大可能就是用户修改了sshd端口,并在外部防火墙或者内置iptables设置了端口访问源ip,取消限制即可
    (4)登陆的时候显示
    ssh ubuntu@139.198.18.155 -p 2201
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
    Someone could be eavesdropping on you right now (man-in-the-middle attack)!
    It is also possible that a host key has just been changed.
    The fingerprint for the ECDSA key sent by the remote host is
    SHA256:xomZ2fDm/lz6lMMBuhDQVeapG+UZYLB0TMLT97vUGkM.
    Please contact your system administrator.
    Add correct host key in /Users/yangyuji/.ssh/known_hosts to get rid of this message.
    Offending ECDSA key in /Users/yangyuji/.ssh/known_hosts:51
    ECDSA host key for [139.198.18.155]:2201 has changed and you have requested strict checking.
    Host key verification failed.
    known_hosts是记录远程主机的公钥的文件,而保存的公钥还是本地的系统公钥而不是远程主机的,在ssh链接的时候首先会验证本地公钥,如果公钥不对,就会报错,解决办法可参考
    https://blog.csdn.net/nahancy/article/details/51052127

    (5)ssh秘钥是注册给root用户,如果使用其他的用户登陆,需要查看.ssh/ authorized_keys的内容是否和公钥一致,如果为空,则需要cat id_rsa.pub >> /.ssh/authorized_keys 或者切换root用户使用ssh秘钥登陆主机

    (6)如果debug提示"debug3: Could not load “/home/z/.ssh/id_rsa” as a RSA1 public key”,有可能是用户修改了公钥认证的路径,比如放在了挂载盘而挂载盘没有mount成功,这种情况建议重新挂载硬盘就可以解决

    (7)ssh私钥登陆提示Server refused our key,确认秘钥是正确的,有可能是生成的~/.ssh/authorized_keys 文件没有selinux上下文属性,导致无法通过Selinux认证,解决办法是关闭selinux 使用命令setenforce 0
    (8)sshd为了安全,对属主的目录和文件权限有所要求。如果权限不对,则ssh的免密码登陆不生效。
    用户目录权限为 755 或者 700,就是不能是77x。
    .ssh目录权限一般为755或者700。
    rsa_id.pub 及authorized_keys权限一般为644
    rsa_id权限必须为600


登录后回复
 

与 青云QingCloud 社区 的连接断开,我们正在尝试重连,请耐心等待