173

I have a problem with jenkins, setting "git", shows the following error:

Failed to connect to repository : Command "git ls-remote -h https://person@bitbucket.org/person/projectmarket.git HEAD" returned status code 128:
stdout:
stderr: fatal: Authentication failed

I have tested with ssh:

git@bitbucket.org:person/projectmarket.git

This is error:

Failed to connect to repository : Command "git ls-remote -h git@bitbucket.org:person/projectmarket.git HEAD" returned status code 128:
stdout:
stderr: Host key verification failed.
fatal: The remote end hung up unexpectedly

I've also done these steps with "SSH key".

Login under Jenkins

sudo su jenkins

Copy your github key to Jenkins .ssh folder

cp ~/.ssh/id_rsa_github* /var/lib/jenkins/.ssh/

Rename the keys

mv id_rsa_github id_rsa
mv id_rsa_github.pub id_rsa.pub

but still not working git repository in jenkins.

thanks by help!.

A. M. Mérida
  • 2,160
  • 3
  • 14
  • 24

14 Answers14

191

Change to the jenkins user and run the command manually:

git ls-remote -h git@bitbucket.org:person/projectmarket.git HEAD

You will get the standard SSH warning when first connecting to a new host via SSH:

The authenticity of host 'bitbucket.org (207.223.240.181)' can't be established.
RSA key fingerprint is 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40.
Are you sure you want to continue connecting (yes/no)?

Type yes and press Enter. The host key for bitbucket.org will now be added to the ~/.ssh/known_hosts file and you won't get this error in Jenkins anymore.

ctc
  • 2,223
  • 1
  • 14
  • 17
  • 6
    show this: Permission denied (publickey). fatal: The remote end hung up unexpectedly – A. M. Mérida Mar 04 '13 at 08:40
  • 6
    Right, but that's an entirely different error. Now you have to go add your public key to the repository over at bitbucket.org. – ctc Mar 04 '13 at 16:57
  • 4
    See step 6 here: https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git – ctc Mar 04 '13 at 16:58
  • Please accept the answer if the error message went away. Judging by your comment, you are no longer getting the 'host key verification failed' error. The error you're now getting is an authentication error. You have to add your public key on your bitbucket.org repository. – ctc Mar 05 '13 at 18:00
  • I had to do this and restart my Jenkins app server (Glassfish) for this to work. – user424174 Jun 12 '13 at 22:58
  • 2
    +1 for "worksforme", had the exact same problem. Created id_rsa for the proper user, chmod-ed to jenkins, added the public key, still did not work. Trying git as sudo -u jenkins yielded the knonwn_hosts file, fixing the problem. – sibidiba Jun 13 '13 at 14:11
  • This worked for me for Jenkins.app on OS X, but you don't need to change to the jenkins user. Just run it from your own user account – James Zaghini Feb 03 '14 at 22:43
  • 3
    You need to run it as the user that is running Jenkins. On most systems, one typically runs it as a separate user (e.g. a 'jenkins' user). Hence, you would need to switch to that user to ensure that the address for bitbucket.org is added to ~/.ssh/known_hosts. – ctc Feb 04 '14 at 02:41
  • What this line means: Change to the jenkins user and run the command manually: I am window OS user. – abdulbasit May 16 '16 at 08:40
  • Modifying the known_hosts file to work for bitbucket worked for me when I had the same error using mercurial – CamHart May 31 '16 at 21:27
  • @CamHart Yes, you can edit it manually, if you know the server's key. The instruction provided updates known_hosts for you. – ctc Jun 01 '16 at 06:37
  • 2
    did you know you can use `ssh-keyscan` for this? example: `ssh-keyscan -t rsa bitbucket.org >> ~/.ssh/known_hosts` – slf Dec 27 '16 at 09:01
  • An important addition: this holds true *even if you run the job as a 'specific user'*! Despite indicating that the job runs as that user, the known hosts from the jenkins user are used to validate outgoing ssh connections. – Rick Moritz Jun 29 '17 at 12:32
  • 1
    what is the jenkins password? – IceFire Oct 03 '19 at 11:46
  • I have a similar requirement. If i"m running `git ls-remote ..` command through a script, how can i enter `yes` to the standard SSH warning from the script itself. – Yash Apr 24 '20 at 08:42
  • It asks for a password that i don't know, it's not any of the passwords for git users, jenkins users, or anything else. – Owl Feb 01 '21 at 17:19
  • @Owl The "git ls-remote" command? Then you haven't set up public key authentication properly. You need to associate the public key of the jenkins user (or whatever account is running Jenkins) with your Bitbucket account. See the Bitbucket docs for details -- I don't use that service, but the same priniciple is true no matter what Git service you're using. – ctc Feb 02 '21 at 15:32
  • @ctc i ended up solving this by making a ssh key on the jenkins client and putting it into the keystore in the git server side, this solved my password issues. – Owl Feb 03 '21 at 09:11
  • 1
    @Owl Yep, that's the right approach. Glad you got it working. – ctc Feb 03 '21 at 21:02
