# Harden a VPS before the first deploy

A fresh Docker host answers on every published port to the whole internet. This page turns it into a box with one door.

By the end you have one machine that boots into a state you can describe from memory: no public SSH, one non-root deploy principal, packet filters that agree, unattended security updates, a UTC clock, Docker with bounded logs, and a layout the reference pages assume. Commands are for Ubuntu 24.04, with Fedora 41 differences called out. Set these first:

```bash
export APP=myapp
export VPS_IP=203.0.113.10      # from your provider, never committed
export TAILNET_IP=100.64.0.10
```

## Sizing: what one box actually carries

The reference stack on one node is Traefik, CrowdSec, one web container, one worker, Postgres and Redis. The ceilings in the app compose are 1 GB app, 256 MB worker, 384 MB Postgres, 128 MB Redis: about 1.8 GB before the ingress. Limits are ceilings, not usage, and the app's 1 GB is not decorative: a 512 MB container OOM-kills Octane during `artisan up`, a deploy that dies with the maintenance flag on.

- 2 GB, 2 vCPU: one small app with an external database. Not enough for app plus Postgres plus Redis plus a build.
- 4 GB, 2 vCPU: the floor for the full stack on one box. Builds still want swap.
- 8 GB: two apps, or one app with a real queue backlog.

40 GB of disk is the minimum; images are large and Postgres grows.

Free ARM tiers are arm64, so an ARM runner and a multi-arch manifest are part of the deal. Every image then needs an arm64 build; x86 has no such tax. Pick what your CI can build natively for.

Budget providers charge a few euros a month with tens of terabytes of egress; others cost several times that for the same RAM. Free ARM tiers are capacity-constrained per availability domain and idle instances can be reclaimed, so treat a free instance as expendable, never as the only copy of a database.

Swap is an OOM seatbelt, not capacity: 2 GB on a 4 GB box turns a build spike into a slow build.

```bash
# Ubuntu 24.04
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
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swap.conf
sudo sysctl --system
free -h
swapon --show
```

Fedora's zram does the same job in compressed RAM:

```bash
sudo dnf install -y zram-generator-defaults
sudo systemctl daemon-reload
sudo systemctl start systemd-zram-setup@zram0.service
zramctl
```

## Base OS, clock and unattended upgrades

Ubuntu 24.04 LTS gives five years of security updates and `unattended-upgrades`. Fedora gives a newer kernel and thirteen months per release, which is a calendar obligation. Both work; the difference that bites is the yearly host upgrade.

Set the clock to UTC and leave it there. Every log line you correlate is UTC: Cloudflare, Traefik, Postgres, Laravel. A host in local time makes incident timelines wrong by hours.

```bash
# Ubuntu 24.04
sudo apt update && sudo apt -y full-upgrade
sudo apt -y install curl ca-certificates gnupg jq git htop unattended-upgrades apt-listchanges ufw fail2ban
sudo hostnamectl set-hostname "${APP}-01"
sudo timedatectl set-timezone UTC
sudo systemctl enable --now systemd-timesyncd
timedatectl
```

```bash
# Fedora 41
sudo dnf -y upgrade --refresh
sudo dnf -y install curl ca-certificates gnupg2 jq git htop firewalld dnf-automatic
sudo hostnamectl set-hostname "${APP}-01"
sudo timedatectl set-timezone UTC
sudo systemctl enable --now chronyd
timedatectl
```

Security patches install without you, but kernel and daemon patches need a reboot, so schedule one and let `restart: unless-stopped` bring the containers back.

```bash
# Ubuntu: unattended security upgrades, reboot at 04:30 UTC
sudo dpkg-reconfigure --priority=low unattended-upgrades
sudo tee /etc/apt/apt.conf.d/20auto-upgrades >/dev/null <<'EOF'
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
APT::Periodic::AutocleanInterval "7";
EOF
sudo sed -i 's|^//Unattended-Upgrade::Automatic-Reboot .*|Unattended-Upgrade::Automatic-Reboot "true";|' /etc/apt/apt.conf.d/50unattended-upgrades
sudo sed -i 's|^//Unattended-Upgrade::Automatic-Reboot-Time .*|Unattended-Upgrade::Automatic-Reboot-Time "04:30";|' /etc/apt/apt.conf.d/50unattended-upgrades
sudo unattended-upgrade --dry-run --debug 2>&1 | tail -20
```

