Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ****

like image 520
Pranavadurai Avatar asked Apr 30 '18 17:04

Pranavadurai


People also ask

How do I remove host key verification?

You need to create a ~/. ssh/config file and disable strict host key checking by adding the content. This will disable host checking for all hosts you connect to. Rather than disabling host check for all Host “*”, it would be safer to specify a particular host.

What is host key verification?

In host key checking, ssh automatically maintains and checks a database containing identification for all hosts it has ever been used with. Host keys are stored in ~/. ssh/known_hosts in the user's home directory. Additionally, the /etc/ssh/ssh_known_hosts file is automatically checked for known hosts.


2 Answers

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

like image 175
Darrell Plessas Avatar answered Dec 05 '22 10:12

Darrell Plessas


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?

like image 27
Pranavadurai Avatar answered Dec 05 '22 11:12

Pranavadurai