目录
问题描述
jenkins 构建完毕后,将构建好的 jar 包推送至远端服务器时,Deploy 阶段报如下错误:
sh
+ sshpass -p **** scp -o StrictHostKeyChecking=no -P 22 -r /data/jenkins/workspace/TAI/TAI/AllCam-tai-cloud/tai-131-lmm/service/target/lmm.jar root@xxx.xxx.xxx.xxx:/home/lmm/app/
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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:/Y24roRxGVOfviBZ/h6IKXgi7DEsDj4LDpJu6SPHq2k.
Please contact your system administrator.
Add correct host key in /var/lib/jenkins/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /var/lib/jenkins/.ssh/known_hosts:2
Password authentication is disabled to avoid man-in-the-middle attacks.
Keyboard-interactive authentication is disabled to avoid man-in-the-middle attacks.
Authorized users only. All activities may be monitored and reported.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
lost connection
报错原因
本文标题已给出 ------ "SSH 密钥失效"
。
先来看看 Pipeline 是怎么写的:
原因:其实 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
的问题显然就是因为目标服务器的主机密钥发生变化而,执行上图红框部分时导致的 SSH 连接报错,这是因为我重装了目标服务器操作系统(重装后 IP 不变),这就导致在 Jenkins 主机上 ~/.ssh/known_hosts
下的主机的记录失效,从而导致无法连接目标服务器。
解决方案
在每次执行发布时先执行下图红框部分指令,此时 SSH 工具会在 Known Hosts 文件中查找与 ${params.ServerIp}
匹配的条目,如果找到,SSH 工具会将该条目删除,这可以避免因为目标服务器的主机密钥发生变化而导致的 SSH 连接报错 - WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
。
---END