Sub topic 11
WireGuard: a minimal modern VPN
This chapter assumes you know IP subnets, routing tables, the difference between TCP and UDP, and basic Linux command-line use. It also assumes you have read the chapters on encapsulation and key exchange.
What WireGuard is
WireGuard is a VPN that runs as a network interface, like the GRE tunnel you saw earlier. Packets sent into the interface are encrypted and wrapped in UDP datagrams. The peer at the other end decrypts them and delivers the inner packet. It has a small codebase and offers no choice of ciphers, so there is little to misconfigure.
Each peer is identified by a public key. There are no certificates, no user database and no negotiation of algorithms. Two peers that hold each other's public keys can talk.
Keys
Each peer generates a private key and derives a public key from it. You share only the public key.
umask 077
wg genkey | tee privatekey | wg pubkey > publickeyThe umask 077 line makes the private key file readable only by you.
A client configuration
The configuration has an [Interface] section for this host and one [Peer] section per remote peer.
[Interface]
PrivateKey = <client-private-key>
Address = 10.8.0.2/24
DNS = 10.8.0.1
[Peer]
PublicKey = <server-public-key>
Endpoint = vpn.example.com:51820
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 25Addressis the inner address of the tunnel interface.Endpointis the outer address and UDP port of the peer. The client needs it to send the first packet. A server can leave it out for clients that connect from changing addresses, and it learns the endpoint from authenticated packets.PersistentKeepalive = 25sends a small packet every 25 seconds. This keeps the NAT mapping open when the client sits behind NAT.
AllowedIPs does two jobs
AllowedIPs works as a routing table and as an access list at the same time.
- Outgoing: a packet is sent to the peer whose
AllowedIPscontains the destination address. - Incoming: a decrypted packet is accepted only if its source address falls inside that peer's
AllowedIPs.
This is called cryptokey routing. It ties each inner address range to one public key, so a peer cannot spoof addresses that belong to another peer.
Split and full tunnel
With AllowedIPs = 10.8.0.0/24, only traffic for the VPN subnet enters the tunnel. This is a split tunnel. Everything else uses the normal default route.
Changing the peer section to the default routes sends all traffic through the VPN:
[Peer]
PublicKey = <server-public-key>
Endpoint = vpn.example.com:51820
AllowedIPs = 0.0.0.0/0, ::/0This is a full tunnel. The wg-quick tool adds the routes for you. It also keeps the route to the Endpoint outside the tunnel, so the encrypted packets do not try to travel through themselves.
Bringing it up and checking it
Save the file as /etc/wireguard/wg0.conf, then run sudo wg-quick up wg0. Use sudo wg show to inspect the state.
The most useful field is latest handshake. If a peer shows no handshake, the peers have not completed a cryptographic exchange. Check the keys, the endpoint, the UDP port in the firewall and NAT. If a handshake is recent but traffic fails, check AllowedIPs, routes and MTU.
Common mistakes
| Symptom | Likely cause |
|---|---|
| No handshake | Wrong public key, blocked UDP port, wrong endpoint |
| Handshake works, no traffic | `AllowedIPs` too narrow, missing route, missing IP forwarding on the server |
| Small packets work, large ones hang | MTU too high for the path |
| Connection drops when idle behind NAT | Missing `PersistentKeepalive` |
Exercise
A client has a peer with AllowedIPs = 10.8.0.0/24. A user opens a web page at 203.0.113.9. Does that traffic go through the tunnel, and why? What single change would send it through?
Show answer
No. 203.0.113.9 is not inside 10.8.0.0/24, so no peer matches it and it follows the normal default route. Adding 0.0.0.0/0 to the peer's AllowedIPs (and ::/0 for IPv6) makes it a full tunnel.