```bash
# Fedora: security-only automatic updates, no automatic reboot
sudo tee /etc/dnf/automatic.conf >/dev/null <<'EOF'
[commands]
upgrade_type = security
apply_updates = yes
reboot = never
EOF
sudo systemctl enable --now dnf-automatic.timer
systemctl list-timers dnf-automatic.timer
```

An automatic Docker upgrade restarts the daemon. `live-restore` keeps containers running, but published-port rules are rebuilt, which is why the drop-in in [chapter 03](/devops-guide/cloudflare-origin-lockdown) exists.

## The deploy user, and what the docker group means

```bash
sudo adduser --disabled-password --gecos "" deploy
sudo usermod -aG docker deploy
sudo install -d -m 0700 -o deploy -g deploy /home/deploy/.ssh
sudo -u deploy ssh-keygen -t ed25519 -N "" -C "deploy@$(hostname -s)" -f /home/deploy/.ssh/id_ed25519
id deploy
```

The docker group is root. Anyone in it can mount the host filesystem into a container and take the machine, so `deploy` is a root-equivalent principal with no sudo and no password. It is an audit boundary, not a security boundary: every action it takes is a `docker` or `git` command. Do not reuse it for humans and never put it in sudoers. Human admin happens as root over the tailnet.

We skip rootless Docker here: binding 80 and 443, the socket and the networking model all get worse.

## SSH: keys only, no public port

```bash
sudo tee /etc/ssh/sshd_config.d/00-hardening.conf >/dev/null <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
PubkeyAuthentication yes
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2
AllowUsers deploy root
EOF
sudo sshd -t && sudo systemctl restart ssh
sudo sshd -T | grep -E '^(passwordauthentication|permitrootlogin|maxauthtries|allowusers) '
```

Run `sshd -t` before every restart and keep the current session open until a second one works. A typo here is a console session. Join the tailnet, then close port 22 in the cloud firewall:

```bash
curl -fsSL https://tailscale.com/install.sh | sh
sudo tailscale up --hostname "${APP}-01"
tailscale status
tailscale ip -4
```

The tailnet is now the only path worth having, so OpenSSH stops listening on the public address entirely. Bind it to the tailnet after the interface exists:

```bash
printf 'ListenAddress %s\nListenAddress 127.0.0.1\n' "${TAILNET_IP}" \
  | sudo tee /etc/ssh/sshd_config.d/20-tailnet-only.conf
sudo install -d -m 0755 /etc/systemd/system/ssh.service.d
printf '[Unit]\nAfter=tailscaled.service\nWants=tailscaled.service\n' \
  | sudo tee /etc/systemd/system/ssh.service.d/10-after-tailscale.conf
sudo systemctl daemon-reload
sudo sshd -t && sudo systemctl restart ssh
sudo ss -tlnp | grep ':22 '
```

From outside the tailnet, `nc -vz "${VPS_IP}" 22` now fails; from the tailnet, `ssh deploy@${TAILNET_IP}` answers. `sshd` refuses to start if the address does not exist at boot, which is why the unit waits for `tailscaled`. Tailscale SSH is the other option, and it is a poor fit here: it wants :22 on the same address and ignores `authorized_keys` forced commands, so one server, one transport.

## One key per app, with a forced command

Shared deploy keys are how one leaked CI secret becomes every app. One key per app, one per purpose, no passphrase, and every authorized key locked to a single command.

```bash
# On the workstation that pushes: one key per app, no passphrase.
ssh-keygen -t ed25519 -N "" -C "ci-${APP}@deploy" -f ~/.ssh/"${APP}"_deploy
```

```bash
#!/usr/bin/env bash
# /usr/local/bin/deploy-myapp on the VPS (myapp is the example APP).
# The only command the CI key can run; anything else exits 64.
# The full release script, with rollback, lives in [chapter 10](/devops-guide/pull-only-deploys).
set -euo pipefail

APP="$(basename "$0")"
APP="${APP#deploy-}"
PATTERN='^deploy v[0-9]+\.[0-9]+\.[0-9]+$'

if [[ -z "${SSH_ORIGINAL_COMMAND:-}" || ! "${SSH_ORIGINAL_COMMAND}" =~ ${PATTERN} ]]; then
  echo "usage: deploy vX.Y.Z" >&2
  exit 64
fi

TAG="${SSH_ORIGINAL_COMMAND#deploy }"
cd "/srv/${APP}"

export IMAGE_TAG="${TAG}"
docker compose pull
docker compose up -d --remove-orphans

# Replace with your release step, or delete it for a stateless app.
docker compose exec -T app php artisan migrate --force

DOMAIN="$(cat DOMAIN)"
curl -fsS -o /dev/null "https://${DOMAIN}/up"
```

