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
 
alico profile image
Ali Akman

Recovering SSH Access on DigitalOcean (Clean Method)

I solved this using a cleaner method.

First, save the SSH key(s) you want to add into a file named ssh.txt. Upload this file to a location you can access publicly (for example: https://abc.com/ssh.txt).

Then go to DigitalOcean → Recovery.

Select Power > Turn Off, then click Boot from Recovery ISO.

After that, turn the server back on with Power > Power On and open the Recovery Console.

Steps

  1. From the options 1 to 7, press 1 and make sure the disk name /dev/vda1 is highlighted in green.
  2. Press 5 for “Attempt to chroot”.
  3. In the command line, run:

cd ~/.ssh

  1. Back up the existing file:

cp authorized_keys authorized_keys.backup

  1. Download your SSH key file:

wget -O ssh.txt https://abc.com/ssh.txt

  1. Append the SSH keys to authorized_keys:

cat ssh.txt >> authorized_keys

  1. Edit the SSH configuration:

nano /etc/ssh/sshd_config

Update the settings as follows:

PasswordAuthentication no     # Disable password authentication (temporarily)
PubkeyAuthentication yes     # Enable SSH key authentication
PermitRootLogin yes          # Enable root login
Enter fullscreen mode Exit fullscreen mode
  1. Restart the SSH service:
    systemctl restart sshd

  2. Verify the service status:
    service ssh status

Close the console window.

Then select Power > Turn Off, switch to Boot from Hard Drive, and click Power > Power On.

Local SSH Configuration

On your local machine, make sure the key defined in ~/.ssh/config under IdentityFile matches the same SSH key:

Host tomserver
    Hostname 198.*.*.*
    User     tom
    Port     22777
    IdentityFile ~/.ssh/tom
Enter fullscreen mode Exit fullscreen mode

Test Connection

To test the connection, open your local terminal and run:

ssh tomserver

That’s all.
Good luck!

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
 
segaz2002 profile image
Kolawole O. Gabriel

Nice, breakdown. Helped 👍

Collapse
 
shtabnoy profile image
Denis Shtabnoy

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

Collapse
 
rvogel profile image
Rafael Vogel de Oliveira

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

Collapse
 
ralpharama profile image
Ralph Capper

Thank you so much for this, helped me out.

Collapse
 
jartaud profile image
Josué Artaud

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

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
 
cazwazacz profile image
Allan Wazacz

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