SSH into Digital Ocean error: “Permission denied (publickey)”

This snippet was borne from a roadblock I encountered when following the official Digital Ocean tutorial for “How to Set Up SSH Keys“. When following those steps I got stuck trying to copy the public key from my local computer to the DO Droplet with the response “Permission denied (publickey)”. Here is how I fixed the problem:

For those who are trying to add a new key to an existing droplet – READ THIS!!!!

If you are getting the “Permission denied (publickey)” when trying to copy your public key to the server, you likely have password authentication disabled in your droplet.

How to fix this:
Log in to your DigitalOcean account and open the console for the drop you are trying to access. While inside the console do the following:
sudo vim /etc/ssh/sshd_config
Scroll down to the very bottom and look for the following directives:
UsePAM yes
IgnoreUserKnownHosts no
PasswordAuthentication no

If UsePAM is set to ‘yes’ and PasswordAuthentication is set to ‘no’ change those to the opposite. It should read the following:
UsePAM no
IgnoreUserKnownHosts no
PasswordAuthentication yes

Then press esc, type : (colon), and type wq and hit enter

This will quit vim editor.

Now type reload ssh

Now go back to your terminal or putty and pick up at the step where it has you copying your local rsa public key to the server. You should now be presented with a request for a password instead of public key denied. Type in your droplet password and hit enter. You should know be inside your server.

Now you can change those settings back from Putty or Terminal by typing the following:
sudo vim /etc/ssh/sshd_config

Change the directives back to what they were and press esc, type : (colon), and type wq and hit enter

reload ssh Again.

Now you can quit the session by typing exit and try to login with just ssh by typing:
ssh username@dropletIP.

Now you should be able to access your server via SSH with your newly created SSH key!

This was a PITA to figure out, but that is what got it all done for me.


About The_Idiot

Self made millionaire.. Oops I mean web developer.. who started out learning how to build websites with tables and dreamweaver, I now almost entirely focus on CMS-based development, usually WordPress. I love coding and designing and regularly switch between my left and right brains.

7 thoughts on “SSH into Digital Ocean error: “Permission denied (publickey)”

  1. Thank you for this solution.  I added a new droplet and could not ssh into it with my existing key.

    These simple steps should be published on Digital Ocean’s help pages.

    Great work!

  2. Merijn

    Thanks! After two hours of bad luck this made my day!

  3. Here is nice cash prize for winning.

  4. […] hours of research and trial and error, I came across this article which helped me figure out a way to gain easy access to the droplets […]

  5. Luis Ramos

    Dear my droplet console is asking me a login, and I do not know from where I should get it

  6. Bakhtiar Dossul

    Thank you for the excellent tip! I got this figured out after spending a few hours of struggling. You da the man! 🙂

  7. Alex

    2 hours. I spent 2 hours trying to figure this out and reading Digital Ocean’s “haha, just plug and play – full stack development is so fun and easy!”

    This article saved my life. Seriously.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">