Skip to content

VPS Setup

Sane defaults for locking down a fresh Debian/Ubuntu VPS: SSH keys, UFW, Tailscale, and Caddy.

A fresh VPS is wide open. These steps get it to a reasonable baseline: no password logins, a tight firewall, and Tailscale for private access.

Tested on Debian 13 (trixie) and Ubuntu 26.04. Commands are shown with sudo; if your provider logs you in as root, run them directly.

Create a non-root user

SSH in using your provider’s default username:

ssh <default-user>@your-server-ip

Create a deploy user and grant it sudo access:

sudo useradd -m -s /bin/bash deploy
sudo usermod -aG sudo deploy
sudo passwd deploy

Copy your SSH public key to the new user:

sudo mkdir -p /home/deploy/.ssh
sudo cp ~/.ssh/authorized_keys /home/deploy/.ssh/
sudo chown -R deploy:deploy /home/deploy/.ssh
sudo chmod 700 /home/deploy/.ssh
sudo chmod 600 /home/deploy/.ssh/authorized_keys

Verify you can log in as the new user before continuing:

ssh deploy@your-server-ip

From here on, use deploy rather than root.

System updates

Apply pending security patches:

sudo apt update && sudo apt upgrade -y
sudo apt install -y curl ufw

Enable automatic security updates:

sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades

Swap

Many small VPS instances ship with no swap. Without it, the OOM killer will start terminating processes silently when memory is exhausted. A 2 GB swapfile is a cheap safety net:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Verify:

free -h

SSH

Harden sshd_config

Edit /etc/ssh/sshd_config and set:

PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

Reload:

sudo systemctl reload ssh

Tailscale

Tailscale gives every machine a stable private IP and lets you scope firewall rules to the tailscale0 interface instead of managing per-IP allowlists.

curl -fsSL https://tailscale.com/install.sh | sh
sudo tailscale up

Follow the authentication URL printed to stdout. Once connected, your machine appears at a stable 100.x.x.x address in your tailnet.

UFW

Reset any existing rules and apply a strict default:

sudo ufw reset
sudo ufw default deny incoming
sudo ufw default allow outgoing
# allow all traffic on the Tailscale interface
sudo ufw allow in on tailscale0
sudo ufw allow http
sudo ufw allow https
sudo ufw enable

Verify:

sudo ufw status verbose

Expected output:

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)

To                         Action      From
--                         ------      ----
Anywhere on tailscale0     ALLOW IN    Anywhere
80/tcp                     ALLOW IN    Anywhere
443/tcp                    ALLOW IN    Anywhere

fail2ban

fail2ban watches logs and temporarily bans IPs that repeatedly fail authentication. With key-only SSH this matters less for port 22, but it still catches bots hammering public HTTP endpoints:

sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban

The defaults are reasonable out of the box. Verify it’s watching SSH:

sudo fail2ban-client status sshd

Caddy

Caddy is a reverse proxy that provisions and renews TLS certificates automatically via Let’s Encrypt: no certbot, no cron jobs.

Install

Add the official Caddy apt repository and install:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' \
  | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' \
  | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update && sudo apt install -y caddy

Caddyfile

The config lives at /etc/caddy/Caddyfile. Replace the default contents:

app.example.com {
    reverse_proxy localhost:8080
}

Multiple apps on the same server:

app.example.com {
    reverse_proxy localhost:8080
}

api.example.com {
    reverse_proxy localhost:3000
}

Path-based routing to different backends:

example.com {
    reverse_proxy /api/* localhost:3000
    reverse_proxy * localhost:8080
}

Apply the config:

caddy fmt --overwrite /etc/caddy/Caddyfile
sudo systemctl reload caddy

Logs

sudo journalctl -u caddy -f

Certificate errors, upstream connection failures, and access logs all appear here. Most TLS issues are DNS-related: Caddy provisions certificates on first request, so the domain must resolve to the server’s public IP before that request arrives.

Running your application

Systemd unit file

Create /etc/systemd/system/myapp.service:

[Unit]
Description=My App
After=network.target

[Service]
Type=simple
User=deploy
WorkingDirectory=/home/deploy/myapp
ExecStart=/home/deploy/myapp/server
EnvironmentFile=-/home/deploy/myapp/.env
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

The - prefix on EnvironmentFile means systemd will not fail if the file does not exist. Restart=on-failure restarts the process only on non-zero exit; RestartSec=5 adds a delay to prevent tight crash loops.

Enable and start in one step:

sudo systemctl daemon-reload
sudo systemctl enable --now myapp

enable registers the unit to start at boot. --now starts it immediately.

Logs

sudo journalctl -u myapp -f

stdout and stderr both land here. The journal persists across restarts, so you can inspect logs from a previous crash:

sudo journalctl -u myapp --since "1 hour ago"

Deploying updates

Copy the new binary to a temporary path and mv it into place, then restart. Writing directly over a running executable fails with ETXTBSY (text file busy); mv sidesteps this by replacing the directory entry, while the running process keeps executing the old inode until the restart:

scp ./server deploy@host:/home/deploy/myapp/server.new
ssh deploy@host 'mv /home/deploy/myapp/server.new /home/deploy/myapp/server && sudo systemctl restart myapp'

mv on the same filesystem is atomic: there is no window where a partial write can be executed. The service will be briefly unavailable during the restart.

Common commands

Command Effect
systemctl start myapp Start the service
systemctl stop myapp Stop the service
systemctl restart myapp Stop then start
systemctl status myapp Current state and last few log lines
systemctl reload myapp Send SIGHUP (only if the app handles it)
journalctl -u myapp -f Tail logs live

Checklist

  • Non-root sudo user created, SSH key installed
  • System packages updated, unattended-upgrades enabled
  • Swap configured (2 GB swapfile, swappiness set to 10)
  • PermitRootLogin no and PasswordAuthentication no in sshd_config
  • Tailscale installed, SSHing via 100.x.x.x from here on
  • UFW active with default-deny incoming
  • fail2ban enabled
  • Caddy installed, Caddyfile configured, service running
  • Application running as a systemd service, enabled at boot

Type to search…

↑↓ navigate↵ selectEsc close