DEV Community

Cover image for DigitalOcean Permission denied (publickey)

DigitalOcean Permission denied (publickey)

Sailesh Choyal on July 11, 2020

Trying to access your DigitalOcean droplet and can't get beyond Permission denied (publickey) ? This could be due to multiple reasons, having th...
Collapse
 
andrerpena profile image
André Pena

This article was very useful, but to help people coming from Google, this error (Permission denied (publickey)) also happens if your identity is not set correctly.

When you do ssh_keygen it will generate a new SSH key, which the default name is id_rsa. If your computer happens to have another key already, with another name, your identity might be set to that key.

You should edit ~/.ssh/config, and make sure IdentityFile is set to the correct private key

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode
Collapse
 
laurianoelmiroduarte profile image
Lauriano Elmiro Duarte • Edited

I lost my access to DigitalOcean,
this tip of yours was my salvation, change the file name to id_rsa

Thanks

Collapse
 
hooloovoochimico profile image
Angelo Moroni

I solved using this solutions

Collapse
 
tcbutler320 profile image
Tyler Butler • Edited

Thanks for the quick solution! If your on a mac you wanna make this edit here /private/etc/ssh/ssh_config

Collapse
 
jfukura profile image
Jason Fukura

i literally created an account just to say thank you.

Collapse
 
3wcircus profile image
Kevin Yancy

Like other commenters, I also created an account just to say Thanks. Somehow I lost the ability to remote ssh into my droplet and your post gave me the fix I desperately needed in a clear and concise manner. Thank you again!

Collapse
 
shinokada profile image
shin • Edited

Thanks for sharing this.
I usually change this:

PermitRootLogin no
Enter fullscreen mode Exit fullscreen mode

I needed one more step.
Once you ssh to your server as a root, I need to copy my root ssh to /home/your-username/.ssh:
Run the following:

rsync --archive --chown=your-username:your-username ~/.ssh /home/your-username
Enter fullscreen mode Exit fullscreen mode

Now you can ssh using your name.

Collapse
 
n_sharadhi profile image
Sharadhi N

amazing! after sifting through 100s of pages talking about either solutions I already know or irrelevant ones, THIS IS THE ONLY THING THAT WORKED FOR ME.
DigitalOcean should include this in their docs. Thanks mate!

Collapse
 
segaz2002 profile image
Kolawole O. Gabriel

Nice, breakdown. Helped 👍

Collapse
 
watchermagic profile image
WatcherMagic

Had an issue with this at step 4, where the console still asked my user account for a password (not the root.) This means when I switched password authentication back to "no" I got the publickey error and was back to step 1. Solved it by:

  1. ssh into root (or use the digitalocean console)
  2. switch user: su - user
  3. sudo mkdir ~/.ssh
  4. sudo touch ~/.ssh/authorized_keys
  5. sudo vim ~/.ssh/authorized_keys then paste in the public key, save, exit
  6. confirm you can ssh without being prompted for a password
  7. sudo nano /etc/ssh/sshd_config --->...etc.

P.S If you're using a remote machine to access the droplet, make sure to GENERATE THE SSH KEY ON THE REMOTE MACHINE.

Collapse
 
ralpharama profile image
Ralph Capper

Thank you so much for this, helped me out.

Collapse
 
rvogel profile image
Rafael Vogel de Oliveira

Hey Sailesh, I've also just created an account to say thanks!!! You rock!

Collapse
 
shtabnoy profile image
Denis Shtabnoy

Thank you Sailesh. This instructions helped me to solve the issue!

Collapse
 
cazwazacz profile image
Allan Wazacz

Like a lot of people I created an account just to thank you! THANK YOU!

Collapse
 
kcbreuil profile image
Katie Breuil

I am getting a permission denied when I try to save the authorized key file in the second to last step. Thoughts?

Collapse
 
jartaud profile image
Josué Artaud

Thanks a lot. The browser console was turning me nuts.