I'm a new user of Jenkins. I'm trying to run a shell script as part of the build step in Jenkins. This script has an scp command which transfers a .tar file to a remote server.

When I run this script, it works fine and gives the desired output, but when it is executed through Jenkins I get an error:

Host key verification failed.
lost connection

Up until the scp command, the script executes successfully, then skips the scp step.

I can't even SSH or SCP through Jenkins to the remote server.

Please advise what can be done here? What are the required plugins to be installed for ssh and scp?

  • I have created a new key-pair and the public key is added to the authorized_keys of the remote server. Using the private key I can SSH and SCP to the remote server, but it doesn't work while executing through Jenkins. – Sateesh Kumar Sahoo May 19 '16 at 14:24
  • You can setup a "credential" so that Jenkins can access repositories. The standard way seems to be to point Jenkins to the repository that it can access using scp/ssh and you trigger Jenkins to pull from the repository by hitting the project's built path using a trigger token and curl. – rupert160 May 19 '16 at 14:57

The hosts key needs to be added to the "known_hosts" file of the client, in your case the user, under whos account the jenkins server is run. So what you need to do is:

  1. Enable ssh login for the jenkins-user
  2. log in as jenkins
  3. ssh to the remote server
  4. answer the question "should the servers identity be added.." with yes..

Then you're done. Afterwards, the script will be able to do scp with the users pubkey.

| improve this answer | |
  • jenkins user is able to ssh and scp to the remote server. But its just not working in the build steps. Simple ssh and scp is not happening through jenkins either. – Sateesh Kumar Sahoo May 20 '16 at 6:15
  • Is it also possible to ssh to the remote server from the BUILD server? – gerhard d. May 20 '16 at 8:17
  • gerhard d, Yes I can ssh. It's just not working while executing the script in Jenkins. I'm working on a fix. if it works will post it here. Thanks – Sateesh Kumar Sahoo May 20 '16 at 8:35

In short you need to add the Jenkins' user ssh key to the server where you trying to log in, as that user.


Log in to jenkins server, then:

sudo passwd jenkins

enter the password you want to give the jenkins user twice, then:

su jenkins

Press enter for all the options. Then copy the new ssh keys to the server you want to copy to:

scp .ssh/id_rsa.pub user@newserver:/home/user/jenkins_key

If you are prompted press 'yes' to continue.

Then login to that server as that user, then:

ssh user@server

and enter password, then

cd .ssh
ls -lah

if there is no 'authorized_keys' then:

cd .ssh
touch authorized_keys

If the 'authorized_keys' file is there ignore the above step, then:

cat jenkins_key >> .ssh/authorized_keys
rm -rf ./jenkins_key

exit/ log out out of server Then back on the jenkins server:

ssh user@newserver

You should log in directly without prompts or entering your password

Now run the scp job again in Jenkins

| improve this answer | |

I got a fix to the issue. 1. I created a new shh key-pair under jenkins user. 2. Added the .pub key to the remote server. While doing ssh from the jenkins server the newly generated private key is used. Now both ssh and scp work as part of the build step. Thank you

| improve this answer | |

The insecure workaround is to use:

scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ${sourceFile} ${targetPath}

It's insecure, because the host key is never verified. This might lead to man-in-the-middle attacks. Still a trick one might want to have in the pocket to use in special cases and in isolated networks.

| improve this answer | |
  • Welcome to the site. Since you already mentioned that your approach is insecure, please include some explanation why this is the case, so that a potential user can assess the risks of following your advice. – AdminBee Jan 22 at 11:42
  • Good point. Thank you. – Marek Puchalski Jan 22 at 14:03

SSH Agent Plugin

This plugin allows you to provide SSH credentials to builds via a ssh-agent in Jenkins. https://plugins.jenkins.io/ssh-agent/

If you see this error again, run this command one time from Jenkins job:

ssh -o StrictHostKeyChecking=no user@host hostname

After that, you can simply run ssh from Jenkins job:

ssh user@host hostname
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.