How to generate SSH keys on DigitalOcean
Generate an SSH key pair locally using <code>ssh-keygen</code> (OpenSSH on Linux/macOS/WSL) or PuTTYgen (Windows), then copy your public key and add it to your DigitalOcean account via Settings > Security > SSH Keys or directly to an existing Droplet. Connect to your Droplet using <code>ssh root@your_droplet_ip</code> without entering a password.
Prerequisites
- A DigitalOcean account with an existing or planned Droplet
- Terminal access on your local computer (built-in on Linux/macOS; WSL or Git Bash on Windows)
- OpenSSH installed (default on Linux/macOS) or PuTTY downloaded for Windows
- Basic familiarity with command-line operations
- Your email address for the SSH key comment
Step-by-Step Instructions
Generate SSH Key Pair on Linux, macOS, or Windows Subsystem for Linux
ssh-keygen command to create a new key pair. By default, this generates a 3072-bit RSA key, but you can specify a stronger 4096-bit RSA key or use Ed25519 for better performance. Run: ssh-keygen -t rsa -b 4096 -C "your_email@example.com" The -t rsa flag specifies the key type, -b 4096 sets the key size to 4096 bits for enhanced security, and -C adds a comment (typically your email) to identify the key. When prompted, press Enter to accept the default file location (~/.ssh/id_rsa for the private key and ~/.ssh/id_rsa.pub for the public key). This ensures your SSH client can automatically locate your keys during authentication.Set a Passphrase for Your Private Key
ssh-keygen will prompt you to enter an optional passphrase. This passphrase encrypts your private key file on disk, adding an extra layer of security. If you enter a passphrase, you will need to provide it each time you use the private key to authenticate. If you prefer passwordless authentication, simply press Enter twice to skip the passphrase. For most users, especially those managing multiple Droplets, adding a passphrase is recommended to protect against unauthorized access if your local computer is compromised.Generate SSH Keys on Windows Using PuTTYgen
Display and Copy Your Public Key
cat ~/.ssh/id_rsa.pub (or ~/.ssh/id_ed25519.pub if you used Ed25519). The output will display your public key, which starts with ssh-rsa or ssh-ed25519 and ends with your email comment. Copy the entire output—you will paste this into DigitalOcean. On Windows with PuTTYgen, you already copied the public key in the previous step from the key generation window. Do not share your private key (id_rsa or id_ed25519 without the .pub extension) with anyone; only the public key should be uploaded to DigitalOcean.Add Your Public Key to Your DigitalOcean Team Account
Create a New Droplet with Your SSH Key
Add Your Public Key to an Existing Droplet
~/.ssh directory exists on your Droplet by connecting via the Recovery Console (available in the DigitalOcean Control Panel under your Droplet's settings). Once you have access, create the directory if needed. Then, append your public key to the ~/.ssh/authorized_keys file on the Droplet. You can do this by piping your public key directly into the file using SSH or by manually editing the file through the Recovery Console. After adding your key, verify that the ~/.ssh directory has permissions 700 and authorized_keys has permissions 600 for security.Connect to Your Droplet Using SSH
ssh root@your_droplet_ip Replace your_droplet_ip with your Droplet's actual IPv4 address (e.g., ssh root@192.0.2.1). If you set a passphrase on your private key, you will be prompted to enter it. If you did not set a passphrase, you will be logged in immediately without any password prompt. On Windows with PuTTY, open PuTTY, enter your Droplet's IP address in the Host Name field, navigate to Connection > SSH > Auth in the left sidebar, and select your .ppk private key file before clicking Open.Verify SSH Key Authentication and Disable Password Login
/etc/ssh/sshd_config using a text editor like nano: sudo nano /etc/ssh/sshd_config Find the line PasswordAuthentication yes and change it to PasswordAuthentication no. Save the file (Ctrl+O, Enter, Ctrl+X in nano) and restart the SSH service: sudo systemctl restart ssh This ensures that only users with valid SSH keys can access your Droplet, significantly improving security.Common Issues & Troubleshooting
Permission denied (publickey) when connecting to Droplet
Verify that your public key is correctly added to the <code>~/.ssh/authorized_keys</code> file on the Droplet. Check file permissions: <code>~/.ssh</code> should have 700 permissions and <code>authorized_keys</code> should have 600. Use the Recovery Console to access your Droplet and manually verify the key content. Ensure you are using the correct username (usually root for new Droplets) and the correct private key file.
SSH key not found or ssh-keygen command not recognized
On Windows, ensure OpenSSH is installed via Windows Subsystem for Linux (WSL), Git Bash, or PowerShell. Alternatively, use PuTTY and PuTTYgen instead. On Linux/macOS, OpenSSH is typically pre-installed; if not, install it using your package manager (e.g., <code>sudo apt install openssh-client</code> on Ubuntu).
Cannot connect to Droplet after adding SSH key
Verify the Droplet's IPv4 address is correct and the Droplet is running. Check your firewall settings to ensure SSH traffic (port 22) is not blocked. If you recently created the Droplet, wait a few moments for it to fully initialize. Use the Recovery Console in the DigitalOcean Control Panel to access your Droplet directly and troubleshoot SSH configuration issues.
Passphrase prompt appears every time you connect
This is normal if you set a passphrase on your private key. To avoid entering the passphrase repeatedly, use an SSH agent to cache your key. On Linux/macOS, run <code>ssh-add ~/.ssh/id_rsa</code> to add your key to the agent. On Windows with PuTTY, use Pageant (PuTTY Authentication Agent) to manage your keys. Alternatively, regenerate your key without a passphrase if security is less critical.
Multiple SSH keys and unsure which one to use
List all keys in your <code>~/.ssh</code> directory using <code>ls -la ~/.ssh</code>. Check the key fingerprint on your Droplet (<code>ssh-keygen -l -f ~/.ssh/id_rsa.pub</code>) and compare it with your local keys. Create an SSH config file at <code>~/.ssh/config</code> to specify which key to use for each Droplet or host, making key management easier for multiple Droplets.