I once spun up a new VPS, installed Docker, deployed an app, and forgot the firewall. Two days later the logs were full of SSH brute-force attempts. Lesson learned: default VPS is not secure. Docker also exposes ports to the world by default.

This article is the checklist I use every time I provision a fresh VPS. 15 minutes, done. Works for homelab, side projects, or small production.

Prerequisites

  • Fresh Ubuntu 24.04 LTS VPS (any provider)
  • SSH key ready (no password login)
  • Domain optional (for SSL later)

Recommended: DigitalOcean — $200 credit for new accounts

Step 1: User & SSH Hardening

Login as root, create a non-root user:

adduser deploy usermod -aG sudo deploy 

Copy SSH key to the new user:

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 

Test login as deploy in a new terminal. If it works, harden SSH:

nano /etc/ssh/sshd_config 

Change/uncomment these lines:

PermitRootLogin no PasswordAuthentication no PubkeyAuthentication yes MaxAuthTries 3 ClientAliveInterval 300 ClientAliveCountMax 2 

Restart SSH:

systemctl reload sshd 

Step 2: UFW Firewall

Default deny incoming, allow outgoing. Only open what you need:

ufw default deny incoming ufw default allow outgoing # SSH (port 22) - change if using custom port ufw allow 22/tcp # HTTP/HTTPS for web ufw allow 80/tcp ufw allow 443/tcp # Docker internal (optional, if containers need to talk) # ufw allow from 172.17.0.0/16 # Enable ufw enable 

Verify:

ufw status verbose 

Step 3: Fail2ban

Block IPs that repeatedly fail login. Install:

apt update && apt install -y fail2ban 

Create custom config:

nano /etc/fail2ban/jail.local 

Content:

[DEFAULT] bantime = 3600 findtime = 600 maxretry = 3 backend = systemd [sshd] enabled = true port = ssh filter = sshd logpath = /var/log/auth.log maxretry = 3 [docker-auth] enabled = true port = 2375,2376 filter = docker-auth logpath = /var/log/auth.log maxretry = 3 

Restart:

systemctl enable --now fail2ban fail2ban-client status sshd 

Step 4: Docker + Docker Compose

Install from official repo (not apt default which can be outdated):

# Remove old versions for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do apt remove -y $pkg done # Install dependencies apt update && apt install -y ca-certificates curl gnupg lsb-release # Add Docker GPG key install -m 0755 -d /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg chmod a+r /etc/apt/keyrings/docker.gpg # Add repository echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \ https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ tee /etc/apt/sources.list.d/docker.list > /dev/null # Install apt update && apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin 

Add user to docker group:

usermod -aG docker deploy 

Test (logout/login first, or newgrp docker):

docker run --rm hello-world 

Step 5: Automatic Security Updates

Production servers need patches without manual intervention:

apt install -y unattended-upgrades needrestart 

Configure:

nano /etc/apt/apt.conf.d/50unattended-upgrades 

Ensure these are uncommented:

Unattended-Upgrade::Allowed-Origins { "${distro_id}:${distro_codename}-security"; "${distro_id}:${distro_codename}-updates"; }; Unattended-Upgrade::AutoFixInterruptedDpkg "true"; Unattended-Upgrade::MinimalSteps "true"; Unattended-Upgrade::Remove-Unused-Dependencies "true"; Unattended-Upgrade::Automatic-Reboot "false"; 

Enable timer:

systemctl enable --now unattended-upgrades 

Check logs:

tail -f /var/log/unattended-upgrades/unattended-upgrades.log 

Step 6: Verification & Test

Post-setup checklist:

# 1. SSH key-only ssh -o PasswordAuthentication=no deploy@<IP> # 2. Firewall active ufw status # 3. Fail2ban running fail2ban-client status sshd # 4. Docker working docker version docker compose version # 5. Auto-updates enabled systemctl status unattended-upgrades # 6. Open ports from outside (should only be 22, 80, 443) nmap -sS <IP> 

Bonus: One-Liner Setup Script

Save this as a GitHub Gist, then run:

curl -fsSL https://gist.githubusercontent.com/<your-username>/<gist-id>/raw/setup-vps.sh | bash 

Script content (setup-vps.sh):

#!/bin/bash set -euo pipefail USERNAME="deploy" SSH_PORT="22" # 1. Create user adduser --disabled-password --gecos "" "$USERNAME" usermod -aG sudo "$USERNAME" # 2. SSH hardening mkdir -p "/home/$USERNAME/.ssh" cp /root/.ssh/authorized_keys "/home/$USERNAME/.ssh/" chown -R "$USERNAME:$USERNAME" "/home/$USERNAME/.ssh" chmod 700 "/home/$USERNAME/.ssh" chmod 600 "/home/$USERNAME/.ssh/authorized_keys" cat > /etc/ssh/sshd_config.d/99-hardening.conf <<EOF PermitRootLogin no PasswordAuthentication no PubkeyAuthentication yes MaxAuthTries 3 ClientAliveInterval 300 ClientAliveCountMax 2 Port $SSH_PORT EOF systemctl reload sshd # 3. UFW ufw default deny incoming ufw default allow outgoing ufw allow "$SSH_PORT"/tcp ufw allow 80/tcp ufw allow 443/tcp ufw --force enable # 4. Fail2ban apt update && apt install -y fail2ban cat > /etc/fail2ban/jail.local <<EOF [DEFAULT] bantime = 3600 findtime = 600 maxretry = 3 backend = systemd [sshd] enabled = true port = ssh filter = sshd logpath = /var/log/auth.log maxretry = 3 EOF systemctl enable --now fail2ban # 5. Docker for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do apt remove -y $pkg 2>/dev/null || true done apt update && apt install -y ca-certificates curl gnupg lsb-release install -m 0755 -d /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg chmod a+r /etc/apt/keyrings/docker.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null apt update && apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin usermod -aG docker "$USERNAME" # 6. Auto-update apt install -y unattended-upgrades needrestart cat > /etc/apt/apt.conf.d/50unattended-upgrades <<EOF Unattended-Upgrade::Allowed-Origins { "\${distro_id}:\${distro_codename}-security"; "\${distro_id}:\${distro_codename}-updates"; }; Unattended-Upgrade::AutoFixInterruptedDpkg "true"; Unattended-Upgrade::MinimalSteps "true"; Unattended-Upgrade::Remove-Unused-Dependencies "true"; Unattended-Upgrade::Automatic-Reboot "false"; EOF systemctl enable --now unattended-upgrades echo "Setup complete. Login as $USERNAME and test docker." 

What's Next

This is the foundation. Next articles in this series:

  • Docker Compose production template (PostgreSQL + Redis + Nginx + Backup)
  • Monitoring stack: Prometheus + Grafana + Loki
  • CI/CD with GitHub Actions + Docker
  • Zero-downtime deployments

Part of the "Production Infrastructure for Developers" series.