Save the block above as `deploy-myapp.sh`, then:

```bash
sudo install -o root -g root -m 0755 deploy-myapp.sh /usr/local/bin/deploy-myapp
sudo install -d -m 0700 -o deploy -g deploy /home/deploy/.ssh
```

```
# /home/deploy/.ssh/authorized_keys, mode 0600 deploy:deploy
restrict,command="/usr/local/bin/deploy-myapp" ssh-ed25519 <PUBLIC_KEY> ci-myapp@deploy
```

Prove the lock by running the command you are not allowed to run and reading the refusal:

```bash
ssh -i ~/.ssh/"${APP}"_deploy -o IdentitiesOnly=yes deploy@"${TAILNET_IP}" 'id; cat /etc/shadow'
# expect: usage: deploy vX.Y.Z
ssh -i ~/.ssh/"${APP}"_deploy -o IdentitiesOnly=yes deploy@"${TAILNET_IP}" 'deploy v1.0.0'
```

If CI runs on a self-hosted runner on the same box, the key never leaves the host and the inbound path disappears; the deploy job pulls images from the machine itself. If CI runs elsewhere, the SSH path to the host is the one you lock down. Pick one deliberately and document only that one.

## Firewall layers, and why ufw does not cover Docker

Three layers, each able to stand alone. The cloud firewall (the provider's network firewall, such as a security group or a managed cloud firewall) is out of band and survives a broken host. The host firewall covers non-container services. Docker is covered by neither.

Docker writes DNAT rules into `nat/PREROUTING` and inserts its own `FORWARD` jumps ahead of ufw's chains, so a published port answers even when ufw says deny. firewalld has the same blind spot: Docker writes nftables rules it does not manage.

A real example: `firewalld` inactive, no `ufw`, empty `DOCKER-USER`, 80/443 published everywhere. A request to the public IP with the right `Host` header returns 200, bypassing Cloudflare. That is the default state of a fresh Docker host. A hardened host filters 80/443 to Cloudflare and nothing else.

```bash
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow in on tailscale0
sudo ufw enable
sudo ufw status verbose

# Prove that ufw does not govern published ports.
docker run -d --rm -p 8080:80 --name ufw-bypass nginx:alpine
curl -s -m 5 -o /dev/null -w 'bypass test: %{http_code}\n' "http://${VPS_IP}:8080/"
docker rm -f ufw-bypass
```

If that prints 200 while ufw reports 8080 denied, you have confirmed the bypass and should never trust ufw for a published port.

```bash
sudo iptables -S DOCKER-USER
sudo ip6tables -S DOCKER-USER
```

Both chains must be non-empty before you publish. [chapter 03](/devops-guide/cloudflare-origin-lockdown) has the full ruleset, unit and drop-in.

## Docker: install and daemon config

```bash
# Ubuntu 24.04
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo 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" | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt update
sudo apt -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
```

```bash
# Fedora 41: dnf5 syntax, dnf4 uses --add-repo
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager addrepo --from-repofile=https://download.docker.com/linux/fedora/docker-ce.repo
sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
```

Three settings and one prohibition. Log rotation, because json-file grows without limit. `live-restore`, because a daemon upgrade should not stop your site. `no-new-privileges`, so a setuid binary in a container cannot gain privileges. The prohibition is `"iptables": false`, which removes the enforcement point.

```bash
sudo tee /etc/docker/daemon.json >/dev/null <<'EOF'
{
  "log-driver": "json-file",
  "log-opts": { "max-size": "10m", "max-file": "3" },
  "live-restore": true,
  "no-new-privileges": true
}
EOF
sudo systemctl enable --now docker
sudo systemctl restart docker
docker info | grep -i -E 'logging driver|live restore'
sudo -u deploy docker ps
```

Never expose the Docker socket over TCP, and treat `:ro` on the socket as documentation rather than a control: read access to the socket is still root.

## Directory layout

One directory per app, one for the ingress, secrets outside both. `/srv` rather than `/home`, because Ubuntu creates home directories mode 750 and a bind mount under one is invisible to a container process running as uid 82: the deploy succeeds and the app fails on write, with an error that points nowhere. If you keep apps under `/home/deploy/apps/${APP}`, run `chmod 755 /home/deploy`.

```bash
sudo install -d -m 0755 /srv/traefik
sudo install -d -m 0755 /srv/traefik/providers
sudo install -d -m 0700 /srv/traefik/secrets

sudo install -d -m 0755 -o deploy -g deploy "/srv/${APP}"
sudo install -d -m 0775 "/srv/${APP}/.storage/logs" "/srv/${APP}/.storage/app" "/srv/${APP}/.storage/framework"
sudo chown -R 82:82 "/srv/${APP}/.storage"
sudo install -d -m 0750 "/srv/${APP}/.postgres-db"
sudo chown 70:70 "/srv/${APP}/.postgres-db"
sudo install -d -m 0700 -o deploy -g deploy "/srv/${APP}/secrets"

stat -c '%a %U:%G %n' "/srv/${APP}" "/srv/${APP}/.storage" "/srv/${APP}/.postgres-db"
```

```
/srv/traefik/
  compose.yml
  traefik.yml
  providers/
  secrets/            mode 0700
/srv/myapp/
  compose.yml
  DOMAIN
  .storage/logs
  .storage/app
  .storage/framework
  .postgres-db/
  secrets/            mode 0700
```

UIDs follow the base image: alpine `www-data` is 82, Debian 33, alpine `postgres` 70, Debian 999. Anything sharing a volume must share an image family, and the deploy job must re-apply ownership after creating directories, because `mkdir` as `deploy` makes directories it cannot hand over.

## First boot verification

```bash
# 1. Nothing unexpected listens on a public interface.
sudo ss -tlnp | grep -v -e '127.0.0.1' -e '::1'

# 2. sshd refuses passwords. prohibit-password normalizes to without-password.
sudo sshd -T | grep -E '^(passwordauthentication|permitrootlogin|maxauthtries|allowusers) '

# 3. Every authorized key is locked to a command.
sudo awk '{print $1, $3}' /home/deploy/.ssh/authorized_keys

# 4. Clock is UTC and synchronized.
timedatectl show -p Timezone -p NTPSynchronized

# 5. The persisted ruleset does not contradict the NSG.
sudo iptables -S INPUT | head -20

# 6. Docker config took effect and the deploy user can drive it.
docker info | grep -i -E 'logging driver|live restore'
sudo -u deploy docker ps

# 7. Automatic updates are scheduled.
systemctl is-enabled unattended-upgrades 2>/dev/null || systemctl is-enabled dnf-automatic.timer
```

## Failure modes

- `curl http://${VPS_IP}/` returns 200 -> `DOCKER-USER` is empty -> apply the page 03 ruleset, test from outside.
- compose fails with `permission denied` on `/app/storage` -> 750 home or wrong UID -> `chmod 755` the parent, `chown` the tree.
- a deploy key works by hand but fails in CI -> passphrase, or a missing `command=` path -> regenerate, read `journalctl -u ssh`.
- the forced command answers `usage:` for a valid tag -> regex mismatch, usually a trailing carriage return -> log `SSH_ORIGINAL_COMMAND`.
- port 22 is reachable from the internet -> cloud rule open, or sshd on `0.0.0.0` -> close the rule, bind to the tailnet.
- the site is down every morning at 04:30 -> automatic reboot without a restart policy -> `restart: unless-stopped`.
- the disk fills in a week -> no `max-size`, no pruning -> fix `daemon.json`, add a prune timer.
- Postgres is OOM-killed during a build -> no swap, no build cap -> add the swapfile, cap concurrency.

## Checklist

- [ ] 4 GB RAM, 2 vCPU for the full stack, swap or zram configured.
- [ ] Patched OS, hostname, UTC clock, NTP, automatic security updates.
- [ ] `deploy` exists, no password, docker group, no sudoers.
- [ ] `sshd` refuses passwords, port 22 closed publicly.
- [ ] Tailscale up, OpenSSH bound to the tailnet only, one ed25519 key per app, refusal verified.
- [ ] Cloud firewall deny-by-default, agreeing with the host rules.
- [ ] `DOCKER-USER` populated for IPv4 and IPv6, ufw bypass demonstrated.
- [ ] `daemon.json` sets rotation, `live-restore`, `no-new-privileges`.
- [ ] `/srv/traefik` and `/srv/${APP}` exist with documented modes and owners.
- [ ] The verification block passes, run by someone else.
