cd ../articles

System Security Homelab

Proxmox beyond the basics: nftables, LUKS, Docker in LXC and automation

Taking a Proxmox host from "it works" to something I can defend: a default-drop firewall, encrypted storage, nested Docker for Immich, and a script that creates containers in batches.

  • Proxmox
  • nftables
  • LUKS
  • Docker
  • Bash

After the basics (containers, VMs, a bridge or two), I wanted a Proxmox host I could actually defend: a default-drop firewall, encrypted storage for sensitive data, Docker where it helps, and no more creating containers by hand. This lab covered all of it.

5building blocks
~20 hof work
1lockout (of course)

Part 1: a host firewall with nftables

The Proxmox firewall is fine for simple cases. nftables, the successor to iptables in the Linux kernel, gives finer control with a much more readable syntax.

iptablesnftables
SyntaxOne command per rule, flag-heavyDeclarative file, readable blocks
IPv4 / IPv6Separate toolsOne inet table for both
Sets and mapsVia ipsetBuilt in
UpdatesRule by ruleAtomic: the whole file loads or nothing does
/etc/nftables.conf
#!/usr/sbin/nft -f
flush ruleset

table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;

        ct state established,related accept
        ct state invalid drop
        iif lo accept
        meta l4proto { icmp, ipv6-icmp } accept

        # SSH, rate limited
        tcp dport 22 ct state new limit rate 3/minute accept
        # Proxmox web UI
        tcp dport 8006 accept

        limit rate 5/minute log prefix "nft-drop: "
    }
    chain forward {
        type filter hook forward priority 0; policy accept;
    }
    chain output {
        type filter hook output priority 0; policy accept;
    }
}

include "/etc/nftables.d/*.conf"
/etc/nftables.d/nat.conf
table ip nat {
    chain prerouting {
        type nat hook prerouting priority dstnat;
        iifname "vmbr0" tcp dport { 80, 443 } dnat to 10.0.0.100
    }
    chain postrouting {
        type nat hook postrouting priority srcnat;
        oifname "vmbr0" masquerade
    }
}
nft -c -f /etc/nftables.conf     # check syntax without applying
systemctl enable --now nftables
nft list ruleset

A year later I applied the same approach to the whole homelab: From a pile of iptables rules to one nftables file.

Part 2: storage, mounts and LUKS

A plain data partition

lsblk
fdisk /dev/sdb          # n, p, 1, defaults, w
mkfs.ext4 /dev/sdb1
mkdir -p /mnt/data
blkid /dev/sdb1         # use the UUID, device names can change
echo 'UUID=<uuid> /mnt/data ext4 defaults,nofail 0 2' >> /etc/fstab
mount -a && df -h /mnt/data

nofail keeps the host booting even if the disk is missing, which matters on a headless box.

Encrypting it with LUKS

apt install -y cryptsetup
cryptsetup luksFormat /dev/sdb1               # type YES, then a strong passphrase
cryptsetup open /dev/sdb1 secure
mkfs.ext4 /dev/mapper/secure
mkdir -p /mnt/secure && mount /dev/mapper/secure /mnt/secure
cryptsetup status secure

Back up the LUKS header right away. If it gets corrupted, the data is unrecoverable, passphrase or not:

cryptsetup luksHeaderBackup /dev/sdb1 --header-backup-file /root/sdb1-luks-header.img
# then move that file off the host

Unlocking at boot, and what it costs

dd if=/dev/urandom of=/root/.luks-key bs=512 count=4 && chmod 400 /root/.luks-key
cryptsetup luksAddKey /dev/sdb1 /root/.luks-key
echo 'secure UUID=<luks-uuid> /root/.luks-key luks' >> /etc/crypttab
echo '/dev/mapper/secure /mnt/secure ext4 defaults,nofail 0 2' >> /etc/fstab

Part 3: containers vs. VMs

LXC containerKVM virtual machine
IsolationProcess-level, shared kernelHardware-level, own kernel
OverheadNear nativeVirtualised hardware
BootAbout a secondTens of seconds
RAMTens of MBHundreds of MB and up
Guest OSLinux onlyAnything (Windows, BSD…)
Good forServices: web, DNS, databasesOther OSes, kernel modules, strong isolation

Containers are not "small VMs". They share the host kernel, which makes them fast and cheap, and also means a kernel bug affects everyone. That is why I keep them unprivileged: root inside maps to an unprivileged user on the host.

