Question

Switching from password to SSH key

If I selected the password option when creating my droplet, is it possible to change it to SSH? Thanks


Submit an answer


This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

alexdo
Site Moderator
Site Moderator badge
June 7, 2024

Heya,

Yes, as mentioned this is totally doable. On top of what’s already been mentioned I’ll include this article on how to add ssh-keys to existing droplets which you can use to add your key to the droplet.

https://docs.digitalocean.com/products/droplets/how-to/add-ssh-keys/

Hope that this helps!

KFSys
Site Moderator
Site Moderator badge
June 7, 2024

Heya,

Yes, you can do that and it’s even a more reliable method to keep your Droplet safe, so it’s a good choice to change it up!

What you’ll need to do is deploy your ssh public key to your Droplet and enable PubkeyAuthentication in your sshd config.

So, let’s begin

Once inside the Droplet, open the file /root/.ssh/authorized_keys and paste your public key there. If there isn’t a .ssh directory in your /root folder, you can create the structure like so:

mkdir -p ~/.ssh
echo "your_copied_public_key" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh

Once that is done, open your sshd config:

nano /etc/ssh/sshd_config

Inside you’ll need to find the line PubkeyAuthentication and uncomment it or you can just add it as a new line

PubkeyAuthentication Yes

Also, if you want to stop PasswordAuthentication, find the line and set it to No

PasswordAuthentication no

Once that is done, save the file, exit it and restart your SSHD service

service sshd restart

That’s it. You can now use your SSH key to enter your Droplet.

Bobby Iliev
Site Moderator
Site Moderator badge
June 6, 2024

Hey there!

Absolutely, you can switch from password authentication to SSH key authentication on your DigitalOcean Droplet. Here’s how you do it:

  1. If you haven’t already, you need to generate an SSH key pair. Open your terminal and use the following command:

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    

    Follow the prompts to save the key and add a passphrase if desired.

  2. Your public key will be saved in ~/.ssh/id_rsa.pub by default. Use this command to copy it:

    cat ~/.ssh/id_rsa.pub
    

    Copy the output to your clipboard.

  3. Log in to your Droplet using the password you set up:

    ssh root@your_droplet_ip
    
  4. Once logged in, create a .ssh directory and an authorized_keys file if they don’t exist:

    mkdir -p ~/.ssh
    echo "your_copied_public_key" >> ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    chmod 700 ~/.ssh
    
  5. Open the SSH configuration file:

    nano /etc/ssh/sshd_config
    

    Make sure the following lines are set:

    PubkeyAuthentication yes
    PasswordAuthentication no
    
  6. To apply the changes, restart the SSH service:

    systemctl restart sshd
    
  7. Open a new terminal window and try logging in again with your SSH key:

    ssh root@your_droplet_ip
    

If you have any questions, feel free to ask. Happy coding!

- Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel