How exactly do I add a key to my DigitalOcean Droplet??

April 25, 2017 701 views
LAMP Stack DigitalOcean MySQL PHP Ubuntu

I changed my public and private keys on Terminal (I'm using Mac OS) many times. So when I "ssh" into the IP address given to me by my Droplet (the IP address is 107.170.60.7), I get the error of Permission denied (publickey). so I know it has to do with me changing the public/private key. So now I need to add that changed public key on my Terminal to my Droplet on DigitalOcean. There were two things suggested by the DigitalOcean community which I've tried:

1) The tutorial on here - but this is for Windows users, not Mac. It's suggests using a "Search and Replace" and checking certain functionalities. But Macs don't have this so I can't even use it:
http://geektnt.com/how-to-add-ssh-key-to-existing-digitalocean-droplet.html

2) A user kept suggesting to someone else that the problem is resolved by visiting this page:
https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2
I read into it and it has everything to do with setting up a SSH key, which I already know, and NOTHING to do with how to ADD an ssh key to an existing Droplet.

I really just need to know how to add my changed ssh key to my Droplet. I would really appreciate the help if anybody knows.

3 Answers

@confused5000

To add an SSH Key to your Droplet, you'll need to be able to access it. Do you still have the old key that you were using? If so, SSH in to your Droplet using the old key and add your new public keys to:

~/.ssh/authorized_keys

The format is one key per line. You can remove the old key from this file.

If you don't have the old key that you were using, you may very well be locked out as console access to the Droplet is limited to password-based authentication.

  • @jtittle I know this is a stupid question, but how do I SSH into my Droplet? This is the correct way to do that right? :

    MACs-MBP:~ MAC$ ssh root@107.170.60.7
    

    I typed that in but I get this:

    Permission denied (publickey).
    

    And no, I don't have that old key since I "overwrote" it with a new public/private key.
    Also, I'm looking inside my .ssh directory and I see no directories called "authorized_keys". Am I supposed to have that directory?

@confused5000

To SSH in with a key, you have to pass the -i flag and define the full path. So if your key name was:

confused

On MacOS, you'd use:

ssh root@107.170.60.7 -i ~/.ssh/confused

That being said, if you created a Droplet with one key and then overwrote it, you've effectively locked yourself out. The only way to get around this would be if you set a password for root, then you could try to login without it, but seeing as you're getting:

Permission denied (publickey).

... when trying to login with ssh root@107.170.60.7, that tells me that you don't / didn't.

  • @jtittle Then there's no way to ssh into Droplet if the old key's been overwritten. Well I looked up how to login as "root" on DigitalOcean and it says to click on the Launch Console button on my Droplet account. I did that and it launches up a "DigitalOcean Droplet Console". It asks me for login: and I type root. Then it asks for password but there's bugs on the page. The password DigitalOcean sent me was long so I tried copying and pasting it onto the console, but this shows up (instead of the password): ^[v. Another thing - when I first type in the password and hit enter, I'm brought to another blank line. I'm guessing this line is asking me to re-enter the password. So when I try typing in the password, it barely gives me 1 second to type, then it goes blank and says Login incorrect. I'm not sure what I'm doing wrong.

@confused5000

If you deployed the Droplet with an SSH Key, a root password is never set, thus you won't be able to login using console, or login as root using a password.

That's why I mentioned that you're effectively locked out since you overwrote the older key.

Have another answer? Share your knowledge.