0

My Jenkins is hosted on Ubuntu 16. I need to copy some build files from Ubuntu to my Mac machine.

In "execute shell script" I am using following command:

scp -r /home/ben/GIT/dock/MySql ben@ipaddress:/Users/ben/Desktop/MySql

I am able to run the same command from the Ubuntu terminal. But when I execute the same command from Jenkins it shows following error in console output:

Started by user admin
Building in workspace /home/ben/GIT/dock
[dock] $ /bin/sh -xe /tmp/hudson1981676562360554479.sh
+ cd /home/ben/GIT/dock/
+ pwd
/home/ben/GIT/dock
+ scp -r /home/ben/GIT/dock/MySql ben@ip_address:/Users/ben/Desktop/MySql
Host key verification failed.
lost connection
Build step 'Execute shell' marked build as failure
Finished: FAILURE

What is causing this error?

Adrian
  • 42,911
  • 6
  • 107
  • 99
Prateek Naik
  • 2,522
  • 4
  • 18
  • 38
  • 3
    Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Jun 22 '17 at 19:04
  • @jww i know its for programming and development questions. But this is what i am facing. And how is this appears to be off topic? and i am clearly stating that i am able to do `scp` operation from terminal and facing problem while doing the same from Jenkins. – Prateek Naik Jun 22 '17 at 19:10
  • Did you try searching first? https://askubuntu.com/questions/45679/ssh-connection-problem-with-host-key-verification-failed-error – fukanchik Jun 22 '17 at 19:20
  • "What is causing this error?" - different user and different `.ssh` settings? When started from script both `ssh` and `scp` can't ask user for password or about host keys, so your .ssh should be correct. ssh/scp option of `-o StrictHostKeyChecking=no` may help. – osgx Jun 22 '17 at 19:20
  • Possible duplicate of [BitBucket: Host key authentication failed](https://stackoverflow.com/questions/40576718/bitbucket-host-key-authentication-failed) – Jakuje Jun 23 '17 at 06:47

3 Answers3

1

Try to add the ssh key in your target server. It should be somewhere /home/ben/.ssh/id_rsa.pub, copy the content and paste it to /Users/ben/.ssh/authorized_keys (in a new line don't delete what is already in this file)

But this work only if jenkins is running under the user Ben, otherwise you probably have to get the key from the user that is running Jenkins.

Crako
  • 73
  • 7
0

The answer is in your code. Host key verification failed. connect once with ssh to your server and accept the key. Then it should work. If you can't connect with SSH then fix that errors first.

Edit:

Connect with the Jenkins user. So change to that user first.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • 1
    Note that you'll need to connect once *as the user Jenkins runs as*. – Adrian Jun 22 '17 at 19:22
  • @Stony and Adrian. After executing `whoami` in jenkins shell executor it shows user as `jenkins`. Now jenkins is a service account and it doesn't have password. Without password how can i connect with SSH. – Prateek Naik Jun 22 '17 at 19:40
  • You can change to that user... get root and then change to the user... `su jenkins -s /bin/bash` – René Höhle Jun 22 '17 at 19:43
  • changed to `Jenkins` user and tried `jenkins@ip_address of ubuntu` from mac terminal and still its asking password. But when i tried to ssh from ubuntu to mac it is connecting. After building the job [these are the logs i got](https://gist.github.com/Beanben/a0752f84df9f86878e56494b659839a3) – Prateek Naik Jun 22 '17 at 20:07
  • sure generate a key for jenkins and add them to the remote server. – René Höhle Jun 22 '17 at 20:14
  • after generating key `.ssh` folder looks like `"config, id_rsa, id_rsa.pub, known_hosts"` but when i tried to connect i am getting error [like this](https://gist.github.com/Beanben/7d96892b2d185b20f4a0b2aaa9584031) – Prateek Naik Jun 23 '17 at 08:47
0

Try this, actually you may have generated the ssh-keygen as root user. But Build jobs are deployed as jenkins user.

Follow below steps:

login to jenkins server su jenkins ssh-keygen now copy the key from /var/jenkins_home/.ssh/id_rsa.pub to production server ~/.ssh/authorized_keys

Hint: Ensure that the key ends like this with jenkins name "jenkins@6cd0dfe4f9a8"