Networking Security Homelab
Reaching my homelab from anywhere with WireGuard
Remote access to every self-hosted service without exposing any of them: WireGuard in a small LXC container, one-command client creation, and internal DNS over the tunnel.
- WireGuard
- LXC
- DynDNS
- Bash
I wanted my self-hosted services from outside the house, on 5G or on public Wi-Fi, without exposing any of them to the internet. A VPN is the clean answer: one UDP port open, everything else stays private.
Why WireGuard
The Proxmox node shares its RAM between a few dozen containers, so the VPN had to be light. WireGuard lives in the Linux kernel, uses a few megabytes of memory, and its configuration is a short readable file instead of the dozens of directives an OpenVPN setup usually needs. Modern cryptography, a small attack surface and good throughput even on a modest CPU.
It runs in its own LXC container (beta, 256 MB of RAM) rather than on the host: isolated from everything else, easy to snapshot, easy to rebuild.
Architecture
Text version
Internet │ UDP 51820, forwarded by the ISP box ▼ ISP box (public IP, kept up to date by DynDNS) │ ▼ Proxmox host vmbr0 192.168.2.100 ──DNAT──▶ vmbr1 │ ▼ beta · WireGuard LXC 192.168.1.10 tunnel 10.0.0.1/24 ├──▶ gamma · Technitium DNS 192.168.1.11 ├──▶ delta · Nginx Proxy Mgr 192.168.1.12 └──▶ every other service on vmbr1
A connected client gets an address in 10.0.0.0/24 and can reach the whole internal subnet as if it were plugged into vmbr1. Services are never VPN peers; only the devices I carry are: phone, laptop, a second Linux machine.
WireGuard inside LXC: two traps
The container has no TUN device by default
Pass /dev/net/tun through in the container config on the host, before starting it:
lxc.cgroup2.devices.allow: c 10:200 rwm
lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=fileRecent Proxmox versions can do the same with a single dev0: /dev/net/tun line, which is what I used later for the Tailscale bastion.
The kernel module lives on the host
A container shares the host kernel, so wireguard must be loaded on the Proxmox node: modprobe wireguard once, plus a line in /etc/modules so it survives reboots.
Server setup
apt update && apt install -y wireguard wireguard-tools qrencode
cd /etc/wireguard && umask 077
wg genkey | tee server_private.key | wg pubkey > server_public.key[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <server private key>
# let VPN clients reach the internal network
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
# phone
[Peer]
PublicKey = <client public key>
AllowedIPs = 10.0.0.2/32With net.ipv4.ip_forward = 1 in /etc/sysctl.conf, the interface comes up at boot:
systemctl enable --now wg-quick@wg0
wg show
One command per new device
Adding a peer by hand means generating keys, picking the next free IP, editing the server config and building a client file. Tedious and easy to get wrong, so I wrote wg-add-client.sh:
./wg-add-client.sh laptop
It does everything in one pass. The interesting parts:
# next free address = highest one already in wg0.conf + 1
LAST_IP=$(grep -oP "${VPN_SUBNET}\.\K[0-9]+" "$WG_DIR/$WG_INTERFACE.conf" | sort -n | tail -1)
CLIENT_IP="${VPN_SUBNET}.$((LAST_IP + 1))"
CLIENT_PRIVATE_KEY=$(wg genkey)
CLIENT_PUBLIC_KEY=$(echo "$CLIENT_PRIVATE_KEY" | wg pubkey)
# client file: internal DNS, stable DynDNS endpoint
cat > "$WG_DIR/clients/${CLIENT_NAME}.conf" <<EOF
[Interface]
PrivateKey = ${CLIENT_PRIVATE_KEY}
Address = ${CLIENT_IP}/24
DNS = 192.168.1.11
[Peer]
PublicKey = ${SERVER_PUBLIC_KEY}
Endpoint = ${SERVER_ENDPOINT}
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF
# append the peer to the server, reload without dropping anyone
cat >> "$WG_DIR/$WG_INTERFACE.conf" <<EOF
# ${CLIENT_NAME}
[Peer]
PublicKey = ${CLIENT_PUBLIC_KEY}
AllowedIPs = ${CLIENT_IP}/32
EOF
wg syncconf "$WG_INTERFACE" <(wg-quick strip "$WG_INTERFACE")
qrencode -t ansiutf8 < "$WG_DIR/clients/${CLIENT_NAME}.conf" # scan with the phone appwg syncconf is the key line: it applies the new peer without restarting the interface, so connected devices stay connected. The QR code sets up a phone in seconds. A web UI like wg-easy would do the same job, but for a device now and then, a script is lighter, exposes no extra web service, and taught me more about how WireGuard actually works.
Names, not IPs, over the tunnel
The client config sets DNS = 192.168.1.11, the internal address of Technitium. With the tunnel up, every query goes there. Service names resolve to Nginx Proxy Manager (192.168.1.12), which routes on the Host header exactly as it does at home. Nothing on the proxy changes whether I connect from the sofa or from a train.
Pitfalls worth knowing
You cannot always test from home
Connecting to your own public IP from inside your network needs hairpin NAT, which many ISP boxes do not do. My tunnel showed traffic going out and nothing coming back, and nothing was broken. Test from mobile data, or point the endpoint at the host's LAN address to validate the rest.
A dynamic IP breaks a hard-coded endpoint
My ISP rotates addresses, so clients eventually pointed at nothing. A DuckDNS name updated every five minutes by cron fixed it: clients use yourname.duckdns.org:51820 and stop caring.
Android Private DNS bypasses the tunnel's DNS
With Private DNS (DNS over TLS) enabled, Android sends queries straight to a public resolver and ignores the DNS pushed by WireGuard. Turning it off let internal names resolve.
wg-quick needs a resolver backend on Linux
On my Arch laptop the interface came up and was immediately deleted with a DNS error. wg-quick applies the DNS = line through resolvconf; enabling systemd-resolved (or installing openresolv) fixed it.
Leftover wildcard records
An old wildcard record answered for a name with no explicit entry and sent me to the wrong place. If a name resolves to something unexpected, look for a * record, and flush the client cache after every change.
Hitting the proxy by IP is not the same as by name
Opening NPM's IP directly shows its default page, because there is no hostname to route on. That is expected; it is exactly why the DNS layer matters.
Result
A VPN that costs 256 MB of RAM, reaches the whole lab by name from anywhere, and takes one command per new device. The build touched virtualisation, kernel modules, NAT, DNS, reverse proxying and a bit of Bash, which is exactly the mix I enjoy about running a homelab.