Question

Change SSH key of root account

I restored my Droplet to a fresh one with Ubuntu (I’m using 19.10 but it’s not on the Topic list) however I can still access with my old SSH key. Is it possible to replace the root key for a new one?


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
January 1, 2020
Accepted Answer

Hello, @JuanDssCarp

You can change the key by simply accessing your droplet and edit the authorized keys file in /root/.ssh/authorized_keys

You can remove the old key and paste your new one and then save the file.

Let me know how it goes.

Regards, Alex

I’m relatively new, so there’s probably better/more elegant solutions somewhere, but here’s my best guess.

Do you still have the .pub file from when you created the SSH key you no longer want? If so, and if you can view the contents, have that available.

Then, SSH into the server and type:

cd ~/.ssh

In that folder, you should have a file called authorized_keys

Use your favorite editor to remove the SSH key that matches the .pub file you have open.

I just tried it on a droplet that I spun up, and it seemed to work. When I tried SSHing in with the key that I deleted, I got the “Permission denied (publickey).” error.

Please reply if this works for you. If so it’ll be my first answer on the forum here!

EDIT: I totally misread your question. You wanted to replace the SSH key, not just remove the old key.

So the same basic principle applies: You need to edit the authorized_keys file with the SSH keys that you want to log in. So the only additional step is to, while you are logged into the server and editing the authorized_keys file, paste in the contents of the .pub file for your new SSH key.

Again, please reply whether this helps or not.

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