jenkins ssh使用private-key登录

1、秘钥生成(必须使用这种方式生成、否则登录失败)

ssh-keygen -m PEM -t rsa -b 4096

2、秘钥生成后执行一下命令

cat id_rsa.pub >> authorized_keys

配置凭证

cat id_rsa 复制到private key中
jenkins ssh使用private-key登录_第1张图片

jenkins测试配置凭证

pipeline {
    agent any


    stages {
        stage('Build') {
            steps {
             test()
            }

            
        }
    }
}


def test(){
    
    // private-key
        def remote = [:]
        remote.name = "node-1"
        remote.host = "192.168.1.200"
        remote.allowAnyHosts = true
        withCredentials([sshUserPrivateKey(credentialsId: 'private-key', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
            remote.user = userName
            remote.identityFile = identity
            # sshCommand 必须写在withCredentials中
            sshCommand remote: remote, command:'ls -all'

            
        }


    
}


你可能感兴趣的:(jenkins,ssh,运维)