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

    }

相关推荐
字节数据平台23 分钟前
刚刚,火山引擎多模态数据湖解决方案发布大数据运维Agent
大数据·运维·火山引擎
原神启动139 分钟前
Docker 场景化作业:生产环境容器操作实训
运维·docker·容器
云老大TG:@yunlaoda36044 分钟前
如何通过华为云国际站代理商CSBS进行备份策略设置?
运维·数据库·华为云
zly35002 小时前
linux查看正在运行的nginx的当前工作目录(webroot)
linux·运维·nginx
来自于狂人2 小时前
华为云Stack服务实例创建失败通用排查对照表(备考+生产故障定位必备)
服务器·数据库·华为云
QT 小鲜肉2 小时前
【Linux命令大全】001.文件管理之file命令(实操篇)
linux·运维·前端·网络·chrome·笔记
呼啦啦呼啦啦啦啦啦啦2 小时前
docker制作镜像的两种方式(保姆级教学)
运维·docker·容器
问道飞鱼3 小时前
【Linux知识】Linux 虚拟机磁盘扩缩容操作指南(按文件系统分类)
linux·运维·服务器·磁盘扩缩容
风雨飘逸3 小时前
【shell&bash进阶系列】(二十一)向脚本传递参数(shift和getopts)
linux·运维·服务器·经验分享·bash
zly35003 小时前
删除文件(rm 命令 删除目录)
linux·运维·服务器