打赏

ssh连接报错

1. ssh Host key verification failed

问题:

ssh 报错:Host key verification failed

原因:

OpenSSH为避免受到DNS Hijack之类的攻击,会将访问过计算机的公钥(public key)都记录在~/.ssh/known_hosts。当下次访问相同计算机时,OpenSSH会核对公钥。如果公钥不同,OpenSSH会发出警告:Host key verification failed.

解决方案:

方案一:

# 忽略严格的校验
ssh -o StrictHostKeyChecking=no root@192.168.1.10

# 或者删除know_hosts文件
rm -f known_hosts

# 修改配置文件
vi /etc/ssh/ssh_config
StrictHostKeyChecking no
UserKnownHostsFile /dev/null

StrictHostKeyChecking变量来配置的。默认情况下,StrictHostKeyChecking=ask。简单所下它的三种配置值:

1.StrictHostKeyChecking=no

最不安全的级别,当然也没有那么多烦人的提示了,相对安全的内网时建议使用。如果连接server的key在本地不存在,那么就自动添加到文件中(默认是known_hosts),并且给出一个警告。

2.StrictHostKeyChecking=ask #默认的级别,就是出现刚才的提示了。如果连接和key不匹配,给出提示,并拒绝登录。

3.StrictHostKeyChecking=yes #最安全的级别,如果连接与key不匹配,就拒绝连接,不会提示详细信息

参考:https://cloud.tencent.com/developer/article/1529106

方案二:

SSH stores the host keys of the remote hosts in ~/.ssh/known_hosts. You can either edit that text file manually and remove the old key (you can see the line number in the error message)

ssh-keygen -R hostname
Removes all keys belonging to hostname from a known_hosts file. This option is useful to delete hashed hosts .

ssh-keygen -R hostname

参考:https://askubuntu.com/questions/45679/ssh-connection-problem-with-host-key-verification-failed-error

2.连接超时

方法一:

ssh -T -o ServerAliveInterval=60 -p 22 root@192.168.1.10 "sh /home/test.sh"

方法二:

服务端

vi /etc/ssh/sshd_config
# ClientAliveInterval: 每隔多少秒给SSH客户端发送一次信号
ClientAliveInterval 60
# ClientAliveCountMax: 请求多少次后都没有连接成功则断开与ssh客户端连接
ClientAliveCountMax 3

客户端

vi /etc/ssh/ssh_config
# ServerAliveInterval:每隔多少秒给SSH服务端发送一次信号
ServerAliveInterval 60
# ServerAliveCountMax:请求多少次后都没有连接成功则断开与ssh客户端连接
ServerAliveCountMax 3

ServerAliveInterval:客户端在向服务器发送空数据包之前(等待连接保持活动状态)将等待的秒数。

ClientAliveInterval:服务器在向客户端发送空数据包之前(等待连接保持活动状态)将等待的秒数。

将值设置为0(默认值)将禁用这些功能,因此如果空闲时间太长,连接可能会断开。

参考链接

https://blog.csdn.net/philosopher3610/article/details/84429389

https://qastack.cn/unix/3026/what-options-serveraliveinterval-and-clientaliveinterval-in-sshd-config-exac

posted @ 2021-11-16 16:35  苍山落暮  阅读(371)  评论(0编辑  收藏  举报