zl程序教程

您现在的位置是:首页 >  后端

当前栏目

SSH连服务器时,连接不上,出现以下错误的原因与解决办法

ssh错误连接 出现 原因 解决办法 以下 不上
2023-06-13 09:15:03 时间

一.ssh: connect to host 192.168.110.249 port 22: Connection refused错误的原因与解决办法

在用 [ssh]远程登陆服务器时遇到如下问题:

ssh: connect to host 192.168.xxx.xxx port 22: Connection refused

解决方法:

  1. 检查是否安装了 [openssh]
ps -e | grep ssh

如果只出现了一个 ssh,说明没有安装 sshd, 使用命令安装 sshd 服务

apt-get install openssh-server

2.sshd 未启动

service sshd restart

ok,现在可以使用 ssh 连接了

二.“Permission denied,please try again” 错误的原因与解决办法

有时候我们需要使用 ssh 连接服务器,一般情况下可以正常连上,不过有时候还是会出现这个错误 “Permission denied,please try again”,错误原因: 服务器能拒绝,说明网络和 ssh 服务没有问题,出现这个问题的最可能的原因是: 1. 账号不存在; 2. 输入密码有误;3. 该账号被禁止登录了(如 sshd 配置文件中禁止 root 登录)。

解决方法:

  1. 服务器上用命令(id testroot)查看该用户名是否存在,如不存在,则创建该用户(useradd testroot),并为该用户设置密码(passwd testroot)
# id testroot
# useradd testroot
# passwd testroot
  1. 检查并确定密码没有错误
  2. 若是 root 用户登录提示上述错误,一般是配置文件中将 root 设置为不允许[远程登录],编辑 sshd 配置文件,将 PermitRootLogin 设置为 yes,以允许 root 登录。最后重启 sshd 服务(systemctl restart sshd)
vim /etc/ssh/sshd_config 
...
PermitRootLogin yes
...
systemctl restart sshd

ok, 问题解决

3.‘...Host key verification failed’

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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:W7VYm+7GNwzifa0h1l9hFWEzSBelJazMTXSYkBn5xyw.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /root/.ssh/known_hosts:2
  remove with:
  ssh-keygen -f "/root/.ssh/known_hosts" -R "192.168.110.249"
ECDSA host key for 192.168.110.249 has changed and you have requested strict checking.
Host key verification failed.

解决方法:

仔细分析了一下大概是因为 192.168.110.249 的主机密钥改了,而本机使用的还是原来的公钥与其匹配,因此会出现错误。一旦使用本机 ssh 连接过目标机,则会在~/.ssh/know_hosts 文件下生成目标机的公钥,以便下次可以直接使用。所以,我们可以把该文件下 192.168.110.249 对应的公钥删除掉,或者直接删除 know_hosts

rm -rf ~/.ssh/known_hosts