Sub topic 11
WireGuard: a minimal modern VPN
This chapter assumes you know IP addressing and subnets, how a routing table and default route work, the difference between TCP and UDP, and basic Linux command-line use. It also assumes you have read the earlier chapters on encapsulation, authentication and encryption, and key exchange.
What WireGuard is
WireGuard is a VPN that runs inside the Linux kernel (and has implementations for other operating systems). It creates a network interface, usually named wg0. Packets routed to that interface are encrypted and sent inside UDP datagrams to a peer. Packets arriving from the peer are decrypted and delivered to the local network stack.
Compared with IPsec or OpenVPN, WireGuard is deliberately small:
- It uses one fixed set of algorithms: Curve25519 for key exchange, ChaCha20-Poly1305 for encryption and integrity, BLAKE2s for hashing. There is no cipher negotiation, so there is no downgrade attack.
- It has no separate control daemon and no certificate authority. Each peer is identified by a public key.
- It uses UDP only, on a port you choose. The conventional port is 51820.
- It stays silent toward unauthenticated packets. A scan of the UDP port gets no reply.
The trade-off is flexibility. WireGuard does not hand out addresses, push routes, or authenticate users with passwords or certificates. You configure those things yourself or with other tools.
The cryptokey routing model
The central idea in WireGuard is cryptokey routing. Each peer has a public key and a list called AllowedIPs. That list does two jobs:
- Outgoing: it works as a routing table. When the kernel hands WireGuard a packet, WireGuard picks the peer whose
AllowedIPscontains the destination address, then encrypts the packet for that peer. The most specific prefix wins. - Incoming: it works as a source filter. After a packet is decrypted and authenticated, WireGuard checks that the inner source address falls inside that peer's
AllowedIPs. If not, it drops the packet.
The second job matters for security. Even a valid peer cannot send traffic that claims to come from another peer's addresses.
Keys
Each side generates a private key and derives a public key from it. Peers exchange only public keys, through any channel you trust to deliver them unmodified.
umask 077
wg genkey | tee privatekey | wg pubkey > publickeyThe umask 077 makes the new files readable only by you. The private key never leaves its host. Anyone who obtains it can impersonate that peer.
WireGuard also supports an optional pre-shared key, a symmetric secret added to the handshake. It is a defence in depth measure against a future attacker with a quantum computer. Public-key exchange alone would not stop such an attacker from decrypting recorded traffic. Generate one with wg genpsk.
A client configuration
The client file normally lives at /etc/wireguard/wg0.conf. Here is a client that reaches only the VPN subnet:
[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 = 25Each field does the following:
| Field | Section | Meaning |
|---|---|---|
| `PrivateKey` | Interface | This host's private key. |
| `Address` | Interface | The address assigned to `wg0`. Used by `wg-quick`, not by the kernel module itself. |
| `DNS` | Interface | Resolver to use while the tunnel is up. Also handled by `wg-quick`. |
| `PublicKey` | Peer | The peer's public key, which identifies it. |
| `Endpoint` | Peer | Where to send UDP packets. Optional for a peer that only receives connections. |
| `AllowedIPs` | Peer | Destinations routed to this peer, and sources accepted from it. |
| `PersistentKeepalive` | Peer | Seconds between empty keepalive packets. |
Because AllowedIPs lists only 10.8.0.0/24, this is a split tunnel. Only traffic for that subnet goes through the VPN. Everything else uses the normal default route.
To bring the interface up and down, use wg-quick:
sudo wg-quick up wg0
sudo wg show
sudo wg-quick down wg0wg show prints each peer, its endpoint, its most recent handshake time and its byte counters. Those fields are your first diagnostic tool.
The server side
The server mirrors the client. It lists the client's public key and the single address that client may use:
[Interface]
PrivateKey = <server-private-key>
Address = 10.8.0.1/24
ListenPort = 51820
[Peer]
PublicKey = <client-public-key>
AllowedIPs = 10.8.0.2/32The server's AllowedIPs for the client is a /32, so the server will accept from this peer only packets with source 10.8.0.2. Add one [Peer] block per client, each with its own /32. Note that the server has no Endpoint for the client. It learns the client's address from the most recent authenticated packet, which also lets the client roam between networks.
If the server must forward client traffic to other networks, enable IP forwarding (sysctl -w net.ipv4.ip_forward=1) and allow the traffic in the firewall.
Full tunnel
Changing AllowedIPs in the client's peer section sends everything through the VPN:
[Peer]
PublicKey = <server-public-key>
Endpoint = vpn.example.com:51820
AllowedIPs = 0.0.0.0/0, ::/0When wg-quick sees a default route in AllowedIPs, it avoids a routing loop. The tunnel's own UDP packets to the Endpoint must not be sent into the tunnel. wg-quick handles this with a firewall mark and a policy routing rule, so marked packets use the normal routing table. If you build the setup by hand with ip route, you must create that exception yourself. A full tunnel also needs a server that does NAT or routing for the client's traffic, and a DNS setting that points at a resolver reachable through the tunnel. Split and full tunnelling are covered in more detail in a later chapter.
Handshake and sessions
WireGuard is connectionless from the user's point of view, but it keeps cryptographic session state.
- The first packet for a peer triggers a handshake, based on the Noise IK pattern. It takes one round trip: an initiation message and a response.
- Both sides derive fresh symmetric session keys. Each side proves possession of its private key.
- Data packets flow, protected by those keys and numbered with a counter so replays are rejected.
- After about two minutes the keys are replaced by a new handshake, which gives forward secrecy. If an attacker later steals a long-term private key, recorded old traffic stays unreadable.
If a peer is idle, there are no packets and no handshake. This is why wg show may report "latest handshake" as never, or minutes ago, on a quiet link. Idle is not the same as broken.
NAT and keepalives
A client behind NAT sends a packet, and the NAT device creates a temporary mapping so replies can return. The mapping expires after a short idle period, often 30 to 120 seconds for UDP. Once it expires, the server cannot reach the client until the client sends something again.
PersistentKeepalive = 25 makes the client send an empty authenticated packet every 25 seconds, which keeps the mapping open. Set it on the peer that sits behind NAT and needs to receive unsolicited traffic. Do not set it on every peer by habit. It generates constant traffic and defeats the quiet behaviour on mobile devices.
MTU
WireGuard adds overhead: an outer IP header (20 bytes for IPv4, 40 for IPv6), a UDP header (8 bytes), and the WireGuard header and authentication tag (32 bytes). That is 60 bytes over IPv4 and 80 over IPv6. On a 1500-byte link, the safe tunnel MTU is 1420, which is also the default wg-quick picks. If your path has a smaller MTU, for example on PPPoE or inside another tunnel, lower it with MTU = 1380 in the [Interface] section.
Test a path with the Don't Fragment bit set:
ping -M do -s 1372 10.8.0.1The payload of 1372 bytes plus 28 bytes of ICMP and IP headers makes a 1400-byte packet. If small pings work and this one fails, you have an MTU black hole. Reduce the size until it passes, then set the interface MTU accordingly.
Common errors
- No handshake in
wg show. Check that the server's UDP port is open in every firewall and security group, and that theEndpointis correct. Check that each side has the other's correct public key. A wrong key produces no error message, only silence. - Handshake succeeds, but no traffic. Check
AllowedIPson both sides and the routing table withip route get <destination>. Check IP forwarding and firewall rules on the server. - Small packets work, large transfers stall. This is an MTU problem. See the test above.
- Works for a while, then stops until the client sends traffic. NAT mapping expiry. Add
PersistentKeepalive. - Two peers overlap in
AllowedIPs. Only one peer can own a given prefix. The latest configuration wins and the other peer loses that traffic.
Exercise
A client config has AllowedIPs = 10.8.0.0/24 and the client can reach 10.8.0.1, but its web browser still shows the home ISP's public IP address. Why, and what single change would send web traffic through the VPN?
Show answer
Only destinations inside 10.8.0.0/24 are routed to the WireGuard peer, so this is a split tunnel. Internet traffic uses the normal default route and leaves through the ISP. Changing AllowedIPs to 0.0.0.0/0, ::/0 makes the tunnel a full tunnel. The server must also forward and NAT the traffic, and DNS should point at a resolver reachable through the tunnel.
Exercise
On the server, why is the client's AllowedIPs written as 10.8.0.2/32 rather than 10.8.0.0/24?
Show answer
On the server, AllowedIPs routes outgoing packets to that peer and filters incoming ones by source address. A /32 means the server sends only traffic for 10.8.0.2 to this peer and accepts only packets whose inner source is 10.8.0.2. A /24 would let that client spoof other addresses in the subnet and would conflict with the other clients' entries.