I'm trying to connect with a specific key on a server but for some reason I cannot connect with my private key if the public key exists next to it. Both clients and servers are Ubuntu 16.04.

The current situation: I have my ssh keys in my .ssh folder:

samk@local:~$ ls -ld .ssh
drwx------ 2 samk samk 4096 Oct 29 11:36 .ssh
samk@local:~$ ls -l .ssh/jenkins-slave*
-rw------- 1 samk samk 1679 Oct 24 13:23 .ssh/jenkins-slave_rsa
-rw------- 1 samk samk  429 Oct 24 12:09 .ssh/jenkins-slave_rsa.pub

The problem is I cannot connect using my ssh private key:

samk@local:~$ /usr/bin/ssh -i ~/.ssh/jenkins-slave_rsa ubuntu@jenkins-slave-01
Permission denied (publickey).

If I rename the public key to something else, I can connect:

samk@local:~$ mv .ssh/jenkins-slave_rsa.pub .ssh/jenkins-slave_other
samk@local:~$ /usr/bin/ssh -i  ~/.ssh/jenkins-slave_rsa  ubuntu@jenkins-slave-01

Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-137-generic x86_64)
Last login: Mon Oct 29 10:25:32 2018 from


If I rename back the public key, I cannot connect anymore.

What is happening here?

  • 2
    You appear to be going to 2 different hosts, jenkins-slave-01 in the first one (failed one), and jenkins-slave in the second one (good one). Also, you are invoking /usr/bin/ssh in the first one, and just ssh in the second one. Make sure you are running /usr/bin/ssh in both, and going to the same host in both. Also, if you want to ssh into a server without using a password, just the public key, the public key needs to be added into the remote users authorized_keys file. The best way to do that is to run ssh-copy-id. Hope this helps. – Lewis M Oct 29 '18 at 12:32
  • Hey Lewis, thank you for your response, that was just a typo, sorry about that. This is indeed the same host. I will correct my post. – SamK Oct 29 '18 at 13:30
  • The public key hosted locally happened to be the wrong one. Even if the file was wrong, I don't understand why ssh would refuse the connection. – SamK Oct 29 '18 at 14:42
  • try chmod 644 .ssh/jenkins-slave_rsa.pub – Alvin Liang Oct 29 '18 at 21:28

It happened that the public key (.ssh/jenkins-slave_rsa.pub) was not matching with the private key (.ssh/jenkins-slave_rsa.pub).

I discovered this while following the steps in this answer: https://serverfault.com/a/426429/30128

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.