-
在 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
相关推荐
zzzzzz31017 小时前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟XIAOHEZIcode18 小时前
Linux系统鼠标偏移常见原因以及修复方案用户0328472220701 天前
如何搭建本地yum源(上)大树884 天前
金刚石散热越强,管路越先见顶摇滚侠4 天前
Linux CentOS7 rpm 安装 MySQL 5.7霸道流氓气质4 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南小宇宙Zz4 天前
Maven依赖冲突Inhand陈工4 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案酣大智4 天前
ARP代理--工作原理shushangyun_4 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?