Jenkins服务器配置密钥对

  1. 在 Jenkins 服务器上执行以下命令

    生成 SSH 密钥对

    ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa -N ""

    查看公钥内容

    cat ~/.ssh/id_rsa.pub

  2. 将显示的公钥内容复制,然后在目标服务器上执行

    在目标服务器上执行

    mkdir -p /root/.ssh
    chmod 700 /root/.ssh
    echo "这里粘贴Jenkins服务器上生成的公钥" >> /root/.ssh/authorized_keys
    chmod 600 /root/.ssh/authorized_keys

  3. 在目标服务器上检查 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

  4. 配置完成后,在 Jenkins 服务器上测试连接

    在Jenkins服务器上执行

    ssh -v [email protected]

  5. 如果你远程属性PasswordAuthentication不小心设置成false,以下是解决方案

    pipeline {
    agent any

    复制代码
     environment {
         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'
                     }
                 }
             }
         }
     }

    }

相关推荐
K·Herbert10 小时前
最新CentOS 7 yum源失效的解决方案(2025年6月)
linux·运维·centos
别骂我h10 小时前
部署KVM虚拟化平台
linux·运维·服务器
繢鴻10 小时前
紧急救援!Ubuntu崩溃修复大赛
linux·服务器·ubuntu
showmethetime11 小时前
优化nginx参数(基本通用参数)
运维·nginx
老六ip加速器11 小时前
获取ip地址安全吗?如何获取静态ip地址隔离ip
运维·网络·智能路由器
Serendipity_筱楠11 小时前
Windows安装部署jenkins
windows·ci/cd·自动化·jenkins·测试
净心净意19 小时前
浅谈DaemonSet
运维·jenkins
Apex Predator1 天前
jenkins流水线打包vue无权限
运维·jenkins
哲讯智能科技1 天前
苏州SAP代理商:哲讯科技助力企业数字化转型
大数据·运维·人工智能
十五年专注C++开发1 天前
Qt .pro配置gcc相关命令(三):-W1、-L、-rpath和-rpath-link
linux·运维·c++·qt·cmake·跨平台编译