51

Jenkins is a service account, it doesn't have a shell by design. It is generally accepted that service accounts. shouldn't be able to log in interactively.

To resolve "Jenkins Host key verification failed", do the following steps. I have used mercurial with jenkins.

1)Execute following commands on terminal

             $ sudo su -s /bin/bash jenkins

provide password

2)Generate public private key using the following command:

              ssh-keygen

you can see output as ::

Generating public/private rsa key pair. 
Enter file in which to save the key (/var/lib/jenkins/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

3)Press Enter --> Do not give any passphrase--> press enter

             Key has been generated

4) go to --> cat /var/lib/jenkins/.ssh/id_rsa.pub

5) Copy key from id_rsa.pub

6)Exit from bash

7) ssh@yourrepository

8) vi .ssh/authorized_keys

9) Paste the key

10) exit

11)Manually login to mercurial server

Note: Pls do manually login otherwise jenkins will again give error "host verification failed"

12)once manually done, Now go to Jenkins and give build

Enjoy!!!

Good Luck

fedorqui 'SO stop harming'
  • 228,878
  • 81
  • 465
  • 523
29

Or you can use:

ssh -oStrictHostKeyChecking=no host

This will be insecure (man in the middle attacks) but easiest solution.

The better way to do that is to generate correct mappings between host and ip address, so ssh will not complain:

#!/bin/bash

for domain in "github.com" "bitbucket.org"; do
    sed -i "/$domain/d" ~/.ssh/known_hosts
    line=$(ssh-keyscan $domain,`nslookup $domain | awk '/^Address: / { print $2 ; exit }'`)
    echo $line >> ~/.ssh/known_hosts
done

Excerpt from gist.

FelikZ
  • 2,672
  • 3
  • 29
  • 40
  • 3
    This opens you up to potential MITM attacks. – ctc Dec 07 '15 at 00:46
  • 1
    You can add -oStrictHostKeyChecking=no and connect once, which will add the host to the known_hosts file (same as when you ssh on the command line and type yes to add the key to the known_hosts file) and then remove this option after that. – krupan Jan 06 '16 at 17:39
  • Domain lookups are dicey because the hosts may be running on a cluster. The machine you got a minute ago could be a different box the next minute. – ingyhere Oct 21 '20 at 09:43
6

Had same problem, i fix it like that :

reset permission on id_rsa* only for current user no group no other

chmod o-rwx ~/.ssh/id*
chmod G-rwx ~/.ssh/id*

ls -lart ~/.ssh/


-rw-------  1 jenkins nogroup  398 avril  3 09:34 id_rsa.pub
-rw-------  1 jenkins nogroup 1675 avril  3 09:34 id_rsa

And clear ~/.ssh/know_hosts

Now Connect as jenkins

sudo su jenkins

Try the jenkins commands

git ls-remote -h git@bitbucket.org:user/project.git HEAD

If no problem appears, now jenkins will be able to connect the repo (for me ^^ at least)

Loodub
  • 61
  • 1
  • 2
  • 1
    The above command(chmod G-rwx ~/.ssh/id*) for changing the group permission failed. The below worked as desired. chmod g-rwx ~/.ssh/id* – samaitra May 15 '14 at 13:38
4

using https://bitbucket.org/YYYY/XX.git

you shoud delete username@

user7610
  • 17,196
  • 7
  • 97
  • 120
im9527
  • 41
  • 1
4

As for the workaround (e.g. Windows slave), define the following environment variable in global properties:

GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"

Jenkins, Global properties, Environment variables, GIT_SSH_COMMAND

Note: If you don't see the option, you probably need EnvInject plugin for it.

kenorb
  • 118,428
  • 63
  • 588
  • 624
3
  • Make sure we are not editing any of the default sshd_config properties to skip the error

  • Host Verification Failed - Definitely a missing entry of hostname in known_hosts file

  • Login to the server where the process is failing and do the following:

    1. Sudo to the user running the process

    2. ssh-copy-id destinationuser@destinationhostname

    3. It will prompt like this for the first time, say yes and it will also ask password for the first time:

      The authenticity of host 'sample.org (205.214.640.91)' can't be established.
      RSA key fingerprint is 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40.
      Are you sure you want to continue connecting (yes/no)? *yes*
      

      Password prompt ? give password

    4. Now from the server where process is running, do ssh destinationuser@destinationhostname. It should login without a password.

      Note: Do not change the default permissions of files in the user's .ssh directory, you will end up with different issues

baikho
  • 4,298
  • 3
  • 35
  • 40
GANESH
  • 51
  • 2
  • in step 3, it might be the case that, you are prompted with key already exist in the file, but you should continue this 4 steps and if you can login without password from jenkins user, than you are all set. – Rakibul Haq May 21 '19 at 10:50
  • Missing .pub file for jenkins user. – Owl Feb 01 '21 at 17:23
3

Copy host keys from both bitbucket and github:

ssh root@deployserver 'echo "$(ssh-keyscan -t rsa,dsa bitbucket.org)" >> /root/.ssh/known_hosts'
ssh root@deployserver 'echo "$(ssh-keyscan -t rsa,dsa github.com)" >> /root/.ssh/known_hosts'
Marco M
  • 1,125
  • 11
  • 12
3

I ran into this issue and it turned out the problem was that the jenkins service wasn't being run as the jenkins user. So running the commands as the jenkins user worked just fine.

Jeff Hutchins
  • 644
  • 7
  • 18
2

Best way you can just use your "git url" in 'https" URL format in the Jenkinsfile or wherever you want.

git url: 'https://github.com/jglick/simple-maven-project-with-tests.git'

Nitin
  • 7,219
  • 3
  • 15
  • 29
2

SSH

If you are trying it with SSH, then the Host key Verification error can come due to several reasons.Follow these steps to overcome all the reasons.

  1. Set the Environment variable as HOME and provide the address as the root directory of .ssh folder. e.g:- If your .ssh is kept inside Name folder. C:/Users/Name.
  2. Now make sure that the public SSH key is being provided in the repository link also. Either it is github or bitbucket or any other.
  3. Open git bash. And try cloning the project from the repository. This will help in adding your repository URL in the known_host file, which is being auto created in the .ssh folder.
  4. Now open jenkins and create a new job. Then click on configure.
  5. provide the cloning URL in Source code management under Git. The URL should be start with git@github.com/......... or ssh://proje........
  6. Under the Credential you need to add the username and password of your repository form which you are cloning the project. Select that credential.
  7. And now apply and save the configuration.
  8. Bingo! Start building the project. I hope now you will not get any Host Key verification error!
Prem Choudhary
  • 160
  • 1
  • 7
2
  1. login as jenkins using: "sudo su -s /bin/bash jenkins"
  2. git clone the desired repo which causes the key error
  3. it will ask you to add the key by showing Yes/No (enter yes or y)

that's it!

you can now re-run the jenkins job.

I hope you this will fix your issue.

  • No, it just asks for a password, and i tried the jenkins password and the git password and it doesn't accept either. – Owl Feb 01 '21 at 17:20
1

Try

ssh-keygen -R hostname

-R hostname Removes all keys belonging to hostname from a known_hosts file. This option is useful to delete hashed hosts

0

issue is with the /var/lib/jenkins/.ssh/known_hosts. It exists in the first case, but not in the second one. This means you are running either on different system or the second case is somehow jailed in chroot or by other means separated from the rest of the filesystem (this is a good idea for running random code from jenkins).

Next steps are finding out how are the chroots for this user created and modify the known hosts inside this chroot. Or just go other ways of ignoring known hosts, such as ssh-keyscan, StrictHostKeyChecking=no or so.