Part 4: Docker inside LXC, for Immich

Some applications only ship as Docker Compose stacks. Running Docker inside an unprivileged LXC container keeps it isolated from the host and lets Proxmox snapshot and back up the whole thing as one unit. Immich, a self-hosted Google Photos, was the test.

  1. Create the container with nesting
    pct create 200 local:vztmpl/debian13.tar.zst \
      --hostname immich --cores 4 --memory 4096 --rootfs local-lvm:32 \
      --net0 name=eth0,bridge=vmbr1,ip=dhcp \
      --features nesting=1,keyctl=1 --unprivileged 1
  2. Install Docker and the Compose plugin
    apt update && apt install -y curl
    curl -fsSL https://get.docker.com | sh     # includes docker compose v2
    docker compose version
  3. Deploy Immich
    mkdir -p /opt/immich && cd /opt/immich
    wget -O docker-compose.yml https://github.com/immich-app/immich/releases/latest/download/docker-compose.yml
    wget -O .env https://github.com/immich-app/immich/releases/latest/download/example.env
    nano .env                  # UPLOAD_LOCATION, DB_PASSWORD, TZ
    docker compose up -d
    docker compose ps

Immich then answers on http://<container-ip>:2283: phone backup, face and object search, a map of your photos, all on your own hardware.

Part 5: CT-Creator, containers in batches

Creating containers by hand is slow and inconsistent. CT-Creator is my Bash script that asks a few questions once, then creates any number of containers in a row.

The convention it enforces is the useful part: the last octet of the IP equals the container ID. CT 102 is 192.168.1.102, CT 104 is .104. I never have to look an address up.

CT-creator.sh (core loop)
read -p "From which ID do you want to create containers: " ID
read -p "How many containers do you want to create: " CT
read -p "What amount of RAM (in MB | default - 512): " RAM; RAM=${RAM:-512}
read -p "Disk size in GB (default - 8): " DISK;            DISK=${DISK:-8}
read -p "Base IP (ex: 192.168.1) : " BASE_IP
read -s -p "Root password for containers: " ROOTPW; echo

for (( i=1; i<=CT; i++ )); do
    [ "$ID" -gt 254 ] && { echo "IP out of range, stopping."; exit 1; }
    IP="${BASE_IP}.${ID}/24"
    read -p "Hostname for CT $ID (default - ct${ID}): " HOSTNAME
    HOSTNAME=${HOSTNAME:-ct${ID}}

    pct create "$ID" "$TEMPLATE" \
        --hostname "$HOSTNAME" --cores "$CPU" --memory "$RAM" \
        --rootfs "${ROM}:${DISK}" \
        --net0 name=eth0,bridge=vmbr1,ip="$IP",gw="$GW" \
        --nameserver 1.1.1.1 --password "$ROOTPW" \
        --unprivileged 1 --features nesting=1 \
        $( [ "$ONBOOT" = "y" ] && echo "--onboot 1" ) \
      && pct start "$ID"

    ((ID++))
done

Every container comes out unprivileged, with nesting on, on the internal bridge and started. Ten containers take about two minutes, with no typos.

Next improvements on my list: SSH keys instead of a root password, auto-detecting storages and templates instead of asking, and a non-interactive mode reading a CSV, so the script itself can be versioned with the list of containers it built.

How it fits together

pmx-docker.svg
The lab, layer by layer
The lab, layer by layer
nftables

Default drop on the host, rate-limited SSH, NAT for the containers.

LUKS

Sensitive data and backups encrypted at rest.

LXC

One service per container, snapshot before every risky change.

Docker in LXC

Compose-only apps, still backed up by Proxmox as one unit.

CT-Creator

Consistent containers, predictable addresses.

Mistakes I made

Drop policy before the SSH rule

Locked out. Proxmox console, fix the rule, and from then on: console or automatic rollback for firewall changes.

Docker without nesting

Docker refused to start. pct set <id> --features nesting=1 and a restart fixed it; the script now sets it by default.

No LUKS header backup

I nearly lost a test volume while experimenting. Now luksHeaderBackup is the step right after luksFormat.

Hard-coded values in the script

The first version broke when the storage name changed. Prompts with sensible defaults fixed it.

What came next

Monitoring, backups with Proxmox Backup Server, a reverse proxy and SSO were on the list at the end of this lab. The reverse proxy came first; the rest is on the homelab roadmap.