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 root@10.53.3.69

  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'
                     }
                 }
             }
         }
     }

    }

相关推荐
林义满2 分钟前
大促零宕机背后的运维升级:长三角中小跨境电商的架构优化实践
大数据·运维·架构
linweidong37 分钟前
顺丰运维面试题及参考答案
运维·nginx·容器·ansible·运维开发·防火墙·python面试
qq_4557608538 分钟前
docker run
运维·docker·容器
叽里咕噜怪38 分钟前
Docker-基础
运维·docker·容器
田里的水稻1 小时前
运维_SOC芯片的架构综述
运维·架构
不染尘.1 小时前
UDP客户服务器模型和UDP协议
服务器·网络·网络协议·计算机网络·udp
太行山有西瓜汁1 小时前
达梦DTS工具:批量导出与导入DDL脚本完整指南
运维·服务器·数据库
蓝晶之心1 小时前
Linux rsync ssh方式数据备份
linux·运维·服务器
fengyehongWorld1 小时前
Linux sudo命令
linux·运维·服务器
WG_172 小时前
Linux:缓冲区_glibc封装
linux·运维·服务器