WireGuardLab
Flat isometric illustration of a keyhole node on a lit pad linked by a bright beam to a glowing hexagonal portal, evoking an encrypted WireGuard tunnel.
protocol-fundamentals

WireGuard Explained: Keys, AllowedIPs, and NAT Traversal

See how WireGuard peers exchange keys, how AllowedIPs routes requests and checks replies, and where to set keepalive when a peer is behind NAT.

By WireGuardLab Editorial · ·Updated · 6 min read

In WireGuard, each peer entry holds the other device’s public key. For outgoing packets, AllowedIPs selects a peer by destination. For incoming packets, it checks whether the authenticated peer may send from the packet’s source address. A keepalive from behind NAT keeps the return path open when the tunnel goes quiet.

Keys: no PKI, no accounts, two public keys

Each device keeps its own PrivateKey under [Interface]. In each [Peer] block, set PublicKey to the remote device’s public key. The server lists the client’s public key, and the client lists the server’s.

Run the wg(8) key-generation commands on each device in a directory where neither output file exists. The umask restricts permissions on newly created files. Share only public.key and keep private.key on the device that generated it.

umask 077
wg genkey | tee private.key | wg pubkey > public.key

The handshake and the timers around it

The WireGuard v1 protocol uses a handshake initiation and response to establish symmetric session keys. The configured public keys authenticate the peers. Rekeying replaces the session keys automatically.

  • Retry. If a handshake initiation goes unanswered, WireGuard retries after a timeout plus a small random delay. It eventually stops retrying and clears queued packets.
  • Rekey. After the tunnel is established, traffic can trigger a new handshake based on session-key age or the number of packets sent with that key. The age-based send trigger applies to the original initiator. WireGuard drops packets once the key is too old or the session counter has passed its limit.
  • Idle. An old handshake timestamp can be normal. With no traffic and no pending exchange, WireGuard has no periodic handshake schedule by default. Persistent keepalive can keep a NAT mapping open during an idle period. WireGuard Quick Start

Reading a config: what wg understands, what wg-quick adds

Here the server’s tunnel IP is 10.7.0.1 and the client’s is 10.7.0.2. These snippets show the peer settings on both devices. Each device already has its own tunnel address and private key configured.

On the reachable server, in the client’s peer block

[Peer]
PublicKey = <client public key>
AllowedIPs = 10.7.0.2/32

On the client behind NAT, in the server’s peer block

[Peer]
PublicKey = <server public key>
AllowedIPs = 10.7.0.1/32
Endpoint = <server hostname>:51820
PersistentKeepalive = 25

Replace the placeholders with the actual public keys and the server’s reachable hostname. This example assumes the server listens on UDP port 51820. Use its reachable port if it differs.

Both snippets use fields understood by wg. The OS still needs routes that send traffic for the peer through the WireGuard interface. For complete wg-quick files with interface addresses and routes, use the Ubuntu server and client setup.

AllowedIPs does two jobs at once

Assume each OS routes packets for the other tunnel address into WireGuard and the two peers have established a session. The outgoing lookup uses the inner destination IP. The receiving check uses the inner source IP after authentication.

PacketSender checks the destinationReceiver checks the source
Request from 10.7.0.2 to 10.7.0.1The client finds 10.7.0.1/32 in the server’s peer block and sends the packet to the server.The server accepts source 10.7.0.2 because the client’s block allows 10.7.0.2/32.
Reply from 10.7.0.1 to 10.7.0.2The server finds 10.7.0.2/32 in the client’s peer block and sends the reply to the client.The client accepts source 10.7.0.1 because the server’s block allows 10.7.0.1/32.
Client packet from 10.7.0.3 to 10.7.0.1The destination still selects the server.The server drops the packet. The client authenticated, but its block allows only source 10.7.0.2.

An OS route can deliver a packet to the WireGuard interface even when no peer matches the destination. In that case WireGuard cannot send it. For split-tunnel and full-tunnel prefixes, see the AllowedIPs configuration guide.

Working out AllowedIPs for a full mesh vs hub and spoke

For three nodes at 10.7.0.1, 10.7.0.2, and 10.7.0.3, start with the destinations each public key should carry.

Config being editedPeer blockAllowedIPsPath
Full mesh, Node 1Node 210.7.0.2/32Direct to Node 2
Full mesh, Node 1Node 310.7.0.3/32Direct to Node 3
Hub, Node 1Node 210.7.0.2/32One address per spoke
Hub, Node 1Node 310.7.0.3/32One address per spoke
Spoke, Node 2 or 3Hub, Node 110.7.0.0/24Other overlay nodes through the hub

The hub’s broader prefix on each spoke allows packets to and from the other spoke to pass via the hub. The hub needs forwarding and firewall permission for that transit traffic. Use the mesh designer to map peer entries and the site-to-site guide for routing between LANs.

NAT traversal, honestly

The client contacts the server at its configured Endpoint. The server learns the client’s outer IP address and UDP port from an authenticated packet and can send to that learned endpoint. WireGuard updates the endpoint when correctly authenticated traffic arrives from a new address or port.

Set PersistentKeepalive = 25 in the reachable server’s peer block on the client behind NAT, as shown above. The client sends authenticated empty packets to refresh its NAT or firewall mapping during idle periods. The upstream Quick Start suggests 25 seconds. Keepalive defaults to off.

If neither peer can receive the other’s initial UDP packets, keepalive cannot establish a direct path. Make one peer reachable with a UDP port forward where you control the router and its firewall. If neither side can expose a UDP listener, both can connect out to a reachable hub and route through it using the hub-and-spoke layout above.

MTU, the cause of “it connects but pages hang”

For packet-size problems after basic traffic passes, use the WireGuard MTU calculator.

WireGuard against OpenVPN and Tailscale

See the OpenVPN speed comparison for performance and WireGuard vs Tailscale for differences in peer discovery and relay options.

A troubleshooting order that saves time

Run wg show on both devices and find the entry for the other device’s public key. From the client, send a request to the server’s tunnel address with ping 10.7.0.1, then check wg show again on both devices. Ubuntu’s WireGuard troubleshooting guide

FieldWhat to check
latest handshakeIf it stays absent after the attempt, check the public-key pairing and whether the configured UDP endpoint is reachable. An older timestamp alone can reflect idle time.
endpointOn the client, check the server address and port. On the server, look for the client’s learned address and port after the client sends authenticated traffic.
transferCompare sent and received byte counts on both ends while sending traffic. If a handshake appears but requests get no reply, check the OS routes and both peers’ AllowedIPs against the request destination and reply source above.

Evidence and limits. The examples are based on the upstream wg(8) manual and WireGuard v1 protocol documentation. The commands and example tunnel were not run.

See also

Sources

  1. WireGuard wg(8) manual
  2. WireGuard v1 Protocol & Cryptography
  3. WireGuard Quick Start
  4. Ubuntu Server documentation: WireGuard on an internal system
  5. Ubuntu Server documentation: Troubleshooting WireGuard VPN

Related