Question

How to switch from password to ssh key authentication?

I created a droplet with root password. Is it possible to change to ssh key authentication without destroying and recreating the droplet? I have already done considerable work on hte droplet and hate to start over again. 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.

KFSys
Site Moderator
Site Moderator badge
June 8, 2020
Accepted Answer

Hi @cpeters5,

There are 3 things you would need to do in order to switch to a using an ssh key rather than a password.

The first step would be to generate an ssh key. On your machine, in the terminal run the following command

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

When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.

Now you should have an SSH key located in

~/.ssh/id_rsa.pub

Copy the contents of the file.

The second step is to add the key to your Droplet. To do that, SSH to it

ssh root@YOurDropletIp

Once you are in open the following file and paste what you’ve copied for your ssh key in it

nano ~/.ssh/authorized_keys

Inside the file on the last line, paste it. Disclaimer, it should all be on one line!

Now you’ve added your SSH key.

The third step is to enable key authentication on the droplet. While you are on the droplet open the following file

nano /etc/ssh/sshd_config

Inside the file, search for a directive called PubkeyAuthentication. This may be commented out. Uncomment the line and set the value to “yes”. This will enable your ability to log in through SSH using ssh keys:

PubkeyAuthentication yes

Save and close the file when you are finished. To actually implement the changes we just made, you must restart the service.

service ssh restart

That’s it you now can ssh with a key rather than a password.

Regards, KDSys

thank you for this. while it does allow for access with ssh it does still keep the authentication with user/pass enabled as well. how to disable that?

Thanks for informations

Try DigitalOcean for free

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

Sign up

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