-
在 Jenkins 服务器上执行以下命令
生成 SSH 密钥对
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa -N ""
查看公钥内容
cat ~/.ssh/id_rsa.pub
-
将显示的公钥内容复制,然后在目标服务器上执行
在目标服务器上执行
mkdir -p /root/.ssh
chmod 700 /root/.ssh
echo "这里粘贴Jenkins服务器上生成的公钥" >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys -
在目标服务器上检查 SSH 配置
在目标服务器上执行
cat /etc/ssh/sshd_config | grep -E "PubkeyAuthentication|PasswordAuthentication|PermitRootLogin"
如果需要修改配置
vi /etc/ssh/sshd_config
确保以下设置正确:
PubkeyAuthentication yes
PasswordAuthentication yes
PermitRootLogin yes重启SSH服务
systemctl restart sshd
-
配置完成后,在 Jenkins 服务器上测试连接
在Jenkins服务器上执行
ssh -v root@10.53.3.69
-
如果你远程属性PasswordAuthentication不小心设置成false,以下是解决方案
pipeline {
agent anyenvironment { TARGET_SERVER = 'ip' JENKINS_HOME = '/var/lib/jenkins' } stages { stage('Modify SSH Config') { steps { script { try { // 修改 SSH 配置,启用密码认证 sh ''' echo "=== 修改 SSH 配置 ===" ssh -F ${JENKINS_HOME}/.ssh/config ${TARGET_SERVER} "sudo sed -i 's/^PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config" echo "=== 重启 SSH 服务 ===" ssh -F ${JENKINS_HOME}/.ssh/config ${TARGET_SERVER} "sudo systemctl restart sshd" echo "=== 验证配置 ===" ssh -F ${JENKINS_HOME}/.ssh/config ${TARGET_SERVER} "cat /etc/ssh/sshd_config | grep PasswordAuthentication" ''' } catch (Exception e) { echo "Error: ${e.message}" currentBuild.result = 'FAILURE' } } } } }}
Jenkins服务器配置密钥对
TPBoreas2025-05-21 16:28
相关推荐
蓝速科技15 小时前
会议室门牌公告通知发布选型与落地指南丨蓝速科技Ivanqhz15 小时前
SVD++算法chicheese15 小时前
Linux 面试速查表:从初级到高级,附高频场景答题模板Linux-lucky16 小时前
32-38-Linux学习之旅之MySQL综合智能运维指南16 小时前
2026年ITSM系统怎么选?四款主流方案与五维评估模型我命由我1234516 小时前
Git 推送报错:error: src refspec main does not match any骇客野人17 小时前
测试环境MySQL迁移Vastbase(海量数据库)完整改造与落地实施方案迪康Defender17 小时前
终端安全实战:如何高效解决企业U盘泄密与管控难题Wang's Blog17 小时前
Java 项目实战: 外卖平台-删除分类的关联校验与自定义业务异常zly350017 小时前
VMware Converter Standalone 物理机转化为虚拟机后源1硬盘变成了2个硬盘(2个硬盘文件)虚拟机无法启动。