Skip to main content
Green binary audience decorative image for WebBuild

Creating and Using SSH Keys with DigitalOcean

Learn to securely log into your DigitalOcean server using SSH keys, avoiding passwords and enhancing your server's security.

Module 6 - Lesson 2

Creating and Using SSH Keys with DigitalOcean

Play

About SSH Keys

SSH keys provide a more secure way of logging into a virtual private server with SSH than using a password alone. While a password can eventually be cracked with a brute force attack, SSH keys are nearly impossible to decipher by brute force alone.

Generating a key pair provides two long strings of characters:

  • Public Key: Placed on the server.
  • Private Key: Stored securely on your client machine.

When the keys match, the system unlocks without a password. You can further secure the private key with a passphrase.

On Mac or Linux

Step 1: Create the RSA Key Pair

Run the following command to create the key pair:

ssh-keygen -t rsa

Step 2: Store the Keys and Passphrase

Follow the prompts:

Enter file in which to save the key (/home/USER/.ssh/id_rsa):

Press Enter to save the key in the default location.

Enter passphrase (empty for no passphrase):

You can leave this blank or enter a passphrase for added security.

  • Public Key: /home/user/.ssh/id_rsa.pub
  • Private Key: /home/user/.ssh/id_rsa

Step 3: Copy the Public Key to Your Server

Use the ssh-copy-id command to copy the public key to your server:

ssh-copy-id root@example.com

After entering your server password, your public key will be added to the authorized_keys file.

Screenshot of SSH on Mac

You can now log into your server without being prompted for a password:

ssh root@example.com

If you set a passphrase, you’ll need to enter it when logging in.

On Windows with PuTTY

For Windows users, PuTTY is a great tool for generating and using SSH keys.

  1. Download PuTTYgen from PuTTY Download.
  2. Follow this DigitalOcean guide to:
    • Generate your key pair.
    • Save the public and private keys.
    • Upload the public key to your server.

Locking Down Root Password Logins

To enhance security, disable root password logins:

  1. Edit the SSH configuration file:
nano /etc/ssh/sshd_config
  1. Locate and update the following line:
PermitRootLogin without-password
  1. Restart the SSH service:
sudo service sshd restart