-
在 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
相关推荐
Huangjin007_4 分钟前
【Linux 系统篇(四十一)】线程(二):线程的优缺点、资源共享与上下文切换Rabitebla42 分钟前
【Linux】文件篇(六):Shell 实战——从系统调用到命令行重定向码云社区1 小时前
Spring Boot云快充协议源码:充电桩远程重启、参数配置与升级运维方案csdn_aspnet2 小时前
生产环境中 SQL Server 的 Docker 使用建议csdn_aspnet2 小时前
如何在 Docker 上安装 SQL Server分布式存储与RustFS2 小时前
Dify 容器一重建知识库就空了:把上传文件和密钥一起存进 RustFSDecay__11 小时前
PostgreSQL 数据库全栈运维实战系列 — 合集索引(24 篇)戊本菌11 小时前
进程(三)runqueue 与 Linux 的 O(1) 调度csdn_aspnet16 小时前
在 Docker 容器中本地运行 SQL Server叼烟扛炮17 小时前
[LangChain] 学习笔记(15):为什么你的重试没用——429、抖动、fallback 与熔断