Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 35 ยท Load Balancers and Security

SSH Hardening and Access Control on DigitalOcean

Secure a new Droplet: SSH keys, a non-root sudo user, sshd settings, ufw, fail2ban, automatic updates, API token scopes and two-factor authentication.

The first thing that happens to a new public server is a stream of automated SSH login attempts. If the server accepts passwords or allows root login, one weak password is enough. Hardening takes about fifteen minutes and removes the easy attacks: use keys, not passwords; never log in as root; allow only the ports you need; ban repeat offenders; apply security updates automatically.

Commands below are shown as read-only text because the Droplet itself is not available here. The Python blocks are stand-ins that audit a configuration and simulate a ban policy.

1. Use SSH keys

Create a key pair on your own machine, and add the public key to your DigitalOcean account (Settings, Security) or to a Droplet at creation time.

Terminal
ssh-keygen -t ed25519 -C "laptop-2026"
cat ~/.ssh/id_ed25519.pub
doctl compute ssh-key import laptop --public-key-file ~/.ssh/id_ed25519.pub

Protect the private key with a passphrase, never copy it to a server, and never commit it to Git. Create the Droplet with the key selected so password login is not needed at all.

2. Create a sudo user and stop using root

Terminal
adduser deploy
usermod -aG sudo deploy
mkdir -p /home/deploy/.ssh
cp /root/.ssh/authorized_keys /home/deploy/.ssh/
chown -R deploy:deploy /home/deploy/.ssh
chmod 700 /home/deploy/.ssh && chmod 600 /home/deploy/.ssh/authorized_keys

Open a second terminal and confirm ssh deploy@your-ip works and sudo -v works before changing anything else.

3. Lock down sshd

Edit /etc/ssh/sshd_config (or a file in /etc/ssh/sshd_config.d/):

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

Test, then reload while keeping your existing session open: sudo sshd -t && sudo systemctl reload ssh (the service is called ssh on Ubuntu and sshd on some other distributions). Changing the port from 22 reduces log noise but is not real security; keys are.

Python Stand-in: audit an sshd_config

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.

4. Firewall: ufw plus a Cloud Firewall

Terminal
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose

Always allow SSH before ufw enable. The Cloud Firewall adds a network-level layer; better still, restrict SSH to your office or VPN address there.

5. fail2ban: ban repeat offenders

fail2ban reads logs and temporarily bans IPs that fail too often.

Terminal
sudo apt update && sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

Settings live in /etc/fail2ban/jail.local: maxretry, findtime (the window) and bantime. This stand-in applies the same policy to a log of failures.

Python Stand-in: fail2ban policy

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 second address fails twice early, but the third failure comes after the window has passed, so no ban; the three quick failures later do trigger one.

6. Updates, accounts and tokens

  • Unattended upgrades: sudo apt install unattended-upgrades and sudo dpkg-reconfigure --priority=low unattended-upgrades install security patches automatically.
  • Two-factor authentication on your DigitalOcean account protects the control panel, which can delete everything.
  • Teams and roles: create a Team and give members only the roles they need instead of sharing a login.
  • API tokens: create tokens with the narrowest scope (read-only where possible, or custom scopes), set an expiry, store them in a secret manager or CI secret, and revoke any token that leaks. Never put a token in Git or in a user_data script that is logged.
  • Recovery: if you ever lock yourself out, the Droplet Recovery Console in the control panel gives console access without SSH.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which sshd setting removes the biggest brute-force risk?

    Without password login, guessing passwords cannot succeed; changing the port only hides the noise.
  2. 2. What should you do before enabling ufw?

    Enabling a default-deny firewall without an SSH rule drops your own session's future connections.
  3. 3. Why open a second terminal before reloading sshd?

    An existing session stays alive, so you can fix a bad configuration.
  4. 4. What is the best practice for API tokens?

    Least privilege and secret storage limit the damage of a leak.

Summary

  • Log in with SSH keys, use a non-root sudo user, disable root and password login.
  • Combine ufw with a Cloud Firewall; allow SSH before enabling.
  • fail2ban bans IPs after repeated failures inside a window.
  • Turn on unattended upgrades and 2FA, use Teams, and give API tokens minimal scope.

Next: TLS Certificates and Let's Encrypt