解决ssh不能添加已访问的主机到信任列表的问题(Failed to add the host to the list of known hosts) ....

本文解决了Gentoo系统中SSH无法将已验证的主机添加到信任列表的问题。通过调整.ssh目录权限,成功避免了重复的签名验证提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决ssh不能添加已访问的主机到信任列表的问题(Failed to add the host to the list of known hosts)

在ssh访问一个服务器的时候第一次会提示签名验证,只要同意之后就会将这个host添加到~/.ssh中的known_hosts中,以后再连接就不会再出现提示了。
原来在ubuntu和freebsd下都没有这个问题。在Gentoo中就有点问题。今天解决了一下,在gentoo-user的mail list中找到了答案。
正常的情况应该是这样的:
rory@gentoo ~ $ ssh xxx.xxx.com
The authenticity of host 'xxx.xxx.com (218.xxx.xxx.xxx)' can't be established.
RSA key fingerprint is 6e:6b:0f:2a:b8:91:3f:c8:f0:39:e4:df:b4:d8:16:6b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xxx.xxx.com,218.xxx.xxx.xxx' (RSA) to the list of known hosts.
rory@xxx.xxx.com's password:

可是Gentoo中每次都出现:
rory@gentoo ~ $ ssh xxx.xxx.com
The authenticity of host 'xxx.xxx.com (218.xxx.xxx.xxx)' can't be established.
RSA key fingerprint is 6e:6b:0f:2a:b8:91:3f:c8:f0:39:e4:df:b4:d8:16:6b.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts
rory@xxx.xxx.com's password:

根据这位作者的回复:
http://www.mail-archive.com/gentoo-user@lists.gentoo.org/msg55719.html
可能是因为使用root用户来移动过home目录,应该修改一下.ssh的用户和权限
  1. chownusername:/home/username/.ssh
  2. chownusername:/home/username/.ssh/*
  3. chmod700/home/username/.ssh
  4. chmod600/home/username.ssh/*
chown username: /home/username/.ssh
chown username: /home/username/.ssh/*
chmod 700 /home/username/.ssh
chmod 600 /home/username.ssh/*

可是我看我的/home/rory/.ssh目录的用户和组都是正常的。所以就是因为目录的权限不对。对照作者说的我的.ssh没有x权限。所以我执行了 chmod 700 /home/rory/.ssh 。这样一切就正常了。
在ssh访问一个服务器的时候第一次会提示签名验证,只要同意之后就会将这个host添加到~/.ssh中的known_hosts中,以后再连接就不会再出现提示了。
原来在ubuntu和freebsd下都没有这个问题。在Gentoo中就有点问题。今天解决了一下,在gentoo-user的mail list中找到了答案。
正常的情况应该是这样的:
rory@gentoo ~ $ ssh xxx.xxx.com
The authenticity of host 'xxx.xxx.com (218.xxx.xxx.xxx)' can't be established.
RSA key fingerprint is 6e:6b:0f:2a:b8:91:3f:c8:f0:39:e4:df:b4:d8:16:6b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xxx.xxx.com,218.xxx.xxx.xxx' (RSA) to the list of known hosts.
rory@xxx.xxx.com's password:

可是Gentoo中每次都出现:
rory@gentoo ~ $ ssh xxx.xxx.com
The authenticity of host 'xxx.xxx.com (218.xxx.xxx.xxx)' can't be established.
RSA key fingerprint is 6e:6b:0f:2a:b8:91:3f:c8:f0:39:e4:df:b4:d8:16:6b.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts
rory@xxx.xxx.com's password:

根据这位作者的回复:
http://www.mail-archive.com/gentoo-user@lists.gentoo.org/msg55719.html
可能是因为使用root用户来移动过home目录,应该修改一下.ssh的用户和权限
  1. chownusername:/home/username/.ssh
  2. chownusername:/home/username/.ssh/*
  3. chmod700/home/username/.ssh
  4. chmod600/home/username.ssh/*
chown username: /home/username/.ssh
chown username: /home/username/.ssh/*
chmod 700 /home/username/.ssh
chmod 600 /home/username.ssh/*

可是我看我的/home/rory/.ssh目录的用户和组都是正常的。所以就是因为目录的权限不对。对照作者说的我的.ssh没有x权限。所以我执行了 chmod 700 /home/rory/.ssh 。这样一切就正常了。
<think>好的,我现在需要解决用户遇到的SSH警告问题,提示“Add correct host key in /root/.ssh/known_hosts to get rid of this message”。首先,我得理解这个错误的原因。用户可能在尝试连接到一个远程服务器时,SSH客户端检测到主机密钥发生了变化,或者这是第一次连接,密钥未被记录。 接下来,我应该分步骤解释解决方法。首先,用户需要定位到相关的known_hosts文件,尤其是root用户下的路径。然后,可能需要删除旧的密钥条目或者整个文件。不过,直接删除整个文件虽然有效,但会清除所有已保存的主机密钥,不是最安全的方法。因此,更好的办法是找到对应的行删除。 另外,用户可能需要更新主机密钥。这时候,使用ssh-keyscan命令获取新的密钥并替换旧的条目会更合适。同时,需要提醒用户注意潜在的安全风险,比如中间人攻击,确保他们确实信任主机。 在结构上,应该先解释错误的原因,再逐步给出解决方案,包括不同情况下的处理方法。最后,强调安全性,让用户明白每一步操作的意义,避免盲目执行命令。还要检查是否有遗漏的细节,比如权限问题或者是否需要重启服务等。确保回答清晰、准确,并且用户能够安全地解决问题。</think>当你在使用SSH连接远程服务器时出现提示"Add correct host key in /root/.ssh/known_hosts to get rid of this message",这意味着本地存储的主机密钥与远程服务器提供的密钥不匹配。以下是分步解释和解决方法: --- ### **原因分析** 1. **首次连接新主机**:SSH首次连接时会自动保存主机密钥到`known_hosts`文件 2. **服务器密钥变更**:常见于以下情况: - 服务器操作系统重装 - SSH服务配置重置 - 服务器IP地址被其他设备占用 --- ### **解决方法** #### **方案1:删除旧记录(推荐)** ```bash # 定位到对应行号后删除(以第15行为例) sudo sed -i '15d' /root/.ssh/known_hosts # 或者直接删除整个文件(慎用) sudo rm /root/.ssh/known_hosts ``` #### **方案2:强制更新密钥** ```bash # 获取新密钥并追加到文件 sudo ssh-keyscan -H your_server_ip >> /root/.ssh/known_hosts ``` --- ### **安全警告** 1. **密钥验证机制**:SSH通过主机密钥验证防止中间人攻击(Man-in-the-Middle) 2. **风险提示**:若不确定服务器是否合法,应联系管理员确认密钥变更原因 3. **权限问题**:操作`/root`目录需要`sudo`权限 --- ### **操作验证** 重新执行SSH连接命令: ```bash ssh root@your_server_ip ``` 此时应该不再出现密钥警告,如果仍有问题,请检查: - 服务器IP/DNS是否输入正确 - 服务器防火墙是否开放SSH端口(默认22) - 本地网络连接是否正常 建议定期使用`ssh-keygen -F your_server_ip`检查密钥指纹是否与服务器一致。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值