Skip to content

WireGuard VPN setup — server, peers, and DNS-aware routing

Deploying a WireGuard server, generating per-peer configs, splitting DNS for split-tunnel, and operating the tunnel like a service rather than a hand-rolled script.

WireGuard is the modern, in-kernel VPN that replaced OpenVPN for most new deployments. Its config file is half a page, the daemon is a kernel module, and the cipher suite is fixed (no negotiation, no surprises). This article covers a production-ready WireGuard server, a per-peer config workflow that scales, and the DNS-aware routing that makes split-tunnel actually pleasant to use.

How to verify

Inspect what is up, what is connected, and the routing:

sudo wg show
sudo systemctl status wg-quick@wg0
sudo ip -4 route show table all | grep wg
sudo sysctl net.ipv4.ip_forward net.ipv6.conf.all.forwarding
sudo ss -tunlp | grep -E ':51820|wireguard'
sudo journalctl -u wg-quick@wg0 --since "1 hour ago"

wg show is the truth: peers, last handshake, transferred bytes. A peer that has never handshook is a misconfigured client or a blocked UDP port — the two most common WireGuard issues.

What’s happening

WireGuard runs on top of UDP. Each peer is identified by an Ed25519-shaped public key; there is no notion of usernames, just keys. The server’s config lists allowed peer public keys and the source IPs they are permitted to use; the client’s config lists the server’s public key and the endpoint to dial.

wg-quick is the systemd-friendly wrapper that reads /etc/wireguard/wg0.conf, sets up the interface, applies routes from the AllowedIPs field, and runs the optional PostUp/PostDown hooks. The kernel module is included in Linux 5.6+; on older kernels, the DKMS module is a clean install but a maintenance liability.

Split-tunnel vs full-tunnel is a routing decision, not a WireGuard one. AllowedIPs = 0.0.0.0/0, ::/0 on the client makes everything go through the tunnel; AllowedIPs = 10.0.0.0/8, 192.168.0.0/16 sends only RFC1918 traffic through. The server reuses AllowedIPs for input filtering — a packet from a peer arriving with a source IP not listed in that peer’s AllowedIPs is dropped.

The procedure

  1. Generate the server key. umask 077 && wg genkey | tee /etc/wireguard/server.key | wg pubkey > /etc/wireguard/server.pub. Permissions matter — the private key must not be world-readable.
  2. Write the server config. /etc/wireguard/wg0.conf:
    [Interface]
    Address = 10.10.0.1/24, fd00:10:10::1/64
    ListenPort = 51820
    PrivateKey = <contents of server.key>
    PostUp = nft add table inet wgforward; nft add chain inet wgforward forward { type filter hook forward priority 0\; }
    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
  3. Enable IP forwarding. sudo sysctl -w net.ipv4.ip_forward=1 net.ipv6.conf.all.forwarding=1 and persist in /etc/sysctl.d/99-wireguard.conf.
  4. Bring up the tunnel. sudo systemctl enable --now wg-quick@wg0. sudo wg show should now list the interface with no peers.
  5. Generate per-peer keys, server-side. A script: for p in alice bob charlie; do umask 077; wg genkey | tee peers/$p.key | wg pubkey > peers/$p.pub; done. Each peer gets a .key (give to peer) and a .pub (add to server).
  6. Add each peer to the server config and reload. Per peer, append:
    [Peer]
    PublicKey = <contents of alice.pub>
    AllowedIPs = 10.10.0.10/32, fd00:10:10::10/128
    PresharedKey = <wg genpsk output, optional but recommended>
    then sudo wg syncconf wg0 <(wg-quick strip wg0). syncconf adds peers without dropping the tunnel.
  7. Hand the peer a config file. The matching client config:
    [Interface]
    PrivateKey = <alice.key contents>
    Address = 10.10.0.10/32, fd00:10:10::10/128
    DNS = 10.10.0.1
    
    [Peer]
    PublicKey = <server.pub contents>
    PresharedKey = <same PSK as server side>
    Endpoint = vpn.example.com:51820
    AllowedIPs = 10.10.0.0/24, fd00:10:10::/64    # split-tunnel; or 0.0.0.0/0, ::/0 for full
    PersistentKeepalive = 25
  8. Document key rotation. Per-peer keys can be rotated by generating a new keypair and replacing the PublicKey line on the server (and reloading). The peer keeps their old config until they receive the new one. A peer that loses their key file gets a new one — the old key continues to authenticate until you remove it from the server.

Common pitfalls

  • A client behind a NAT that closes idle UDP flows needs PersistentKeepalive = 25 or the tunnel goes silent and the next packet sees the keepalive failure. Server side, set the same on the peer block if the server initiates traffic.
  • AllowedIPs on the server doubles as an input ACL — sending a peer config with AllowedIPs = 10.10.0.10/32 and the peer using 10.10.0.11 results in dropped packets with no log entry. Match the addresses exactly.
  • DNS leaks: a full-tunnel client with DNS = 10.10.0.1 on Linux still uses systemd-resolved’s default per-interface DNS unless resolvconf is installed. Verify on each client OS — Linux, macOS, Windows, iOS, Android all behave differently.
  • WireGuard logs minimally by design — wg show shows handshake state and transferred bytes, that’s it. For per-flow logging, you need iptables/nftables LOG rules on the wg0 interface.
  • The MTU = 1420 default works on most paths but a low-MTU underlay (PPPoE, certain mobile carriers) needs MTU = 1300 or lower to prevent fragmentation black holes. Test with ping -M do -s 1372 <internal-host>.

In the engagements we run, WireGuard is the default operator VPN — paired with an SSH bastion behind it, the access pattern becomes “connect to WireGuard, then SSH to the box”. The peer-config issuance, key rotation, and offboarding live in Managed Operations — a VPN without an offboarding process is a backdoor with a TODO.