How do i add an ssh key to an existing droplet?

November 22, 2014 60.1k views


I have been trying to add a public key on a pre-existing droplet and I am not able to connect although I have tried to paste it on the original SSH KEY, and on a new key.
I have followed this tutorial, but it is not really showing it well on how i can get into a preconfigured drop-let.

Thank you

1 comment
  • You probably want to ssh in as root and not a specific user as you might be doing it for the first time and have not created a user yet.

8 Answers

You can add an ssh key to an existing droplet by appending the public key (ssh-rsa ...) to /home/user/.ssh/authorized_keys or /root/.ssh/authorized_keys.

If you’re on Linux, you can run the following command to do that automatically:

ssh-copy-id user@

On OSX, you can run this command instead:

cat ~/.ssh/ | ssh user@ "mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys"
  • Kamal. I have a current droplet in which I need to add my ssh key to.

    I have tried your code for OSX a ton of different ways and it keeps timing out.

    I have tried which does not seem to work either.

    Also tried your code in variation with and that didn’t seem to work either. Really stuck here.

    I need to log into my server without a password.

  • Hi guys, Having the same issue, I had to format my mac hence add a new SSH key.
    Step 1: I have created a new SSH key following the tutorial here:

    Step 2: I have tried the code

    cat ~/.ssh/ | ssh user@ "mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys"

    But all I have as reply from the terminal is :

    root@'s password: 

    And I have never set up any paw for the Server and I have also no idea of which paw I should us. Could you please help me out?

  • I have personally sorted this out adding as step the reset of the root password. I did not have one since the first time that I created the droplet I created it immediately using the SSH.

    Now all is up and running!

  • @internetmarketi Hi had the same issue with root@ip.address’s password after I’m done all of above steps. How could you over come the problem then?

  • @trieuquanganh I have simply asked for a password reset of the droplet. Once I had that password I then was able to enter my server again.
    After that you can add a new SSH key. I hope that helps.

  • Thanks mate. Just one more thing. How can I “ask for a password reset of the droplet”? By saying “reset of the droplet”, did you mean you dropped the existing one and create a new one?

    Sorry for the dummy question but I’m new to this.

  • @trieuquanganh: You can reset your droplet’s root password from the control panel: Access -> Reset Root Password:

  • @kamaln7 By doing this, does that mean I have to re-create a completely new droplet? Or it just shutdown the existing one and restart again?

  • It shuts down the droplet and then boots it back up after resetting the password. The whole process should take about one minute.

  • Am I losing all my setup? I have installed Rails and uploaded my website on that. Is that gonna affect my current setting?

  • No, your data will not be affected - you can safely reset an existing droplet’s root password without losing any data. The only thing that will be different when the droplet boots up is your root password :)

  • @kamaln7 All good mate. Thanks for your help :-)

  • This avoids the need to enter a password when accessing through an exterior terminal such as PuTTY, but doesn’t do anything for accessing through the java terminal in I am very new… but the only way I can see to do this is to add the SSH when creating the droplet. So here’s what I’m going to try:

    1. Power the droplet down
    2. Take a snapshot
    3. Destroy the droplet
    4. Re-create the droplet (taking care to have the same IP address and to use the same checkbox settings for Private Networking, IPv6, Backups and User Data and, at this point, add the key. (Is this the private key or the public key? Page doesn’t say. I’ve asked support.)
    5. Go to the access tab and see if the droplet can be accessed without a password
    6. Make any necessary changes to sshd config
    7. When all is working as desired, take a new snapshot

    I reserve the right to come back here, if I remember, to edit this post. ;)

  • What is “user” in “user@”. Can you explain,

  • Thanks @kamaln7! I’ve been going in circles trying to troubleshoot this problem of adding a ssh key to an existing droplet. I was finally able to fix it by adding the public key to /home/{user}/.ssh/authorized_keys instead of my failed attempts of adding it to /root/.ssh/authorized_keys

  • Awesome!!! Works :)

  • Thank you. I accidentally rewritten my private key while the connection was open on PuTTy and I just generated the new private-public pair and replaced the existing public key with the new one in /root/.ssh/authorized_keys.

i followed this steps and add ssh key to my existing droplet as you say, and now can login with user@ipaddress without password. but when i trying to login as root@ipadress or on other computer as user@ipadress and root@ipaddress, it still asking password except automatically kicking out without being prompted for a password. whats the problem?

by Etel Sverdlov
This guide is for Mac OS X and Linux users. Learn how to use SSH Keys with DigitalOcean Droplets.

btw, i dont sure if my message sent..

Unable to edit my previous post. According to support it is the public key that is entered into the “Add SSH Key” field when creating a new droplet. The respond further, writing, “The [control panel] console does not allow you to use SSH keys. These can only be used for [connecting with SSH via PuTTY or other appropriate client]. The console on your control panel is a VNC style connection intended to be used when you cannot connect via SSH. It can only be used with passwords.” As you see, I edited their text somewhat for clarity.

*Tip: *

If you have forgotten root password then you need to reset the root password.

Just in case you cannot login as root from your computer console, you need to enable root login.

nano /etc/ssh/sshd_config then PermitRootLogin yes and restart ssh service ssh restart.

then proceed as above.

Don’t forget to change PermitRootLogin without-password and restart ssh service service ssh restart or just kill the process ps auxw | grep ssh.

*TIP2: *

If you are using a different ssh key other than then modify your ~/.ssh/config file.

The following have worked for me:

  1. Create GitHub repo and put your key in it
  2. Clone the repo using console
  3. Run cat /path/to/cloned/github/project/key | cat >> ~/.ssh/authorized_keys

NB key is a file containing the value

I got it to work by adding the ssh key to both /root/.ssh/authorized_keys and /root/.ssh known_hosts then restarting the droplet

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!