Course contentsShow
← Security
Lesson 494 of 3,05211. Network Security - VPNs and TunnelingFree lesson

WireGuard Peer Configuration and Key Management

Configuring WireGuard peers, generating and exchanging public keys, setting allowed IPs, and understanding the stateless connection model.

WireGuard Peer Configuration and Key Management

You'll learn how to set up WireGuard connections by configuring peers, managing cryptographic keys, and defining which traffic flows through the tunnel.

The Core Concept

Unlike traditional VPNs with client/server hierarchies, WireGuard treats all endpoints as peers on equal footing. Each peer has a public/private key pair, and you configure which peer can talk to whom by explicitly mapping public keys to allowed IP addresses. This stateless design means no handshake negotiations or connection establishment—peers simply encrypt and send packets when needed.

Think of WireGuard like a postal system where each address (peer) has a unique mailbox key. You tell your mailbox: "Accept mail only from these specific keys, and only if it claims to come from these return addresses (allowed IPs)."

Key Generation and Exchange

First, generate a key pair for each peer:

# Generate private key
wg genkey > private.key

# Derive public key from private key
wg pubkey < private.key > public.key

Exchange public keys securely with your peers (never share private keys). Each peer's configuration includes their own private key and other peers' public keys.

Peer Configuration Structure

Each peer configuration specifies:

  • PublicKey: The other peer's public key (for authentication)
  • AllowedIPs: IP ranges this peer can send from and that will route to them
  • Endpoint: Where to send packets (IP:port), optional for roaming peers

The AllowedIPs field serves dual purposes: it acts as both an access control list (only accept packets from these source IPs) and a routing table (send packets to these destinations through this peer).

Stateless Connection Model

WireGuard has no "connected" state. Peers silently accept authenticated packets and create ephemeral session keys automatically when needed. This eliminates connection timeouts and complex state machines that plague older VPN protocols.

Key Takeaway: WireGuard peer configuration revolves around cryptographic key pairs and explicit IP whitelisting—no sessions to manage, just public keys that define trust relationships and allowed IPs that define routing.