Adding a SSH key with new Recovery Console

Posted July 28, 2021 993 views
NetworkingDigitalOcean Droplets

I have a new laptop, so I wanted to update my SSH key that I use for my droplet. I generated a new key with ssh-keygen on the new PC, and initially followed a DigitalOcean tutorial to update my SSH Key in their Settings > Security menu on the Dashboard.

I found out later that this only affects new droplets, and that I needed to update ~/.ssh/authorized_keys on my droplet. However, I could not connect with any of my old accounts as I would get Permission denied (publickey). and no longer have access to that key.

The next advice I found was to use the web console on the Dashboard to update the keys file on my droplet. When I launched the Recovery Console (the only console I could find) I encountered strange red glitches you can see here and eventually a singular blinking cursor that did not respond to interaction or input.

But wait, this is where things get interesting! If I went to the Recovery tab on the droplet dashboard, selected Recovery ISO, rebooted, and was able to use the Recovery Console to do things like mount the disk, reset root password, update files, and more. I happily updated ~/.ssh/authorized_keys by using curl to grab my new ssh key I stored on a bin website then copying it into the file. I had to do that because pasting into the web console did not work correctly.

Finally, I restarted my droplet, making sure to boot to disk directly instead of the Recovery ISO. The exact same problem still happens.

Strangely, I can connect using the new ssh key while I am still in recovery mode, although I get a WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! that if I push through I can log on like normal. I am guessing that the recovery ISO does not save changes to the disk for this reason.

So, I don’t know what to do. If the web console worked outside of Recovery ISO, I could fix it, but I cannot. Any ideas to fix this would be amazing!

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.

Submit an Answer
2 answers

I finally got this working by:

  1. Adding my new SSH Key in Settings > Security > SSH Keys on the Dashboard.
  2. Stopped my original droplet and created a Snapshot.
  3. Created a new droplet from the Snapshot.
  4. Updated A records to point towards new droplet.
  5. Logged in with root without needing to go into recovery, created the /home/username/.ssh/authorized_keys file for my day to day user.
  6. Also set permissions as so: chown -R username:username /home/username/.ssh chmod 700 /home/username/.ssh chmod 600 /home/username/.ssh/authorized_keys

And voila! Logging in with ssh username@droplet worked! The only downside was the DNS downtime. I would hate to do this on a production server, so I’ll create backups of my SSH key this time.

  • Hi @coachsyncinbox,

    That is indeed one way of resolving your issue. I’m glad to see it worked!

    Anyway, I’m baffled as to why the previous stuff mentioned didn’t work but hey, as long as it’s working now!

Hi @coachsyncinbox,

What I’ll suggest is to turn on PasswordAuthentication in your /etc/ssh/sshd_config while in Recovery Console. Don’t forger to restart the sshd service after you make the change.

service sshd restart

Once you do, you’ll be able to use your Regular Terminal to enter your Droplet using your root password.

Once you enter, you can easily copy your SSH key to the ~/.ssh/authorized_keys file.

Once you do remember to set PasswordAuthentication to Off and again restart the sshd service.

  • Hi @KFSys,

    Thank you for the suggestion. PasswordAuthentication is already on. Root can connect in recovery mode using a password authentication, but as soon as I switch back to booting from the disk instead of recovery mode, the problem returns and even root gets the Permission denied (publickey). error.

  • To follow up, I tried this again both with the bash access in the Recovery Console, as well as logging in as root and doing this from my regular terminal. Both cases I can access the systen, make changes, and restart sshd, but the authorized_keys file is nonetheless reverted back to the old public key when I turn the server off and switch it back to booting from disk in the Dashboard Recovery menu, then power back on.