Trying to access your DigitalOcean droplet and can't get beyond Permission denied (publickey)
?
This could be due to multiple reasons, having the wrong public_key entry in your droplet's authorized_keys
, or creating a new public key on your system which the droplet doesn't recognize yet.
After trying the usual things, this is what works:
- If you have created a new SSH public key, run:
cat ~/.ssh/id_rsa.pub
on your local system, and copy the complete key it displays. [Skip to point 2] In case it doesn't display anything, you probably don't have a public SSH key or you could have one under a different name. Go tocd .ssh
and see if you have any of the*.pub
files there, if you want to use any of those, just runcat filename.pub
and copy the key. If you don't have any existing public SSH keys, you can generate one using:ssh-keygen -o
, then follow the instructions as shown.
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Created directory '/home/user/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
d0:82:24:8e:d7:f1:bb:9b:33:53:96:93:49:da:9b:e3 user@mylaptop.local
Once the public key is generated, view it using cat ~/.ssh/id_rsa.pub
or replace id_rsa.pub
with whatever name you gave the file. Now copy that key.
Now head to your digitalocean web console. Go to Settings > Security > SSH Keys > Add SSH Key. Add the SSH key you copied and give it any name.
Note that if the key already exists, it won't be copied, so you can skip this step.
Now try connecting via SSH, if it works, you lucky (de)bugger you. If it doesn't work, that's what you're here for.Head over to your droplet's dashboard and from the left-side options, select Access > Console Access > Launch Console.
Enter your username and password for the droplet, and it'll connect you to the droplet via SSH (using a web browser).You'll need to enable password login to your droplet. To do so,
sudo nano /etc/ssh/sshd_config
, scroll down and set these toyes
PermitRootLogin yes
PasswordAuthentication yes
Save the file and run service sshd restart
to restart the sshd service and apply the changes.
Now some articles might suggest heading over to the
~/.ssh/authorized_keys
on the droplet from the browser SSH console, but it has a bug which only copies the SSH key upto a certain characters, and because of that, it won't work. Therefore, we'll not do this step right now.As you've enabled password authentication to the server, try SSH to it from the terminal, it'll prompt for the password, after which you should be able to connect. Well, problem solved, or at least most of it.
Now we're going to edit the
nano ~/.ssh/authorized_keys
file on the server, paste your SSH key here in a newline(which you had generated in Step 1). You can now restart the ssh service again usingservice sshd restart
.Try SSH using your terminal and you should be able to connect without entering the password. As it's a good security practice to disable Password based login, head over to
sudo nano /etc/ssh/sshd_config
again on your server, and set
PasswordAuthentication no
Save the file and restart sshd using service sshd restart
.
That's it, you should now be able to login to the server without any issues.
Latest comments (16)
Like a lot of people I created an account just to thank you! THANK YOU!
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:
P.S If you're using a remote machine to access the droplet, make sure to GENERATE THE SSH KEY ON THE REMOTE MACHINE.
Thank you so much for this, helped me out.
Thanks a lot. The browser console was turning me nuts.
Thanks for sharing this.
I usually change this:
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:
Now you can ssh using your name.
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!
Thank you Sailesh. This instructions helped me to solve the issue!
Hey Sailesh, I've also just created an account to say thanks!!! You rock!
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 isid_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 sureIdentityFile
is set to the correct private keyI lost my access to DigitalOcean,
this tip of yours was my salvation, change the file name to id_rsa
Thanks
Thanks for the quick solution! If your on a mac you wanna make this edit here /private/etc/ssh/ssh_config
I solved using this solutions
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!
I am getting a permission denied when I try to save the authorized key file in the second to last step. Thoughts?