TodayTools

WireGuard VPN

How WireGuard works

A small, fast, modern VPN

Donate
L3NetworkPort 51820UDP
What brings you here?

Watch it happen

How much detail

A laptop at a cafe connects to the office through WireGuard and reaches the file server.

Kenji's laptop
Cafe router
Internet
Office VPN
File server
Wi-FiInternetCableEncrypted tunnelSetupDataRequestResponseControlEncrypted

4 steps, 7 packets

First up: Initiation. Press Play to watch it happen, one step at a time.

0 of 40/4

What is WireGuard?

WireGuard is a VPN: it builds an encrypted tunnel between two devices and carries whole IP packets through it. It gives your computer a new network interface, so any app can use the tunnel without knowing it exists.

It is deliberately small. Each side has a key pair, like SSH keys, and lists the public keys it trusts. There are no options to get wrong and no cipher menu: one fixed set of modern algorithms, and a handshake of just two packets.

Think of it like this. WireGuard is like a private pneumatic tube between your desk and the office. Anything you put in goes straight to the other end, sealed. The office end only opens capsules stamped with keys it already knows, and simply ignores anyone else knocking on the tube.

Step by step

Normal

4 steps

A laptop at a cafe connects to the office through WireGuard and reaches the file server.

  1. 1

    Initiation

    One packet from the laptop

    When the laptop first needs to send something through the tunnel, it sends a 148-byte Handshake Initiation. Its identity is encrypted inside, so the cafe network cannot see who is connecting.

  2. 2

    Response

    One packet back

    If the server knows the laptop's public key, it answers with a 92-byte Handshake Response. Both sides now hold fresh session keys. The whole handshake is one round trip.

  3. 3

    Encrypted data

    Whole IP packets inside UDP

    Each inner packet is encrypted with ChaCha20-Poly1305 and sent as a Transport Data message. The server decrypts it and passes it to the office network like any other packet.

  4. 4

    Keepalive

    A 32-byte empty packet

    With PersistentKeepalive = 25, the laptop sends an empty encrypted packet every 25 seconds when idle. It keeps the cafe router's NAT mapping open so the office can still reach the laptop. Keys are renewed with a new handshake about every 2 minutes.

Roaming

3 steps

The laptop leaves the cafe Wi-Fi for a phone hotspot, and the tunnel just keeps working.

  1. 1

    On cafe Wi-Fi

    Packets come from 198.51.100.40

    The tunnel is up and data flows. The server has the cafe address saved as the laptop's endpoint.

  2. 2

    Network changes

    Now from 203.0.113.200

    Kenji walks out and the laptop joins his phone hotspot. Its public address changes. The next encrypted packet simply arrives from the new address.

  3. 3

    Server follows

    Endpoint updated, no new handshake

    The packet decrypts with the right session key, so it must be from Kenji's laptop. The server updates the endpoint and replies to the new address. A packet that fails to decrypt can never move the endpoint.

Stranger knocks

3 steps

Someone without the right keys probes the VPN, and WireGuard stays completely silent.

  1. 1

    Random probe

    Junk on UDP 51820

    A scanner sends random bytes to see if anything answers. WireGuard sends nothing back, not even an error, so the port looks closed.

  2. 2

    Fake handshake

    Wrong mac1, dropped

    Without the server's public key, the scanner cannot make a valid mac1. The server checks that cheap field first and drops the packet before doing any heavy maths. Under heavy load it can also answer real peers with a cookie, which proves their address before any work is done.

  3. 3

    Known peer

    Kenji still gets in

    Kenji's laptop knows the right keys, so its handshake gets a reply as normal.

Myths and mix-ups

  • WireGuard is not an RFC standard; it is defined by its whitepaper and reference code.
  • A VPN does not make you anonymous; the VPN server sees your traffic leave.
  • WireGuard does not need a constant connection; it is quiet when nothing is sent, unless you turn on keepalive.
  • It does not run over TCP; blocked UDP means you need another way through.

Try it yourself

Run these on your own computer to see WireGuard at work.

  • See tunnel status

    sudo wg show

    Lists each peer, its endpoint address, the latest handshake time and bytes sent and received.

  • Make a key pair

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

    Keep private.key secret. Share only public.key with the other side.

  • Bring a tunnel up

    sudo wg-quick up wg0

    Reads /etc/wireguard/wg0.conf and creates the wg0 interface.

  • Test the tunnel

    ping 10.0.0.1

    Ping an address that only exists inside the tunnel. Then run wg show again to see the handshake time.

WireGuard at a glance

Layer
Network (3), a virtual interface
Default port
51820 (any UDP port works)
Transport
UDP only
Handshake
2 messages (Noise IK pattern)
Crypto
Curve25519, ChaCha20-Poly1305, BLAKE2s
Rekey
New handshake about every 2 minutes
Overhead
32 bytes plus outer UDP and IP headers
In Linux
Since kernel 5.6 (2020)

Defined inWireGuard whitepaper (no RFC; there is no IETF standard)Noise Protocol Framework (IKpsk2 pattern)

Header fields

What WireGuard puts in its messages, with example values from the stories above.

WireGuard

Type
1 (Handshake Initiation)
Reserved
0
Sender index
0x5c1a7e02
Ephemeral public key
32 bytes (Curve25519)A one-time key for this handshake only.
Encrypted static key
48 bytesThe laptop's long-term public key, encrypted so onlookers cannot tell who is connecting.
Encrypted timestamp
28 bytes (TAI64N)The server rejects any timestamp not newer than the last one, so a recorded copy cannot be replayed.
mac1
16 bytesKeyed with the server's public key. Senders that do not know it are dropped before any heavy maths.
mac2
zeros (no cookie)
Receiver index
0x5c1a7e02Matches the laptop's sender index, so it knows which handshake this answers.
Encrypted nothing
16 bytesAn empty message plus tag. Decrypting it proves the server holds the right private key.

Inner IPv4 + ICMP (after decryption)

Source
10.0.0.2
Destination
10.0.0.20
ICMP
Echo (ping) requestThis is the real packet. Only the two tunnel ends ever see it in the clear.

Test yourself

Question 1 of 5Score 0

How many packets does a WireGuard handshake take?

Questions people ask

What port does WireGuard use?

UDP 51820 is the common default, but you can set any UDP port with ListenPort in the config.

Is WireGuard better than OpenVPN or IPsec?

It is simpler and usually faster, with far less code to review. IPsec is still common for office to office links and in enterprise gear. All three can be secure when set up well.

Does WireGuard work behind NAT?

Yes. The side behind NAT starts the handshake, and PersistentKeepalive keeps the mapping open so the other side can reach it.

Is WireGuard an official standard?

No. There is no RFC. It is defined by its whitepaper and reference code, and it is part of the Linux kernel since version 5.6.

How do I check if my WireGuard tunnel is connected?

Run sudo wg show. If latest handshake shows a recent time and the transfer counters grow, the tunnel works.

Donate