Canalización de Jenkins: scp intenta copiar a otro control remoto, falló la verificación de la clave del host

Canalización de Jenkins: scp intenta copiar a otro control remoto, falló la verificación de la clave del host

Yo uso Jenkins en el servidor Ubuntu. En este caso, quiero copiar un archivo a otro servidor remoto. Utilizo el comando SCP dentro de sshagent la canalización de Jenkins.

Probé la solución deincapaz de scp en jenkins, usuario ya creado: jenkins, guarde la clave pública en ubuntu@remoteip Allowed_host y su clave privada ssh se guarda en las credenciales de Jenkins con el ID jenkins-ssh-to-ubuntu.

También probé desde ssh directamente desde el servidor jenkins a la IP remota con el jenkinsusuario en el servidor jenkins, puede conectarse a la IP remota.

Cada vez que quiero ejecutar scpun comando en proceso, la consola devuelve un error. Pero cuando es solo el comando ssh habitual, cat atext.txtimprime el resultado. Aquí está el registro de consola de la canalización.

[Pipeline] sh
ssh -o StrictHostKeyChecking=no ubuntu@remoteip cat atext.txt
Failed to add the host to the list of known hosts (/var/lib/jenkins/.ssh/known_hosts).
{
  example: "it prints out the long text to the jenkins console output"
}
[Pipeline] sh
+ scp -r docker-compose-prod.yml ubuntu@remoteip:.
Host key verification failed.
lost connection

aquí está mi tubería

    stage('Copy requiredfile to deployment'){
        sshagent(['jenkins-ssh-to-ubuntu']){
            sh "ssh -o StrictHostKeyChecking=no ubuntu@remoteip atext.txt"
            sh "scp -r docker-compose-prod.yml ubuntu@remoteip:."
        }
    }

¿Cómo puedo resolver esto?

Actualizar: La verificación de la clave de host falló cuando uso scpLa pregunta es la misma que la mía, pero no tenía el mismo registro de consola, no hubo ninguna REMOTE HOST IDENTIFICATION HAS CHANGED!advertencia.

Comprobando el permiso en la máquina jenkins stat ~jenkins/.sshes 0700

  File: /var/lib/jenkins/.ssh/
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: fc01h/64513d    Inode: 265912      Links: 2
Access: (0700/drwx------)  Uid: (  111/ jenkins)   Gid: (  115/ jenkins)
Access: 2019-01-18 03:22:46.519541657 +0000
Modify: 2019-01-18 03:07:42.447547320 +0000
Change: 2019-01-18 03:07:42.447547320 +0000
 Birth: -

También actualicé el ejemplo, creo que hay algún comando que funciona y otro no.

Actualizar:Ejecutando ssh manualmente usando jenkinsel usuario

jenkins@ubuntu:/home/ubuntu$ ssh ubuntu@remoteip
The authenticity of host 'remoteip (remoteip)' can't be established.
ECDSA key fingerprint is SHA256:fingerprint.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/var/lib/jenkins/.ssh/known_hosts).
Enter passphrase for key '/var/lib/jenkins/.ssh/id_rsa':
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-141-generic x86_64) 

Después de ese mensaje de bienvenida, inicié sesión como ubuntu en la máquina remota.

este es el resultado dels -la /var/lib/jenkins/.ssh

jenkins@ubuntu-s-1vcpu-1gb-sgp1-01:/home/ubuntu$ ls -la /var/lib/jenkins/.ssh
total 24
drwx------  2 jenkins jenkins 4096 Jan 18 03:07 .
drwxr-xr-x 22 jenkins jenkins 4096 Jan 18 10:06 ..
-rw-------  1 jenkins jenkins 1766 Jan 18 03:07 id_rsa
-rw-r--r--  1 jenkins jenkins  416 Jan 18 03:07 id_rsa.pub
-rw-------  1 root    root     666 Jan  7 09:40 known_hosts
-rw-r--r--  1 jenkins jenkins  888 Dec 27 01:47 known_hosts.old

y este es el contenido de/etc/ssh/ssh_config

# This is the ssh client system-wide configuration file.  See
# ssh_config(5) for more information.  This file provides defaults for
# users, and the values can be changed in per-user configuration files
# or on the command line.

# Configuration data is parsed as follows:
#  1. command line options
#  2. user-specific file
#  3. system-wide file
# Any configuration value is only changed the first time it is set.
# Thus, host-specific definitions should be at the beginning of the
# configuration file, and defaults at the end.

# Site-wide defaults for some commonly used options.  For a comprehensive
# list of available options, their meanings and defaults, please see the
# ssh_config(5) man page.

Host *
#   ForwardAgent no
#   ForwardX11 no
#   ForwardX11Trusted yes
#   PasswordAuthentication yes
#   HostbasedAuthentication no
#   GSSAPIAuthentication no
#   GSSAPIDelegateCredentials no
#   GSSAPIKeyExchange no
#   GSSAPITrustDNS no
#   BatchMode no
#   CheckHostIP yes
#   AddressFamily any
#   ConnectTimeout 0
#   StrictHostKeyChecking ask
#   IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
#   IdentityFile ~/.ssh/id_ecdsa
#   IdentityFile ~/.ssh/id_ed25519
#   Port 22
#   Protocol 2
#   Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc
#   MACs hmac-md5,hmac-sha1,[email protected]
#   EscapeChar ~
#   Tunnel no
#   TunnelDevice any:any
#   PermitLocalCommand no
#   VisualHostKey no
#   ProxyCommand ssh -q -W %h:%p gateway.example.com
#   RekeyLimit 1G 1h
    SendEnv LANG LC_*
    HashKnownHosts yes
    GSSAPIAuthentication yes

Respuesta1

Su comando manual ssh ubuntu@remoteipno puede guardar la identificación del host como se muestra con

Failed to add the host to the list of known hosts (/var/lib/jenkins/.ssh/known_hosts).

porque este archivo es propiedad rootdel usuario y no puede escribirlojenkins

-rw-------  1 root    root     666 Jan  7 09:40 known_hosts

Primera ejecución comoroot

chown jenkins.jenkins /var/lib/jenkins/.ssh/known_hosts

luego ejecuta comojenkins

ssh ubuntu@remoteip

La primera vez debería guardar la identificación del host y la próxima vez no debería volver a preguntar. Después de esto, tu scpcomando debería funcionar.

información relacionada