ansible的Q&A:Failed to connect to the host via ssh: ssh_exchange_identification和Authentication or per

本文档记录了Ansible在使用过程中遇到的一些典型错误及其解决办法,包括连接失败、认证失败等问题,并提供了详细的定位和解决方案。

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

本文档记录ansible使用中遇到的错误及解决办法

 问题一:

 

[root@host-10-1-241-158 logs]# ssh 10.1.241.161

ssh_exchange_identification: read: Connection reset by peer

ips10.1.241.159 | UNREACHABLE! => {

    "changed": false,

    "msg": "Failed to connect to the host via ssh: ssh_exchange_identification: read: Connection reset by peer\r\n",

    "unreachable": true

}

[root@host-10-1-241-158 logs]# ansible root10.1.241.161   -m shell  -a 'pwd'

root10.1.241.161 | FAILED | rc=0 >>

MODULE FAILURE

 

[root@host-10-1-241-158 logs]# ansible   ips10.1.241.161    -m shell  -a 'pwd'

ips10.1.241.161 | UNREACHABLE! => {

    "changed": false,

    "msg": "Authentication failure.",

    "unreachable": true

}

 

 

定位是因为:

10.1.241.161主机上

vi /etc/hosts.deny

有sshd,sftp,telnet,ftp: 10.1.241.158

如何解除/etc/hosts.deny

161上执行:

[root@host-10-1-241-161 ~]# service denyhosts stop

sent DenyHosts SIGTERM

vi /etc/hosts.deny中,删除对应的主机信息

 

Authentication failure."报错是因为:/etc/ansible/hosts的用户,在被连接主机上没有sudo权限:

vi /etc/sudoers

添加对应权限即可。

 

Ansible报错:"Failed to connect to the host via ssh: ssh_exchange_identification: read: Connection reset by peer\r\n"

ips10.1.241.159 | UNREACHABLE! => {

    "changed": false,

    "msg": "Failed to connect to the host via ssh: ssh_exchange_identification: read: Connection reset by peer\r\n",

    "unreachable": true

}

 

解决方法:

service denyhosts stop

cat /dev/null  >/etc/hosts.deny

问题二:

ansible执行报错:Authentication or permission failure

+ ansible IPSMachines -m shell -a 'pwd'

root10.1.235.28 | UNREACHABLE! => {

    "changed": false,

    "msg": "Authentication or permission failure. In some cases, you may have been able to authenticate and did not have permissions on the remote directory. Consider changing the remote temp path in ansible.cfg to a path rooted in \"/tmp\". Failed command was: ( umask 77 && mkdir -p \"` echo $HOME/.ansible/tmp/ansible-tmp-1536806706.35-106960540689166 `\" && echo ansible-tmp-1536806706.35-106960540689166=\"` echo $HOME/.ansible/tmp/ansible-tmp-1536806706.35-106960540689166 `\" ), exited with result 1",

    "unreachable": true

}

root10.1.235.27 | UNREACHABLE! => {

    "changed": false,

    "msg": "Authentication or permission failure. In some cases, you may have been able to authenticate and did not have permissions on the remote directory. Consider changing the remote temp path in ansible.cfg to a path rooted in \"/tmp\". Failed command was: ( umask 77 && mkdir -p \"` echo $HOME/.ansible/tmp/ansible-tmp-1536806706.35-1073010745840 `\" && echo ansible-tmp-1536806706.35-1073010745840=\"` echo $HOME/.ansible/tmp/ansible-tmp-1536806706.35-1073010745840 `\" ), exited with result 1",

    "unreachable": true

}

 

 

【定位】原因是,被登录主机上的ansible用户的密码不对了;或者密码过期了;修改下对应用户的密码为正确密码即可;

回答: 出现"Failed to connect to the host via ssh: Permission denied (publickey)"的错误提示是因为SSH连接时权限被拒绝,可能是由于密钥配置不正确或者没有正确添加到系统中所致。\[1\]要解决这个问题,你可以按照以下步骤进行操作: 1. 确保你的SSH配置文件正确,包括正确指定了密钥文件端口号等信息。 2. 检查密钥文件的权限是否正确,一般来说,私钥文件的权限应该设置为600。 3. 确保你的公钥已经正确添加到目标主机的授权文件中,一般是~/.ssh/authorized_keys文件。 4. 如果你使用了ssh-agent来管理密钥,确保ssh-agent正在运行,并且你已经将密钥添加到ssh-agent中。你可以使用"ssh-add"命令来添加密钥到ssh-agent中。\[2\] 5. 如果你使用了ansible命令进行连接,确保你在ansible的配置文件中正确指定了密钥文件用户名等信息。同时,还要确保目标主机的SSH服务已经启动,并且允许使用密钥进行连接。\[3\] 通过检查以上步骤,你应该能够解决"Failed to connect to the host via ssh: Permission denied (publickey)"的问题。如果问题仍然存在,你可以进一步检查目标主机的SSH日志以获取更多的错误信息,以便更好地定位解决问题。 #### 引用[.reference_title] - *1* [ssh: connect to host localhost port 22: Connection refused](https://blog.csdn.net/mba1398/article/details/122829875)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Windows设置SSH出错,Permission denied (publickey),Error connecting to agent](https://blog.csdn.net/u013013023/article/details/109068239)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,pas](https://blog.csdn.net/asmartkiller/article/details/121714079)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值