PortGate

Blueprints · Server Hardening

Zero-Trust 25 min · Alpine Linux

Alpine Linux v3.21 minimal VPS and container host hardening

Adds a wheel user with doas, locks root only after a public key exists, and applies a persistent stateful iptables policy that still accepts SSH.

Live config

Baked-in protections

Step 1 · bash

doas, SSH, and locked root

/root/portgate-blueprints/alpine-ssh.sh

passwd -l root runs only after a public key is installed. This is the control for an empty root password in an old image.

#!/bin/sh
set -eu
if [ "$(id -u)" -ne 0 ]; then
  echo "Run this script as root." >&2
  exit 1
fi
apk add --no-cache openssh doas
id "deploy" >/dev/null 2>&1 || adduser -D -s /bin/ash "deploy"
addgroup "deploy" wheel
mkdir -p /etc/doas.d
printf '%s\n' 'permit persist :wheel' > /etc/doas.d/wheel.conf
chmod 600 /etc/doas.d/wheel.conf
install -d -m 700 -o "deploy" -g "deploy" "/home/deploy/.ssh"
touch "/home/deploy/.ssh/authorized_keys"
chown "deploy:deploy" "/home/deploy/.ssh/authorized_keys"
chmod 600 "/home/deploy/.ssh/authorized_keys"
if [ -n "" ]; then
  grep -qxF "" "/home/deploy/.ssh/authorized_keys" || printf '%s\n' "" >> "/home/deploy/.ssh/authorized_keys"
fi
if [ ! -s "/home/deploy/.ssh/authorized_keys" ]; then
  echo "Stop. deploy has no SSH public key. Root was not locked and sshd was not changed." >&2
  exit 1
fi
mkdir -p /etc/ssh/sshd_config.d
cat > /etc/ssh/sshd_config.d/99-portgate.conf <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
EOF
sshd -t
rc-service sshd reload || rc-service sshd restart
passwd -l root

Step 2 · bash

Persistent stateful iptables

/root/portgate-blueprints/alpine-iptables.sh

Established connections and the SSH port are accepted before the input policy becomes DROP.

#!/bin/sh
set -eu
apk add --no-cache iptables
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
mkdir -p /etc/iptables
iptables-save > /etc/iptables/rules-save

Step 3 · bash

Install the PortGate agent

/root/portgate-blueprints/alpine-agent.sh

Uses --mode all. The token must include server, AI telemetry, and malware abilities.

#!/bin/sh
set -eu
# The token needs server:ingest, ai:telemetry, and malware:scan.
curl -fsSL "https://example.com/downloads/install.sh" | sh -s -- --token "paste-from-dashboard" --mode all --url "https://example.com"