The first time I configured WireGuard after a decade of OpenVPN, I felt stupid. Stupid because it took me fifteen minutes to set up a site-to-site tunnel that had previously taken me an entire afternoon and a small nervous breakdown. Stupid because the config file was twelve lines. Stupid because the handshake just worked, on the first try, behind NAT, through two carrier-grade NATs and a flaky hotel WiFi.
WireGuard is what happens when you let a kernel developer who hates complexity design a VPN. It's roughly 4,000 lines of code, compared to OpenVPN's 100,000+. It uses a small fixed set of modern primitives - Curve25519, ChaCha20-Poly1305, BLAKE2s, HKDF - instead of letting you choose from a menu of algorithms where 70% of them are bad. There are no cipher negotiations. There's no TLS handshake with twelve rounds of negotiation. There's just a Noise IK exchange, then encrypted packets. That minimalism is its entire personality.
The catch: minimalism means you have to understand what you're doing. There's no --server flag, no automatic IP assignment, no DHCP-over-VPN. You write a config file, you assign IPs, you wire up routing yourself. If you mess up AllowedIPs, traffic goes one way and never comes back, and you'll spend an hour staring at tcpdump before you find it. This article is the one I wish I'd had the first time - the mental model, the actual configs, and the production habits that separate "it works on my laptop" from "this is what we route prod traffic through."
If you've ever spent a Saturday afternoon fighting OpenVPN's TLS cipher mismatch errors, or stared at a 600-line IPSec policy dump wondering which setkey invocation was the one that actually worked, you're going to enjoy this.
What WireGuard Actually Is (and What It Isn't)
WireGuard is a Layer 3 VPN that uses a concept called cryptokey routing. Every peer has a public key. Every public key is associated with a set of allowed IP addresses. When an encrypted packet arrives, WireGuard decrypts it, looks at the source IP inside, and only accepts the packet if the source IP matches what's registered for that peer's public key. When you send a packet, WireGuard looks at the destination IP, finds the peer whose allowed IPs include that address, and encrypts the packet to that peer's public key. That's the whole routing model.
There's no concept of "server" and "client" at the protocol level. There are just peers. Some peers happen to sit behind public IPs and listen on UDP 51820, and we call those servers. Other peers sit behind NAT and reach out to the server's endpoint, and we call those clients. But the protocol doesn't care - either side can initiate, and either side can be the listener. The handshake is symmetric and quick (one round trip under good conditions).
What WireGuard is not:
- It's not a mesh networking product. It doesn't auto-discover peers, doesn't coordinate ACLs, doesn't have an admin UI. If you want that, you want Tailscale or Netmaker (which both build on top of WireGuard).
- It's not anonymous. There's no obfuscation layer. If you need to hide that you're using a VPN from a censor, use Shadowsocks or obfs4 over WireGuard.
- It's not a TLS replacement. It tunnels IP packets. It doesn't terminate HTTPS.
- It's not default-deny at the network layer. A peer with
AllowedIPs = 0.0.0.0/0can route all your traffic. The access control lives in your firewall rules, not in the protocol.
| VPN tech | LoC | Ciphers | Handshake | State | Best for |
|---|---|---|---|---|---|
| WireGuard | ~4k | Fixed (modern) | 1-RTT Noise | Stateless, sessionless | Site-to-site, road warrior, embedded |
| OpenVPN | ~100k | Negotiable (TLS) | Multi-RTT TLS | Stateful, heavy | Legacy, fine-grained ACLs |
| IPSec/StrongSwan | ~600k (kernel+user) | Negotiable | IKEv1/v2, multi-RTT | Stateful, kernel SAs | Enterprise, B2B tunnels |
| Tailscale | (uses WG) | WireGuard | WireGuard + coord | Mesh, auto-discover | Teams that don't want to think |
| OpenVPN across NAT | needs UDP or TCP 443 | TLS | slow | Stateful | When only TCP/443 egress works |
The honest take: if you're a single admin or a small platform team standing up site-to-site or road-warrior VPNs, WireGuard is the default. If you need enterprise-grade ACLs and compliance paperwork, IPSec still has a place. If you want zero-thought networking for a remote team, pay Tailscale.
Prerequisites
- A Linux box to act as the server. Anything with a kernel ≥ 5.6 (WireGuard landed in mainline in March 2020) works out of the box. Ubuntu 20.04+, Debian 11+, RHEL 9+, Alpine 3.14+. If you're on an older kernel, install
wireguard-toolsplus thewireguardDKMS module. - A public UDP port reachable from the internet. Usually 51820. If your server sits behind NAT without port forwarding, you'll need to put it on a real public host - a $5 VPS is enough.
- Root or sudo on the server.
wg-quickneedsipandiptables, which need CAP_NET_ADMIN. - A peer (laptop, phone, second server, a Raspberry Pi) to test from.
-
iptablesornftablesfor firewall rules. The examples useiptablesbecause it's still the lingua franca; if you're on a modern Debian/Ubuntu, you're really runningnf_tablesvia theiptables-nftwrapper, which is fine. - A basic understanding of CIDR notation, routing, and what NAT does. If
192.168.1.0/24looks alien, brush up first - WireGuard will not be kind to you. - Optional but recommended: a domain pointing at your server, with a cron job updating it if your home IP is dynamic. WireGuard endpoints can be specified as hostnames, and
wg-quickwill re-resolve on restart.
The Cryptokey Routing Mental Model
This is the part most people skip and then regret. Spend five minutes here, save five hours later.
Each WireGuard interface has a private key. From the private key you derive a public key. The public key is what you share with peers (it's safe to share - it's a public key). The private key stays on the box it was generated on.
When you configure a peer, you give WireGuard three things:
- The peer's public key - so WireGuard knows which key to encrypt to.
- The peer's endpoint - IP and port to send packets to. Optional for "server" peers that don't initiate; the server learns the endpoint from the first incoming packet.
- The peer's AllowedIPs - the set of IPs that (a) this peer is allowed to send traffic from, and (b) traffic destined to these IPs will be routed to this peer.
That last clause is the killer. AllowedIPs is both an ACL and a routing table entry. When you write AllowedIPs = 10.0.0.2/32, 192.168.50.0/24, WireGuard does two things: it accepts packets from this peer only if their source IP is 10.0.0.2 or in 192.168.50.0/24, and it adds a route to the system routing table sending 10.0.0.2/32 and 192.168.50.0/24 to this WireGuard interface.
The most common rookie mistake is overlapping AllowedIPs between peers. If peer A has AllowedIPs = 10.0.0.0/24 and peer B also has AllowedIPs = 10.0.0.0/24, WireGuard will refuse to load the second one, because it can't decide which peer to send a packet for 10.0.0.5 to. Each IP must belong to exactly one peer. This is also why "road warrior" peers each get their own /32 - they're not sharing an address.
Architecture Overview
For the rest of this article we'll build two real topologies. First, a road-warrior setup where your laptop and phone tunnel into a VPS and reach a private network behind it. Second, a site-to-site tunnel between two offices.
CodeCopy Road warrior topology
┌─────────┐ ┌──────────┐
│ Laptop │ 10.0.0.2 │ VPS │ 10.0.0.1
│ (NAT) │ ──────────── UDP 51820 ────────────> │ (public) │
└─────────┘ └────┬─────┘
│ eth0
▼
┌──────────┐
│ Office │ 10.10.0.0/24
│ network │
└──────────┘
Site-to-site topology
┌──────────┐ 10.99.0.1 10.99.0.2 ┌──────────┐
│ Office A │ <─────── UDP 51820 ─────────> │ Office B │
│ server │ 192.168.10.0/24 │ server │
│ │ 192.168.20.0/24 │ │
└──────────┘ └──────────┘
│ │
192.168.10.0/24 192.168.20.0/24
office LAN office LAN
The key insight is the same in both: the server's AllowedIPs for a peer must cover the network(s) that peer's LAN should be reachable through the tunnel. If office B's LAN is 192.168.20.0/24, then on office A's server, the peer entry for office B must include AllowedIPs = 10.99.0.2/32, 192.168.20.0/24. Otherwise A has no idea where to send packets destined for 192.168.20.5.
Installation
On any modern Linux with kernel ≥ 5.6:
BashCopy
shell
Debian / Ubuntu
sudo apt update && sudo apt install -y wireguard wireguard-tools
RHEL / Fedora / Rocky
sudo dnf install -y wireguard-tools
Arch
sudo pacman -S wireguard-tools
Alpine
sudo apk add wireguard-tools
plaintext
That's it. The kernel module is built in. wireguard-tools gives you wg (the raw control tool) and wg-quick (the friendly wrapper that reads .conf files and brings interfaces up/down).
On macOS, install wireguard-tools from Homebrew (brew install wireguard-tools) or grab the official Mac App Store client. On Windows, the official WireGuard client is fine. On iOS and Android, the official apps work great and can read configs via QR code - handy for distributing peer configs without copy/paste on a phone.
Verify:
BashCopy
shell
wg --version
wireguard-tools v1.0.20210914 - https://git.zx2c4.com/wireguard-tools/
markdown
If modprobe wireguard succeeds, you're on a kernel with the module. If it doesn't, you're either on a very old kernel or you need the DKMS package (apt install wireguard-dkms).
Generating Keys
Keys are 32-byte Curve25519 private keys, base64-encoded. Generate them with wg genkey, pipe to wg pubkey for the public side:
BashCopy
shell
Server
umask 077
wg genkey | tee server_private.key | wg pubkey > server_public.key
Peer (laptop)
wg genkey | tee laptop_private.key | wg pubkey > laptop_public.key
plaintext
umask 077 is important - without it, the private key file will be world-readable, and wg-quick will refuse to load it. (Good. This is one of those cases where the tool protects you from a stupid mistake.)
A key looks like:
CodeCopy
plaintext
server_private.key
sG5M2fE3xk7LZp1qVn8WcYbH4tRjFvKuA6dQ9wN0oI=
server_public.key
xT3pL7mN9vRqK2sF8yWcZbH4tRjFvKuA6dQ9wN0oI=
markdown
(Those are made up. Generate your own.)
The keys are not passwords. They're not derived from anything memorable. You store them in files, never in your head. If a key leaks, you rotate it - there's no password reset, just wg genkey again and update the configs on both ends.
Server Configuration
WireGuard configs live in /etc/wireguard/<interface>.conf. By convention, the interface is wg0. wg-quick up wg0 will look for /etc/wireguard/wg0.conf.
Here's a complete road-warrior server config. I'll annotate it below.
iniCopy
ini
/etc/wireguard/wg0.conf
[Interface]
The server's private key (the contents of server_private.key)
PrivateKey = sG5M2fE3xk7LZp1qVn8WcYbH4tRjFvKuA6dQ9wN0oI=
Tunnel IP for the server inside the VPN
Address = 10.0.0.1/24
UDP port to listen on
ListenPort = 51820
MTU - see the section on MTU below. 1420 is the safe default.
MTU = 1420
Bring up firewall/NAT rules when the interface comes up, tear them down on stop.
Replace eth0 with your server's actual internet-facing interface.
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
Peer: laptop
[Peer]
Laptop's public key
PublicKey = lT3pL7mN9vRqK2sF8yWcZbH4tRjFvKuA6dQ9wN0pZ=
Only this laptop may use 10.0.0.2 inside the tunnel.
AllowedIPs = 10.0.0.2/32
Peer: phone
[Peer]
PublicKey = yK8nQ2xJ9rL3mVp6sT7wBcD4fHgN1oI0uZ5aE8bPqY=
AllowedIPs = 10.0.0.3/32
Peer: another server that needs to reach the office LAN behind us
[Peer]
PublicKey = pW4rT9vN6cL2kJ3hM8xF1yB7dGqZ0sP5aE3bH9cNoY=
AllowedIPs = 10.0.0.4/32, 192.168.99.0/24
plaintext
Things to notice:
- The server's
Addressis10.0.0.1/24- this/24matters becausewg-quickwill create a route for10.0.0.0/24via thewg0interface. Without it, you'd need an explicit route. - Each peer's
AllowedIPsincludes only its own/32(for road warriors) or its/32plus its LAN CIDR (for site-to-site peers). No overlaps. -
PostUp/PostDownuse%ias a placeholder for the interface name (wg0).iptables -A FORWARDallows packets to be forwarded betweenwg0and other interfaces.MASQUERADEdoes source NAT for outgoing traffic so packets from10.0.0.2look like they come from the server's public IP.
Permissions on this file should be 600:
BashCopy
shell
sudo chmod 600 /etc/wireguard/wg0.conf
sudo chown root:root /etc/wireguard/wg0.conf
plaintext
wg-quick will refuse to load it otherwise.
Peer Configuration
For each peer (laptop, phone, second server), generate a small .conf file they can import into their WireGuard client:
iniCopy
ini
/etc/wireguard/wg0.conf on the laptop
[Interface]
PrivateKey =
Address = 10.0.0.2/24
Optional: use the server for DNS while tunnel is up (great with Pi-hole)
DNS = 10.0.0.1
[Peer]
Server's public key
PublicKey = xT3pL7mN9vRqK2sF8yWcZbH4tRjFvKuA6dQ9wN0oI=
Route EVERYTHING through the VPN (full-tunnel road warrior)
AllowedIPs = 0.0.0.0/0
For split-tunnel (only office traffic), use instead:
AllowedIPs = 10.0.0.0/24, 192.168.10.0/24
Server's public endpoint
Endpoint = vpn.example.com:51820
Keep the tunnel alive through NAT (25s is the canonical value)
PersistentKeepalive = 25
markdown
Three things worth explaining:
AllowedIPs = 0.0.0.0/0 is what makes this a "full tunnel" - all your traffic goes through the VPN. wg-quick is smart enough to handle the routing table changes (it adds a couple of fwmark-based rules so that WireGuard's own encrypted UDP packets don't get recursively routed back into the tunnel). For split tunnel, list only the CIDRs you want routed through the VPN.
PersistentKeepalive = 25 is the magic number for NAT. If your peer is behind NAT (and almost everyone is), the NAT translation entry for the UDP flow will time out after a few minutes of inactivity. After that, the server can't send packets back to you until you send one first. Setting keepalive to 25 seconds keeps the NAT mapping alive. Don't set it lower than 25 - you're just wasting bandwidth. Don't set it higher than ~120 - you'll start dropping tunnels. 25 is the convention, and it's right.
DNS = 10.0.0.1 tells wg-quick to set the system resolver to 10.0.0.1 while the tunnel is up, then restore it on teardown. On Linux this requires resolvconf or openresolv installed. On macOS/Windows/iOS/Android the official clients handle it natively. Pair this with a Pi-hole on 10.0.0.1 and you've got ad-blocking VPN DNS - lovely.
Generate a QR code for mobile clients:
BashCopy
shell
sudo apt install qrencode
qrencode -t ansiutf8 < laptop.conf
markdown
Scan with the WireGuard app on your phone, you're in.
Bringing It Up and the systemd Service
BashCopy
shell
Bring the interface up
sudo wg-quick up wg0
Check status
sudo wg show
interface: wg0
public key: xT3pL7mN9vRqK2sF8yWcZbH4tRjFvKuA6dQ9wN0oI=
private key: (hidden)
listening port: 51820
peer: lT3pL7mN9vRqK2sF8yWcZbH4tRjFvKuA6dQ9wN0pZ=
endpoint: 203.0.113.7:51820
allowed ips: 10.0.0.2/32
latest handshake: 4 seconds ago
transfer: 1.21 KiB received, 1.45 KiB sent
Tear down
sudo wg-quick down wg0
plaintext
The latest handshake line is the most important field in that output. If it says "4 seconds ago" or anything under 2 minutes, the tunnel is healthy. If it says "3 minutes ago" or never, the tunnel is dead - peer is offline, NAT mapping expired, or the endpoint changed.
To make WireGuard start on boot:
BashCopy
shell
sudo systemctl enable --now wg-quick@wg0
markdown
The @wg0 is a systemd template instance - /lib/systemd/system/wg-quick@.service is the template, and wg0 becomes %i. So wg-quick@wg1.service would manage /etc/wireguard/wg1.conf the same way. You can run multiple interfaces this way (wg0 for road warriors, wg1 for site-to-site, wg2 for the k8s control plane).
Routing and IP Forwarding
By default, Linux doesn't forward packets between interfaces. To make the server route packets from wg0 to eth0 (and back), you need to enable IP forwarding.
BashCopy
shell
Runtime
sudo sysctl -w net.ipv4.ip_forward=1
sudo sysctl -w net.ipv6.conf.all.forwarding=1
Persistent
echo 'net.ipv4.ip_forward = 1' | sudo tee /etc/sysctl.d/99-wireguard.conf
echo 'net.ipv6.conf.all.forwarding = 1' | sudo tee -a /etc/sysctl.d/99-wireguard.conf
sudo sysctl --system
plaintext
This is the single most common "tunnel comes up, handshake succeeds, but I can't ping anything" cause. People skip this step because WireGuard itself doesn't complain - it just faithfully encrypts and delivers packets to the local network stack, which then drops them because forwarding is off. Verify with:
BashCopy
plaintext
sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1
plaintext
If your cloud provider's VPC also has "source/dest check" or "anti-spoofing" enabled (AWS calls it SourceDestCheck, Hetzner and DigitalOcean have similar), you'll need to disable that on the network interface or attach a second elastic IP - otherwise the provider drops packets whose source doesn't match the VM's MAC. AWS in particular: set SourceDestCheck=false on the ENI.
Firewall Rules and NAT
The PostUp/PostDown lines in the server config handle the essentials, but real production servers usually have a more careful firewall. Let's rewrite it using explicit chains so it's clear what's happening, and add IPv6 support:
iniCopy
conf
/etc/wireguard/wg0.conf (server, IPv4 + IPv6)
[Interface]
PrivateKey =
Address = 10.0.0.1/24, fd00:dead:beef::1/64
ListenPort = 51820
MTU = 1420
Allow forwarding, NAT for v4 and v6
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; \
iptables -A FORWARD -o wg0 -j ACCEPT; \
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; \
ip6tables -A FORWARD -i wg0 -j ACCEPT; \
ip6tables -A FORWARD -o wg0 -j ACCEPT; \
ip6tables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; \
iptables -D FORWARD -o wg0 -j ACCEPT; \
iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE; \
ip6tables -D FORWARD -i wg0 -j ACCEPT; \
ip6tables -D FORWARD -o wg0 -j ACCEPT; \
ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
PublicKey =
AllowedIPs = 10.0.0.2/32, fd00:dead:beef::2/128
plaintext
If you're running ufw (Ubuntu's frontend) or firewalld (RHEL's), you need to open the UDP port for incoming handshakes:
BashCopy
shell
ufw
sudo ufw allow 51820/udp
sudo ufw route allow in on wg0 out on eth0
sudo ufw route allow in on eth0 out on wg0
firewalld
sudo firewall-cmd --permanent --add-port=51820/udp
sudo firewall-cmd --permanent --add-masquerade
sudo firewall-cmd --reload
plaintext
The ufw route allow lines matter because ufw's default FORWARD policy is DROP. You need explicit route rules for forwarded traffic, not just INPUT/OUTPUT.
If you're using nftables directly, the equivalent rule set:
CodeCopy
plaintext
table ip nat {
chain postrouting {
type nat hook postrouting priority 100;
oifname "eth0" masquerade
}
}
table ip filter {
chain forward {
type filter hook forward priority 0;
iifname "wg0" accept
oifname "wg0" accept
}
}
markdown
Site-to-Site Setup
Two offices, each with a server and a LAN. Office A has LAN 192.168.10.0/24, office B has LAN 192.168.20.0/24. The tunnel itself uses 10.99.0.0/30 (a tiny /30 - two usable IPs).
Office A server (/etc/wireguard/wg0.conf):
iniCopy
ini
[Interface]
PrivateKey =
Address = 10.99.0.1/30
ListenPort = 51820
Office B will initiate, so no PostUp NAT needed if both LANs route back via the tunnel.
But you DO need forwarding enabled system-wide.
[Peer]
PublicKey =
Office B's tunnel IP /32, plus its LAN so we route 192.168.20.0/24 over the tunnel
AllowedIPs = 10.99.0.2/32, 192.168.20.0/24
Endpoint = office-b.example.com:51820
PersistentKeepalive = 25
markdown
Office B server (/etc/wireguard/wg0.conf):
iniCopy
ini
[Interface]
PrivateKey =
Address = 10.99.0.2/30
ListenPort = 51820
[Peer]
PublicKey =
AllowedIPs = 10.99.0.1/32, 192.168.10.0/24
Endpoint = office-a.example.com:51820
PersistentKeepalive = 25
plaintext
Note that both sides specify each other's endpoint. Either side can initiate - whichever sends the first packet does the handshake, the other side responds. This is great for redundant links where you don't know which office will be reachable first.
On each office's LAN, the default gateway (usually your router) needs a static route: "to reach the other office's LAN, send via the WireGuard server." On a Mikrotik/Ubiquiti/OPNsense, that's a one-liner. On a plain Linux router:
BashCopy
shell
On office A's router (or on each client if there's no central router)
ip route add 192.168.20.0/24 via 192.168.10.1 # where 192.168.10.1 is the WG server's LAN IP
markdown
If you skip this, hosts on office A's LAN can't reach office B because their default gateway doesn't know to send traffic for 192.168.20.0/24 to the WireGuard box. They'll send it to their default route (the ISP), and it'll die.
IPv6 Dual-Stack
WireGuard handles IPv6 natively - you just add v6 addresses alongside v4 in the Address field. Pair each v4 with a v6 from a ULA or your routed v6 prefix:
iniCopy
ini
[Interface]
Address = 10.0.0.1/24, fd00:dead:beef::1/64
...
[Peer]
PublicKey = ...
AllowedIPs = 10.0.0.2/32, fd00:dead:beef::2/128
markdown
For dual-stack road warrior with AllowedIPs = 0.0.0.0/0, ::/0, the client tunnels both v4 and v6 default routes through WireGuard. This is great for clients on IPv6-broken networks (looking at you, half the hotel WiFi in Europe).
One gotcha: if your server only has IPv4 egress and you tunnel IPv6 traffic through it, your v6 traffic will die at the server. Either give the server an IPv6 address too, or don't tunnel ::/0.
The MTU Trap
WireGuard adds 80 bytes of overhead per packet (60 bytes for the outer UDP/IP headers, 20 for the WireGuard header on top of the inner packet). If your outer link has an MTU of 1500, your inner MTU must be at most 1420 - otherwise packets get fragmented, and fragmented UDP over the public internet is a recipe for weirdness.
wg-quick defaults to MTU = 1420, which is correct for the standard 1500-byte ethernet path. But if you're running WireGuard inside another tunnel (a WireGuard-over-WireGuard setup, or WireGuard through PPPoE which has 1492, or through a cloud overlay like Tailscale's DERP), you need to subtract another 80 (or more). On PPPoE: MTU = 1412. On WireGuard-in-WireGuard: MTU = 1340 or you'll start seeing massive packet loss at exactly 1413 bytes of payload and zero below it.
Symptoms of a bad MTU:
- Handshake succeeds, small packets (ping, SSH banner) work, but
curl https://example.comhangs forever. - Large file transfers stall at "X bytes received" and never complete.
-
sshconnects butapt updatehangs after the first few packets.
Diagnose with:
BashCopy
shell
From peer, send a packet that won't fragment, with a payload size that exceeds the inner MTU
ping -M do -s 1392 vpn.example.com
If this fails but ping -M do -s 1380 works, your MTU is too high.
markdown
The math: ping -s N sends an ICMP payload of N bytes, plus 28 bytes of IP+ICMP header. If you want to find the largest packet that fits, start at 1380 and increase by 8 until it fails, then back off. Set MTU = N + 28 in the config.
A reliable fallback when you're not sure: MTU = 1280. That's the IPv6 minimum, fits through almost any sane path, and the performance cost (a few more packets per large transfer) is usually invisible.
Monitoring
WireGuard itself doesn't expose metrics. The standard tool is wireguard_exporter - a small Go binary that reads wg show and exposes Prometheus metrics. There are a few flavors; the most-maintained one is github.com/MindFlavor/prometheus_wireguard_exporter.
BashCopy
shell
Quick run
docker run -d --name wg-exporter \
--cap-add NET_ADMIN \
--network host \
-p 9586:9586 \
mindflavor/prometheus-wireguard-exporter
Or as a systemd service
sudo apt install prometheus-wireguard-exporter
sudo systemctl enable --now prometheus-wireguard-exporter
plaintext
Metrics you care about:
-
wireguard_peer_rx_bytes_total/wireguard_peer_tx_bytes_total- bytes per peer -
wireguard_peer_last_handshake_seconds- the most important one. Alert if this exceeds 180 (3 minutes). It means the tunnel is dead. -
wireguard_peer_receive_errors_total- decryption failures, almost always means "wrong key on one side"
A basic Prometheus alert rule:
YAMLCopy
yaml
groups:
- name: wireguard rules:
- alert: WireguardPeerDown expr: time() - wireguard_peer_last_handshake_seconds > 180 for: 1m labels: severity: critical annotations: summary: "WireGuard peer {{ $labels.peer }} has no recent handshake" description: "Last handshake was {{ $value | humanizeDuration }} ago."
plaintext
For quick CLI checks without Prometheus, I keep this alias around:
BashCopy
shell
alias wgstat='watch -n 5 "wg show | grep -E \"peer:|endpoint:|latest handshake:|transfer:\""'
markdown
Common Mistakes
Overlapping AllowedIPs. Two peers both listing 10.0.0.0/24. wg-quick will refuse to start the interface with RTNETLINK answers: File exists. Fix: each peer gets its own /32 tunnel IP, plus optionally a LAN CIDR that no other peer claims.
Forgetting net.ipv4.ip_forward=1. Handshake works, ping 10.0.0.1 works (it's local to the server), ping 8.8.8.8 from the peer hangs forever. Fix: enable forwarding, then sysctl --system.
Trusting wg-quick to handle dynamic endpoints. WireGuard caches the endpoint address after the first packet from a roaming peer. If the peer's IP changes (e.g., phone moves from WiFi to LTE), the server will keep trying the old endpoint until the peer sends a new packet - which it will, because PersistentKeepalive = 25 keeps the flow alive. So actually this is mostly fine, as long as you set keepalive. The mistake is omitting it on a NAT'd peer.
Setting PersistentKeepalive = 0 (or omitting it) on a peer behind NAT. Symptom: handshake succeeds, traffic flows for 2-3 minutes, then stops dead. Restarting the peer's WireGuard fixes it for another 2-3 minutes. That's the NAT translation table timing out. Fix: PersistentKeepalive = 25.
Wrong UDP port in Endpoint. The ListenPort on the server is the port the server receives on. The Endpoint on the peer must be the public IP and port of the server, after any port forwarding. A common mistake is putting the server's private IP in the peer's Endpoint field - that only works if both peers are on the same LAN, and is a useless config for road warriors.
AllowedIPs = 0.0.0.0/0 on the server's [Peer] section for a road warrior. The server's AllowedIPs for a peer should be that peer's /32 only. If you put 0.0.0.0/0, you're telling the server "this peer can send me packets from any source IP" - that's a security hole, and it also breaks routing because the server will try to send all traffic (including its own internet traffic) to that peer.
MTU too high on nested tunnels. Symptom: handshake works, small packets work, large transfers stall. See the MTU section.
Letting keys leak via file permissions. wg0.conf should be chmod 600. wg-quick enforces this, but if you've stored keys in /etc/wireguard/keys/ for scripting, check those too. Any backup of /etc/wireguard/ should be encrypted at rest.
Forgetting to re-add the route after a network restart. If the server's main interface restarts (e.g., systemctl restart networking), wg0 stays up but the system routing table may be wiped. systemctl restart wg-quick@wg0 restores it. Or just wg-quick down wg0 && wg-quick up wg0.
Troubleshooting
Handshake never completes. From the server, tcpdump -i eth0 udp port 51820 -n. If you see incoming packets from the peer's IP but no outgoing reply, the server is receiving but rejecting - usually wrong peer public key in the server's config, or the server's ListenPort doesn't match. If you see no incoming packets at all, the peer's Endpoint is wrong, a firewall is dropping UDP 51820, or the peer is behind a captive portal that blocks UDP.
Handshake completes, no traffic flows. Almost always AllowedIPs or IP forwarding. From the peer: ping 10.0.0.1 (the server's tunnel IP). If that works but ping 8.8.8.8 doesn't, the server isn't forwarding - check sysctl net.ipv4.ip_forward and the PostUp iptables rules. From the server: tcpdump -i wg0 -n to see if packets are arriving on the tunnel. If they arrive but nothing comes back, the server's reply isn't being routed - check the routing table with ip route get 8.8.8.8.
One-way traffic. ping from peer to server works, server to peer doesn't (or vice versa). Usually AllowedIPs mismatch. The peer's AllowedIPs includes the source address the peer is sending from; the server's AllowedIPs for that peer must include the same address. If the peer has Address = 10.0.0.2/24 but the server's AllowedIPs = 10.0.0.2/32, that's correct. If the peer has Address = 10.0.0.2/32 but the server has AllowedIPs = 10.0.0.0/24 (overlapping another peer), you'll get weird partial failures.
Handshake works, but performance is terrible / large transfers stall. MTU. Drop to 1280 and re-test. If that fixes it, walk MTU up to find the sweet spot.
Peer disconnects every few minutes. NAT timeout. Add PersistentKeepalive = 25 on the NAT'd side.
wg-quick up fails with RTNETLINK answers: File exists. Overlapping AllowedIPs, or a stale route from a previous failed wg-quick up. wg-quick down wg0 and try again. If it still fails, ip route flush dev wg0 and ip link delete wg0, then wg-quick up wg0.
Peer can reach server's tunnel IP but not the LAN behind it. Two possibilities: the LAN hosts don't have a route back to the peer's tunnel subnet (add ip route add 10.0.0.0/24 via <wg-server-lan-ip> on the LAN router), or the server's iptables FORWARD chain is dropping packets (check iptables -L FORWARD -v -n).
Server reboots, WireGuard doesn't come up. Forgot systemctl enable wg-quick@wg0. Or the .conf file has PostUp rules that fail (e.g., iptables rule that conflicts with an existing rule) and wg-quick aborts. Check journalctl -u wg-quick@wg0.
wg show shows the peer with latest handshake: (none) but the peer reports handshake success. The peer is talking to a different server than you think. Probably the peer's Endpoint points at an old server IP, or DNS is resolving to a stale record. Re-resolve the endpoint hostname and re-test.
Production Considerations
Key rotation. Treat WireGuard keys like TLS certs - rotate them on a schedule. Quarterly is reasonable for most setups, more often if you've had staff turnover. The rotation is painless: generate new keys on both ends, swap the public keys in both configs, systemctl restart wg-quick@wg0 on both sides. There's a brief blip (a second or two) as the new handshake completes. I keep a small script that generates new keys, writes new config files, and SSHes both ends to restart.
Multi-hop. If you're paranoid (or you're routing sensitive traffic through untrusted territory), chain WireGuard tunnels. Peer → VPS1 → VPS2 → destination. Each hop decrypts only its layer; the inner tunnel stays encrypted end-to-end. This costs latency but adds defense in depth.
Dynamic endpoints with DDNS. If the server sits on a dynamic IP (home server behind consumer ISP), run a DDNS client (ddclient, or a Cloudflare API script). The peer's Endpoint should be the hostname. wg-quick resolves it on interface up. For mid-session IP changes, you need a small cron job that re-resolves the endpoint:
BashCopy
shell
/etc/cron.hourly/wg-reresolve
!/bin/bash
Re-resolve all WireGuard endpoints in case DNS changed
for iface in /etc/wireguard/*.conf; do
name=$(basename "${iface%.conf}")
if wg show "$name" >/dev/null 2>&1; then
wg-quick down "$name" && wg-quick up "$name"
fi
done
(Yes, this drops the tunnel briefly. If that's unacceptable, look at the wg set command - you can update an endpoint without taking the interface down: wg set wg0 peer <PUBKEY> endpoint <new-ip:port>.)
Kill switch. For road warriors, a kill switch ensures traffic never leaks outside the tunnel. wg-quick already sets up fwmark-based routing for full-tunnel configs (AllowedIPs = 0.0.0.0/0), which means if the tunnel goes down, packets can't find a route and are dropped. Verify with ip rule - you should see a rule like 32765: not from all fwmark 0xca6c lookup 51820. If you want stricter (block all traffic even to the WireGuard server when the tunnel is down), you need explicit iptables OUTPUT policy rules. The Tailscale client does this for you if you'd rather not think.
DNS through the tunnel. Set DNS = 10.0.0.1 on peers and run a resolver on the server. Pi-hole is the canonical choice - ad-blocking VPN DNS, two birds, one stone. dnsmasq or unbound work if you just want DNS. This also fixes the "DNS leak" issue where your OS keeps using the local resolver even when the tunnel is up.
Secrets in config. The private key in /etc/wireguard/wg0.conf is a secret. Don't commit it to git. Don't put it in your dotfiles repo. Use Ansible Vault, SOPS, or just don't template this file at all - generate keys on each host and copy the public keys around manually. For team setups, the typical flow is: each operator generates their own keypair, sends the public key to the platform team, the platform team adds a [Peer] block to the server config.
Cost Considerations and Alternatives
WireGuard itself is free. The costs are the server(s) and your time.
| Option | Cost (monthly) | Best for |
|---|---|---|
| WireGuard on a $5 VPS | $5 | Solo / small team road warrior |
| WireGuard on dedicated hardware (home) | $0 (already own) | Home lab, personal VPN |
| Tailscale (free up to 100 devices) | $0 / $6+/user/mo for teams | Mesh networking for teams |
| Tailscale on dedicated infrastructure | $0 + your infra | Self-hosted mesh |
| Netmaker (self-hosted) | $0 | WireGuard-based mesh with UI |
| OpenVPN Cloud | $7+/connection/mo | Legacy enterprise |
| AWS Site-to-Site VPN | $36/mo per connection + egress | AWS-native, transit gateway |
| Commercial IPSec appliances | $$ to $$$$ | Enterprise with compliance reqs |
If you're a team of <100 people who need remote access to internal services, Tailscale is genuinely great - it automates away all the manual config in this article. You pay for that with vendor lock-in (their coordination server is closed source, though Headscale is a self-hosted alternative). If you're routing prod traffic between data centers, bare WireGuard is the right call.
The honest framing: WireGuard is the Linux kernel of VPNs. It does the core thing well, and everything else is your problem. Tailscale is the managed cloud. Use bare WireGuard when you need control, audit, or zero ongoing cost; use Tailscale when you need to ship something today and don't want to maintain configs.
Real-World Use Cases
Remote access to a homelab. A Raspberry Pi at home running WireGuard, port-forwarded through your router. From your laptop anywhere, you reach your home LAN, your self-hosted services, and your Pi-hole. Cost: $0 (beyond the Pi).
Site-to-site between two small offices. Two OPNsense boxes or Linux servers, one WireGuard tunnel, static routes on each LAN's router. Replaces a $2,000/year SD-WAN appliance for offices under 50 people.
Kubernetes node-to-node overlay. Kilo, WireGuard-based CNI, builds a mesh between k8s nodes. Useful when your nodes are in different clouds or behind NAT. The flat pod network rides WireGuard underneath.
Secure bastion replacement. Instead of a bastion host SSH-jumping your way into private subnets, run WireGuard on a tiny VPS, expose only the WireGuard UDP port, and access your private instances directly over the tunnel. Closes port 22 to the world.
Multi-region database replication. Postgres streaming replication between two regions, over WireGuard. Encrypts at the network layer so you don't need to configure Postgres' TLS, and adds a stable IP overlay so failover scripts don't need to update replication configs.
Privacy on hostile WiFi. Your phone, configured with full-tunnel WireGuard to your home server. Coffee shop WiFi sees one UDP stream to your house. Your DNS, your traffic, your rules.
Conclusion
WireGuard is the rare piece of infrastructure that's both simpler and more secure than what came before it. The protocol is small enough to hold in your head. The config is small enough to fit on a Post-it. The performance is better than OpenVPN, the security story is cleaner than IPSec, and the deployment story is something a sing...
