Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins won't use SSH key

I'm sorry to have to ask this question, but I feel like I've tried every answer so far on SO with no luck.

I have my local machine and my remote server. Jenkins is up and running on my server.

If I open up terminal and do something like scp /path/to/file user@server:/path/to/wherever then my ssh works fine without requiring a password

If I run this command inside of my Jenkins job I get 'Host Key Verification Failed'

So I know my SSH is working correctly the way I want, but why can't I get Jenkins to use this SSH key?

Interesting thing is, it did work fine when I first set up Jenkins and the key, then I think I restarted my local machine, or restarted Jenkins, then it stopped working. It's hard to say exactly what caused it.

I've also tried several options regarding ssh-agent and ssh-add but those don't seem to work.

I verified the local machine .pub is on the server in the /user/.ssh folder and is also in the authorized keys file. The folder is owned by user.

Any thoughts would be much appreciated and I can provide more info about my problem. Thanks!

Update:

Per Kensters suggestion I did su - jenkins, then ssh server, and it asked me to add to known hosts. So I thought this was a step in the right direction. But the same problem persisted afterward.

Something I did not notice before I can ssh server without password when using my myUsername account. But if I switch to the jenkins user, then it asks me for my password when I do ssh server.

I also tried ssh-keygen -R server as suggested to no avail.

like image 609
Frankie Avatar asked Dec 24 '22 21:12

Frankie


2 Answers

Try

su jenkins
ssh-keyscan YOUR-HOSTNAME >> ~/.ssh/known_hosts

SSH Slaves Plugin doesn't support ECDSA. The command above should add RSA key for ssh-slave.

like image 149
abguy Avatar answered Jan 10 '23 04:01

abguy


Host Key Verification Failed

ssh is complaining about the remote host key, not the local key that you're trying to use for authentication.

Every SSH server has a host key which is used to identify the server to the client. This helps prevent clients from connecting to servers which are impersonating the intended server. The first time you use ssh to connect to a particular host, ssh will normally prompt you to accept the remote host's host key, then store the key locally so that ssh will recognize the key in the future. The widely used OpenSSH ssh program stores known host keys in a file .ssh/known_hosts within each user's home directory.

In this case, one of two things is happening:

  1. The user ID that Jenkins is using to run these jobs has never connected to this particular remote host before, and doesn't have the remote host's host key in its known_hosts file.
  2. The remote host key has changed for some reason, and it no longer matches the key which is stored in the Jenkins user's known_hosts file.

You need to update the known_hosts file for the user which jenkins is using to run these ssh operations. You need to remove any old host key for this host from the file, then add the host's new host key to the file. The simplest way is to use su or sudo to become the Jenkins user, then run ssh interactively to connect to the remote server:

$ ssh server

If ssh prompts you to accept a host key, say yes, and you're done. You don't even have to finish logging in. If it prints a big scary warning that the host key has changed, run this to remove the existing host from known_hosts:

$ ssh-keygen -R server

Then rerun the ssh command.

like image 37
Kenster Avatar answered Jan 10 '23 05:01

Kenster