Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 35 ยท Droplets and Networking

SSH Keys and Secure Access to Droplets

Generate an Ed25519 SSH key, add it to DigitalOcean, understand fingerprints, use ssh-agent and config files, rotate keys, and harden sshd with no root and no passwords.

SSH is how you log in to a Droplet, and SSH keys are the safe way to do it. A password can be guessed by bots that hammer every public server around the clock; a key pair cannot. In this lesson you will generate a key, register it with DigitalOcean, understand the fingerprint the platform shows, and configure a convenient but safe workflow.

How a key pair works

An SSH key pair has two files:

  • The private key (for example ~/.ssh/id_ed25519) stays on your computer. Never share it, never upload it. Protect it with a passphrase.
  • The public key (id_ed25519.pub) is a single line of text that you may share freely. DigitalOcean copies it into ~/.ssh/authorized_keys on a new Droplet.

When you connect, the server challenges your client to prove it holds the private key, using a signature, without the key itself ever leaving your machine.

Generating a key

Ed25519 is the modern default: short, fast and secure.

Terminal
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/do_ed25519
# enter a passphrase when asked

cat ~/.ssh/do_ed25519.pub
# ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA... [email protected]

RSA keys of at least 3072 bits also work if you need compatibility with old software.

Adding the key to DigitalOcean

In the control panel go to Settings, then Security, then Add SSH Key, and paste the contents of the .pub file. Give it a name such as "laptop-2026". With doctl:

Terminal
doctl compute ssh-key import laptop-2026 --public-key-file ~/.ssh/do_ed25519.pub
doctl compute ssh-key list --format ID,Name,FingerPrint

Keys stored in your account can be selected when you create Droplets. Remember: the key is copied onto a Droplet when it is created. Adding a key to your account later does not add it to existing Droplets; for those you edit ~/.ssh/authorized_keys by hand.

Understanding fingerprints

The list shows a fingerprint for each key, such as 3b:16:bf:e4:.... A fingerprint is a hash of the public key blob; it is a short way to identify and compare keys. DigitalOcean displays the MD5 form (colon-separated hex), while modern OpenSSH prints SHA256 (base64). Both are derived from the same decoded key.

The following program is not a DigitalOcean tool; it is a real, working fingerprint calculator that runs entirely in your browser. It builds a public key line from fixed bytes (a made-up key, never use it for real login) and computes both fingerprints.

Python Compute SSH key fingerprints (made-up key)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The same blob gives two different strings, which is why a fingerprint you see in one tool may not look like the one in another tool; compare like with like.

Connecting comfortably

Typing ssh -i ~/.ssh/do_ed25519 [email protected] gets old. Use an SSH config file (~/.ssh/config):

TEXT
Host web-1
  HostName 203.0.113.10
  User deploy
  IdentityFile ~/.ssh/do_ed25519
  IdentitiesOnly yes

Now ssh web-1 is enough. To avoid retyping the passphrase, add the key to the agent once per session:

Terminal
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/do_ed25519

Agent forwarding (ForwardAgent yes) lets a server use your keys to reach other servers, but a compromised server could abuse it. Prefer ProxyJump for hopping through a bastion host:

TEXT
Host db-admin
  HostName 10.114.0.5
  User deploy
  ProxyJump web-1

Hardening sshd

On the server edit /etc/ssh/sshd_config (or a file in /etc/ssh/sshd_config.d/):

TEXT
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AllowUsers deploy
MaxAuthTries 3

Validate before restarting, so a typo cannot lock you out:

Terminal
sudo sshd -t && sudo systemctl restart ssh

Additional layers: a Cloud Firewall that allows port 22 only from your office IP (Cloud Firewalls), fail2ban to block repeated failures, and keeping the operating system patched.

Rotating and removing keys

Keys should have owners and expiry dates in your head, if not in a file:

  1. Generate a new key and add it to the account.
  2. Append the new public key to authorized_keys on each server and test it.
  3. Remove the old key from authorized_keys and from the account.

When someone leaves the team, remove their key from every authorized_keys. Better still: manage keys centrally with cloud-init or Terraform so the list is the same everywhere.

If you lose access

Every Droplet has a Recovery Console (a browser terminal, available in the control panel) that logs in without SSH. If you keep a root password set, it can be used there; otherwise you can boot into recovery mode, mount the disk and fix authorized_keys. Prevention is easier: keep a second, backup key safely stored.

Python Stand-in: audit an authorized_keys file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which key file must never leave your computer?

    The private key proves your identity. Anyone who has it can log in as you, so protect it with a passphrase.
  2. 2. You add a new SSH key to your DigitalOcean account. Existing Droplets...

    Add the key to ~/.ssh/authorized_keys on existing servers yourself, or automate it.
  3. 3. What does the sshd setting PasswordAuthentication no do?

    Bots cannot guess passwords if password login is off.
  4. 4. What should you run before restarting sshd after editing its config?

    sshd -t tests the configuration, so a syntax error does not lock you out.

Summary

  • A key pair replaces passwords: keep the private key secret and share only the public key.
  • Ed25519 is the modern default; add the public key to your account and select it when creating Droplets.
  • Fingerprints are hashes of the public key; MD5 colon form and SHA256 base64 describe the same key.
  • Use ~/.ssh/config, ssh-agent and ProxyJump; harden sshd with no root and no passwords, and validate with sshd -t.
  • Rotate keys, remove departed users, and keep the Recovery Console in mind.

Next: Cloud-init and User Data