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

    }

相关推荐
KIDULT°1 小时前
MySQL系列 (1):数据库原理
运维·数据库·mysql·云计算
wewowilling4 小时前
认识微服务
java·运维·微服务·springcloud
玖石书4 小时前
Ubuntu 24.04 UFW 全命令攻略:从入门到端口开放实战
linux·运维·ubuntu
weifont6 小时前
自动化抓取和上架商品:能不动手,就少动手
运维·前端·自动化
拂拉氏6 小时前
【知识讲解】 Linux之动静态库的了解
linux·运维·服务器·动静态库
Fcy6487 小时前
Linux下 五种IO模型的认识与⾮阻塞IO的实现
linux·服务器·io
Mikko77 小时前
Linux 重启后日志没了?进程被 OOM 杀了去哪看?logrotate 之后日志还往旧文件写?三台真机实测日志与性能排查
linux·运维·centos
光依旧7 小时前
发版就掉请求?Spring Boot优雅停机的四步
运维·spring boot·nginx·高可用·优雅停机·mcp server
yichengerp8 小时前
电子行业常用erp系统有哪些?推荐适合中小电子厂的易呈erp软件,操作简单,易落地
大数据·运维·人工智能·云计算·制造
天国梦9 小时前
国密门禁品牌怎么选?2026年最新3个避坑要点
运维·服务器·数据库·python