Jenkins 推送报错 - SSH 密钥失效


目录


问题描述

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 [email protected]:/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

相关推荐
LaughingZhu3 小时前
PH热榜 | 2025-04-24
运维·经验分享·搜索引擎·产品运营·jenkins
去伪存真5 小时前
现学现用之Jenkins--从泛泛了解到实操
前端·jenkins
TracyCoder1237 小时前
ElasticSearch深入解析(五):如何将一台电脑上的Elasticsearch服务迁移到另一台电脑上
大数据·elasticsearch·jenkins
木心9 小时前
Github两种鉴权模式PAT与SSH
ssh·github
韶君14 小时前
Jenkins(CI/CD工具)
ci/cd·jenkins
极小狐1 天前
如何创建并使用极狐GitLab 部署令牌?
运维·git·ssh·gitlab·github
PLUS_WAVE1 天前
【SSH 端口转发】通过SSH端口转发实现访问远程服务器的 tensorboard
linux·服务器·ssh·软件工程·端口转发·tensorboard
极小狐1 天前
极狐GitLab 的压缩和合并是什么?
运维·git·ssh·gitlab·github
极小狐2 天前
极狐GitLab 合并请求依赖如何解决?
运维·git·ssh·gitlab·github
Cloud_Air7542 天前
从零开始使用SSH链接目标主机(包括Github添加SSH验证,主机连接远程机SSH验证)
运维·ssh