Pranavadurai
Pranavadurai

Reputation: 1030

SCP command fails to copy "Host key verification failed."

Hi I am new to Jenkins pipeline and I am trying to copy the file from one AWS Jenkins server to another AWS server. Both are Ubuntu servers. When I am running scp command in command line in Jenkins server it copies the file from the Jenkins server to destination server but in Jenkins pipeline it it showing "Host key Verification failed". What mistake I am doing ?

pipeline stage is

  stage('Deliver') {
        steps {
            sh 'scp -i /home/ubuntu/connec/new_one.pem **/target/*.jar [email protected]:/home/ubuntu'
        }
    }

error is

 scp -i /home/ubuntu/connec/new_one.pem **/target/*.jar [email protected]:/home/ubuntu

 Host key verification failed.

 lost connection

 script returned exit code 1

****Solved i have posted as a answer what i did. please suggest if i am wrong. Thanks ****

Upvotes: 9

Views: 14628

Answers (2)

Pranavadurai
Pranavadurai

Reputation: 1030

I found the all mistakes done by me. in this case. as i am new to this i don't aware of many things.so i like to mention the steps which are all done by me to complete this step. if anything i am doing is incorrect or which can be done in different way please suggest. Thanks.

1st was as Darrell mentioned in answers i added the -o "StrictHostKeyChecking=no" in my SCP command which was like this

   scp -v -o StrictHostKeyChecking=no -i /home/ubuntu/connec/new_one.pem **/target/*.jar [email protected]:/home/ubuntu

still i faced the error as

    Load key "/home/ubuntu/connec/new_one.pem": Permission denied

    debug1: No more authentication methods to try.

after searching about the error in google and stackoverflow i found that it's permission issue for the user of Jenkins. when i submit the command in terminal i was using the user as ubuntu but jenkin uses the user as "jenkins". so i have done 2 changes added sudo in my scp command

    sudo scp -v -o StrictHostKeyChecking=no -i /home/ubuntu/connec/new_one.pem target/*.jar [email protected]:/home/ubuntu

and then i faced that "Jenkins" user don't have authority to run sudo command so i have edited the file name "VISUDO"

 sudo visudo

and in last added the line

 jenkins ALL=(ALL) NOPASSWD: ALL

after that when i ran the pipeline it completed and copied the file...yaayyy.

Still i am wondering am i doing correctly?? any other way is there?

Upvotes: 7

Darrell Plessas
Darrell Plessas

Reputation: 211

scp -v is you friend here. This is the verbose flag for scp.

I suspect that the user you are testing with on the box and the user that Jenkins runs as are two different users, so the initial key exchange has never happened for the Jenkins user and Jenkins is headless so it has no ability to type yes to accept the host identification key.

If this is the case you can use the -o "StrictHostKeyChecking=no" option with scp to auto-accept and allow you to pass here.

Add the -v flag to your scp command and I bet we can see the what is going on in the output.

Here is the man page for scp

https://linux.die.net/man/1/scp

Good Luck

Upvotes: 15

Related Questions