19.5. Creating a WireGuard VPN
WireGuard is a VPN built around one idea: a peer is a public key. Every
interface has a key pair; every remote peer is listed with its public
key and the address ranges it may speak for. There are no certificates,
no handshake negotiation beyond the key exchange, and no separate
daemon - the implementation lives in the Linux kernel, and the
wireguard-tools package supplies the commands to configure it. The
three options a Debian administrator actually chooses among:
| VPN | Implementation | Identity | Typical Debian use |
|---|---|---|---|
| WireGuard | kernel module; UDP only; a few thousand lines of code | static Curve25519 key pairs, exchanged out of band | Networks you control: site links, remote access to your own gateway |
| OpenVPN | userspace daemon over OpenSSL; TCP or UDP | X.509 certificates with expiry and revocation | Connecting to existing services and commercial providers; Section 19.6 |
| IPsec (strongSwan) | kernel XFRM plus daemon; complex policy model | certificates or pre-shared keys; interoperates with enterprise gear | Interoperating with third-party gateways and enterprise requirements |
Building your own site-to-site or remote-access VPN is WireGuard's
home ground; joining someone else's network is usually a .ovpn
profile away. This section builds both of WireGuard's shapes - a
remote-access client and a site-to-site link - between the chapter's
hosts, with the administration network standing in for the untrusted
path between them.
19.5.1. Keys, Peers, Endpoints, and AllowedIPs
Install the tools on all three participants - rtrouter, rtpeer, and
rtclient - as john on each, with sudo apt install wireguard-tools.
The kernel side ships with Debian's kernel; nothing else is needed.
Generate the key pair in the workspace, on rtrouter as john:
$ wg genkey > wg-rtrouter.key
$ chmod 600 wg-rtrouter.key
$ wg pubkey < wg-rtrouter.key > wg-rtrouter.pub
All three commands are silent. wg genkey writes a private key to
stdout, the redirect stores it, chmod closes it to john alone, and
wg pubkey derives the public key from it. rtpeer and rtclient
generate their own pairs the same way (wg-rtpeer.*, wg-rtclient.*);
each host then needs the other hosts' public keys, which are copied
over the administration network with scp (Chapter 20
covers the login) or any out-of-band channel - the design assumption is
that public keys travel by some route you already trust, because the
configuration that follows places all its security in them. The
private key, by contrast, is a credential that never leaves its host;
its file mode is part of the procedure, as rtclient's shows - on
rtclient, as john:
$ ls -l wg-rtclient.key
-rw------- 1 john john 45 Nov 21 19:21 wg-rtclient.key
And the router's public key, the value its peers will configure - on rtrouter, as john:
$ cat wg-rtrouter.pub
UvZjldYteq3cULo4AEWp1f0YJY1cSiTzsnKVLqqGpic=
That value - the router's identity to its peers - was captured on
rtrouter; rtpeer's key begins a+MqTaEL... and rtclient's n2UB...,
and both appear, in full, inside the configurations below.
The one concept WireGuard adds to routing is cryptokey routing: each
peer's AllowedIPs list is simultaneously a route and a firewall rule.
Outbound, wg-quick installs a route for each AllowedIPs range through
the tunnel, so the kernel chooses the peer for those destinations.
Inbound, a decrypted packet whose source address is not inside that
peer's AllowedIPs is dropped - a peer simply cannot claim another
peer's addresses. AllowedIPs therefore must not overlap between two
peers of one interface, because the kernel cannot know which peer a
destination belongs to.
Figure 19.5. Conceptual view of the two tunnel shapes built in this section: a site-to-site pair where each side claims the other's networks, and a remote-access client whose single peer claims the networks the server publishes for it.
The commands this section uses:
wg / wg-quick form |
Description |
|---|---|
wg genkey / wg pubkey |
Write a private key to stdout / derive the matching public key. |
wg show [interface] |
Live state: keys, listen port, peers, endpoints, AllowedIPs, handshakes, counters. |
wg-quick up <name> |
Create the interface from /etc/wireguard/<name>.conf and apply routing and DNS. |
wg-quick down <name> |
Remove the interface, its routes, and its DNS entries. |
wg-quick strip <name> |
Print the config without the private key - safe to share. |
19.5.2. Configuring a Remote-Access Tunnel with wg-quick
A WireGuard configuration is an INI file. The interface half:
[Interface] key |
Meaning |
|---|---|
PrivateKey |
This side's private key, from the key pair just generated. |
Address |
The tunnel address; a /24 for a site router, a /32 for a single client. |
ListenPort |
Fixed UDP port for a server; omitted (ephemeral) for a client behind NAT. |
DNS |
Resolver passed to resolvconf when the tunnel comes up. |
MTU, Table, PostUp |
Sizing, routing-table choice, and hook commands; defaults are usually right. |
[Peer] key |
Meaning |
|---|---|
PublicKey |
The remote side's public key - its identity. |
AllowedIPs |
The ranges this peer may speak for: routes out, filter in. |
Endpoint |
Where to reach the peer, host:port; a roaming peer omits it and is learned from its first packet. |
PersistentKeepalive |
Seconds between keepalive packets; keeps NAT mappings from expiring. |
Build the router's configuration first, because the remote-access
client connects to it. Display it with the private key redacted -
sed replaces the secret line, which is also a safe way to review or
share a configuration - on rtrouter, as john:
$ sed "s/^PrivateKey = .*/PrivateKey = (redacted)/" wg0.conf
[Interface]
Address = 10.19.0.1/24
ListenPort = 51820
PrivateKey = (redacted)
[Peer]
PublicKey = a+MqTaELh2f7E/bcBPlMhS0ss5cA5VCxPC9gpUxYgzk=
AllowedIPs = 10.19.0.2/32, 198.51.100.0/24
Endpoint = 192.168.121.49:51820
PersistentKeepalive = 25
[Peer]
PublicKey = n2UByNRsSuLzhobm/AwyH6oV1u1f59kiRF3J86r8xkE=
AllowedIPs = 10.19.0.10/32
The file, as john creates it in the workspace, contains the real
private key value in place of (redacted). The interface owns the
tunnel network 10.19.0.0/24 from address 10.19.0.1, on fixed UDP
port 51820. Two peers: the first - with its own fixed endpoint at
rtpeer's administration address - is the site-to-site peer that
Section 19.5.4 brings up; until then it simply sits without a session,
which is harmless. The second is the remote-access client from this
subsection: its public key, a single /32 AllowedIPs (this one client,
nothing behind it), and deliberately no Endpoint - a road warrior
roams, so the router learns its current address from the client's
first packet.
Write the client's counterpart - on rtclient, as john, same redaction trick:
$ sed "s/^PrivateKey = .*/PrivateKey = (redacted)/" wg1.conf
[Interface]
Address = 10.19.0.10/32
PrivateKey = (redacted)
DNS = 192.168.220.10
[Peer]
PublicKey = UvZjldYteq3cULo4AEWp1f0YJY1cSiTzsnKVLqqGpic=
Endpoint = 192.168.121.117:51820
AllowedIPs = 10.19.0.0/24, 192.168.220.0/24
PersistentKeepalive = 25
The client's differences are all load-bearing: a /32 address (one
host, not a network), no listen port (its outbound packets open the
path), one peer with the router's endpoint, and AllowedIPs that claim
the whole tunnel network plus LAN B - the second range is what makes
this a remote-access service rather than a point-to-point link, and
the DNS line points at the resolver Section 19.5.3 activates. That
line also carries a package requirement: wg-quick registers the
resolver through the resolvconf command and stops the whole up if
the command is missing, so install it on rtclient, as john, with
sudo apt install resolvconf before bringing the tunnel up.
Both files go to /etc/wireguard/ with tight modes - the directory
private, the files unreadable beyond root. On rtrouter, as john:
$ sudo install -d -m 700 /etc/wireguard
$ sudo install -m 600 wg0.conf /etc/wireguard/wg0.conf
And on rtclient, as john, wg1.conf the same way:
$ sudo install -m 600 wg1.conf /etc/wireguard/wg1.conf
The file's basename is the interface name: wg0.conf makes wg0,
wg1.conf makes wg1. Bring the router's side up first, on rtrouter
as john:
$ sudo wg-quick up wg0
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.19.0.1/24 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] ip -4 route add 198.51.100.0/24 dev wg0
Every line prefixed [#] is a command wg-quick ran on the
configuration's behalf - creating the interface, applying the keys,
adding the address, setting the MTU (Section 19.5.5 explains the
1420), and one route per remote AllowedIPs range not already covered
by the interface address. Only 198.51.100.0/24 needed a route: the
/24 interface address already covers 10.19.0.2 and 10.19.0.10,
and the site peer's /32 claims add nothing new. Now the client, on
rtclient as john:
$ sudo wg-quick up wg1
[#] ip link add wg1 type wireguard
[#] wg setconf wg1 /dev/fd/63
[#] ip -4 address add 10.19.0.10/32 dev wg1
[#] ip link set mtu 1420 up dev wg1
[#] resolvconf -a tun.wg1 -m 0 -x
[#] ip -4 route add 192.168.220.0/24 dev wg1
[#] ip -4 route add 10.19.0.0/24 dev wg1
The client's /32 address makes the routing explicit - two
AllowedIPs entries, two routes - and the resolvconf line is the
DNS setting registering the tunnel's resolver through the package
installed above. The interface state, on
rtclient as john:
$ ip -br addr show wg1
wg1 UNKNOWN 10.19.0.10/32
UNKNOWN is normal operational state for a tunnel - there is no
carrier to detect. The session, on rtclient as john:
$ sudo wg show wg1
interface: wg1
public key: n2UByNRsSuLzhobm/AwyH6oV1u1f59kiRF3J86r8xkE=
private key: (hidden)
listening port: 60943
peer: UvZjldYteq3cULo4AEWp1f0YJY1cSiTzsnKVLqqGpic=
endpoint: 192.168.121.117:51820
allowed ips: 10.19.0.0/24, 192.168.220.0/24
latest handshake: 16 seconds ago
transfer: 652 B received, 660 B sent
persistent keepalive: every 25 seconds
The handshake happened - WireGuard's only signaling, performed on
first traffic and then every ~2 minutes - and both directions have
bytes. listening port: 60943 is the client's ephemeral source port;
the server side will remember the client as its endpoint
192.168.121.229:60943. A peer that shows no latest handshake has
no session, and Section 19.5.5 turns that observation into a
diagnosis procedure.
19.5.3. Routing, DNS, Forwarding, and Firewall Integration
The tunnel exists; now make it serve the networks behind the router. Routing is already half-done by AllowedIPs - on rtclient, as john:
$ ip route get 192.168.220.10
192.168.220.10 dev wg1 src 10.19.0.10 uid 1001
cache
LAN B is reached through the tunnel. The other half is the return
path: when the client pings 192.168.220.10, rtserver must be able to
send replies back to 10.19.0.10. That is exactly what the second
[Route] entry in rtserver's eth1 file (Section 19.1.2) provides -
10.19.0.0/24 via rtrouter's LAN B address - so the server returns
tunnel traffic to the router, and the router's AllowedIPs table
delivers it to the client. The alternative, used when the servers
behind a VPN gateway cannot be given return routes, is masquerading on
the router (Chapter 18 covers the nftables
mechanics): the gateway rewrites tunnel sources to its own LAN address,
trading visibility for zero server-side configuration.
The router itself must forward - the ip_forward sysctl from
Section 19.1.1 covers it, and that is the entire configuration.
DNS is a service the tunnel should carry, not leak around. rtserver
provides it - install the package there first, as john, with
sudo apt install dnsmasq - and give it a record for the exercise so
the tunnel's clients resolve through it. The drop-in, created in the
workspace and installed on rtserver:
# /etc/dnsmasq.d/ch19.conf
listen-address=192.168.220.10
bind-interfaces
no-resolv
server=192.168.121.1
address=/server.ch19.example/192.168.220.10
address= answers for the whole domain - any name under
server.ch19.example resolves to the LAN B address - and server=
forwards everything else to the upstream resolver, so the tunnel's
clients lose nothing. On rtserver, as john:
$ sudo install -m 0644 server-dnsmasq.conf /etc/dnsmasq.d/ch19.conf
$ sudo systemctl restart dnsmasq
Both silent; the restart makes dnsmasq re-read its drop-ins. Enable the
service so the record survives reboots, as john on rtserver, with
sudo systemctl enable dnsmasq - also silent, and Section 19.7.1
will hold it to that promise. On
rtclient, the DNS line already registered the resolver when wg1
came up, and the resolver file shows it - on rtclient as john:
$ cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 192.168.220.10
nameserver 192.168.121.1
The tunnel's resolver sits first; the local one remains as fallback. Resolve through it, on rtclient as john:
$ getent hosts server.ch19.example
192.168.220.10 server.ch19.example
And the full service path - name to address, address through the tunnel, reply back - on rtclient as john:
$ ping -c 2 192.168.220.10
PING 192.168.220.10 (192.168.220.10) 56(84) bytes of data.
64 bytes from 192.168.220.10: icmp_seq=1 ttl=63 time=0.894 ms
64 bytes from 192.168.220.10: icmp_seq=2 ttl=63 time=0.792 ms
--- 192.168.220.10 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1020ms
rtt min/avg/max/mdev = 0.792/0.843/0.894/0.051 ms
ttl=63: one router hop - the packet entered the tunnel, crossed the
router, and reached LAN B.
The firewall integration is best understood by watching the rule counts move. A WireGuard server needs two things from a filter: UDP port 51820 accepted inbound, and forwarding permitted between the tunnel and the served networks. Build a small observation table - accept-everything chains with counters, on rtrouter as john:
$ sudo nft add table inet ch19vpn
$ sudo nft "add chain inet ch19vpn input { type filter hook input priority 0; policy accept; }"
$ sudo nft "add chain inet ch19vpn forward { type filter hook forward priority 0; policy accept; }"
$ sudo nft add rule inet ch19vpn input udp dport 51820 counter accept
$ sudo nft add rule inet ch19vpn forward iifname wg0 counter accept
$ sudo nft add rule inet ch19vpn forward oifname wg0 counter accept
All six are silent. The quoting on the chain commands protects the braces from the shell; the rules count and accept the three movements a VPN gateway performs. Generate traffic - on rtclient, as john, the same ping:
$ ping -c 2 192.168.220.10
PING 192.168.220.10 (192.168.220.10) 56(84) bytes of data.
64 bytes from 192.168.220.10: icmp_seq=1 ttl=63 time=0.968 ms
64 bytes from 192.168.220.10: icmp_seq=2 ttl=63 time=0.818 ms
--- 192.168.220.10 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.818/0.893/0.968/0.075 ms
Then read the table with its counters, on rtrouter as john:
$ sudo nft list table inet ch19vpn
table inet ch19vpn {
chain input {
type filter hook input priority filter; policy accept;
udp dport 51820 counter packets 2 bytes 312 accept
}
chain forward {
type filter hook forward priority filter; policy accept;
iifname "wg0" counter packets 2 bytes 168 accept
oifname "wg0" counter packets 2 bytes 168 accept
}
}
Two packets hit each rule: the client's handshakes arrived on UDP
51820, the forwarded requests entered from wg0, and the replies
left through it. These are exactly the movements a production
WireGuard ruleset must permit - and the ones a forgotten forward
drop rule silently blocks. The table here is a demonstration, built
with accept policies so it observes without enforcing; a real
gateway's filtering belongs in /etc/nftables.conf with a drop
policy and explicit accepts, exactly as Chapter 18
built them. Remove the demonstration table, on rtrouter as john:
$ sudo nft delete table inet ch19vpn
Silent, and nothing about the tunnel changes - the rules were never doing anything but counting.
19.5.4. Site-to-Site Routes and Overlapping Address Plans
The site-to-site link connects two networks, not two hosts: each side claims the other's LAN in AllowedIPs, and the pair of routers become one routed fabric. rtpeer's configuration - display with the same redaction, on rtpeer as john:
$ sed "s/^PrivateKey = .*/PrivateKey = (redacted)/" wg0.conf
[Interface]
Address = 10.19.0.2/24
ListenPort = 51820
PrivateKey = (redacted)
[Peer]
PublicKey = UvZjldYteq3cULo4AEWp1f0YJY1cSiTzsnKVLqqGpic=
AllowedIPs = 10.19.0.1/32, 192.168.219.0/24
Endpoint = 192.168.121.117:51820
PersistentKeepalive = 25
Compare the two AllowedIPs lists and the design becomes visible:
rtrouter claims 10.19.0.2/32 and 198.51.100.0/24 for the peer;
rtpeer claims 10.19.0.1/32 and 192.168.219.0/24 for rtrouter.
The tunnel addresses are claimed as /32s - only the far router's
address, since the interface's own /24 already covers the rest -
and each side claims the other's LAN prefix. That is the whole
site-to-site routing table, installed by wg-quick as one route per
side, and it is also why the LAN claims must never overlap: two peers
claiming one prefix is the one arrangement cryptokey routing cannot
resolve.
Install and bring the peer up, on rtpeer as john:
$ sudo install -m 600 wg0.conf /etc/wireguard/wg0.conf
$ sudo wg-quick up wg0
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.19.0.2/24 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] ip -4 route add 192.168.219.0/24 dev wg0
One route this time - the interface's /24 covers the router's
tunnel address. Both sessions, from the router's point of view, on
rtrouter as john. The second peer was idle when this was captured -
wg1 was down - so it lists only its allowed ips; a peer with a live
session shows an endpoint, a handshake, and counters, as the site
peer does (and will, if wg1 from Section 19.5.2 is still up while
you read this):
$ sudo wg show
interface: wg0
public key: UvZjldYteq3cULo4AEWp1f0YJY1cSiTzsnKVLqqGpic=
private key: (hidden)
listening port: 51820
peer: a+MqTaELh2f7E/bcBPlMhS0ss5cA5VCxPC9gpUxYgzk=
endpoint: 192.168.121.49:51820
allowed ips: 10.19.0.2/32, 198.51.100.0/24
latest handshake: 6 seconds ago
transfer: 180 B received, 272 B sent
persistent keepalive: every 25 seconds
peer: n2UByNRsSuLzhobm/AwyH6oV1u1f59kiRF3J86r8xkE=
allowed ips: 10.19.0.10/32
The site peer has an endpoint, a fresh handshake, and transfer counters - everything a healthy session shows. Now prove the tunnel carries the network, not just the routers. The tunnel itself, on rtpeer as john:
$ ping -c 2 10.19.0.1
PING 10.19.0.1 (10.19.0.1) 56(84) bytes of data.
64 bytes from 10.19.0.1: icmp_seq=1 ttl=64 time=0.562 ms
64 bytes from 10.19.0.1: icmp_seq=2 ttl=64 time=0.558 ms
--- 10.19.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1030ms
rtt min/avg/max/mdev = 0.558/0.560/0.562/0.002 ms
And through it to LAN A, on rtpeer as john:
$ ping -c 2 192.168.219.10
PING 192.168.219.10 (192.168.219.10) 56(84) bytes of data.
64 bytes from 192.168.219.10: icmp_seq=1 ttl=63 time=0.986 ms
64 bytes from 192.168.219.10: icmp_seq=2 ttl=63 time=0.949 ms
--- 192.168.219.10 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.949/0.967/0.986/0.018 ms
ttl=63: rtpeer's packet entered the tunnel, rtrouter routed it onto
LAN A, and rtclient's reply followed the claimed AllowedIPs home -
rtrouter claims 192.168.219.0/24, so the reply addressed to
rtpeer's tunnel network is delivered through wg0. No static route was
written on either side; the AllowedIPs design is the routing.
Persist the site tunnel on both routers, as john on each:
$ sudo systemctl enable wg-quick@wg0
Created symlink '/etc/systemd/system/multi-user.target.wants/wg-quick@wg0.service' → '/usr/lib/systemd/system/wg-quick@.service'.
The instance name after @ is the interface, matching the config
filename. The client's wg1 is deliberately not enabled: a road
warrior comes and goes, and its tunnel belongs to the person who
brings the laptop up, not to the boot sequence - systemctl disable
wg-quick@wg1 (silent) makes sure a stray enable does not outlive the
decision.
Site-to-site links run into one planning problem often enough to
design for: overlapping address plans. Both sites in this chapter
use 192.168.0.0/16-style private space, and two companies that each
picked 192.168.1.0/24 in their first year cannot route a tunnel
between them - the same destination prefix is local on both sides,
and no route table can send one instance of a prefix into a tunnel
while keeping the other local. WireGuard cannot fix this; nothing can,
because the packets are indistinguishable. Three real options, in
order of preference:
- Renumber one site. With systemd-networkd the change is a
series of
.networkedits and a maintenance window; it is the only option that leaves a clean, debuggable network. - NAT one side behind the tunnel. The router translates the colliding range to a documentation prefix before it enters the tunnel, with nftables' one-to-one prefix mapping. Illustrative - this mapping is not part of the working configuration the chapter builds:
bash
# concept: translate site B's 192.168.1.0/24 to 203.0.113.0/24
# inside the tunnel, one address to one address, in both directions
nft add rule inet nat postrouting oifname "wg0" \
snat ip prefix to ip saddr map { 192.168.1.0/24 : 203.0.113.0/24 }
nft add rule inet nat prerouting iifname "wg0" \
dnat ip prefix to ip daddr map { 203.0.113.0/24 : 192.168.1.0/24 }
The price is permanent: the far side can only reach the
translated addresses, logs show the wrong source, and per-host
services behind the NAT need one rule each.
3. Narrow the collision. If site A uses 192.168.1.0/24 and site
B uses 192.168.0.0/16 somewhere, splitting site B's allocation
into /25s that avoid the collision can buy years - with the same
renumbering debt accruing interest.
The lab avoided the problem by design: every network in Figure 19.1
has a distinct prefix, and the tunnel networks 10.19.0.0/24 and
10.19.1.0/24 were reserved before anything used them. That is the
actual lesson - address plans are chosen, not inherited, and a
site-to-site design starts by listing every prefix on both sides.
19.5.5. Handshake, MTU, Roaming, and NAT Troubleshooting
Four things go wrong with WireGuard in practice, and each has a signature.
No handshake. wg show lists the peer with its allowed ips but
no latest handshake, and pings vanish. Work outward: verify the
interface is up (wg show names it), verify the endpoint is
reachable (ping the endpoint address - the underlay must work
before the tunnel can), verify the keys match (each side's PublicKey
must be the other side's wg genkey output - a single wrong
character is silent), and check that UDP to the fixed port is not
filtered on the path (ss -ulnp | grep 51820 on the server shows the
socket; a blocked port on the far side is invisible from here). The
handshake also only happens on traffic or keepalives - two idle
configured peers show no handshake until something is sent.
MTU. The tunnel interface inherits a reduced MTU, visible on rtrouter as john:
$ sudo ip link show wg0
8: wg0: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1420 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/none
WireGuard adds 60 bytes of overhead to an IPv4 packet - 20 IP + 8 UDP
+ 32 for the encrypted inner packet - and wg-quick sizes the tunnel
for the worst case, IPv6, at 80 bytes: it takes the underlay's MTU
and subtracts 80, giving 1420 on a 1500-byte link. link/none and
POINTOPOINT are the tunnel's nature: no MAC layer, one peer per
destination route. Find the working payload size empirically, on
rtpeer as john - -M do forbids fragmentation, -s sets the payload:
$ ping -M do -s 1392 -c 2 10.19.0.1
PING 10.19.0.1 (10.19.0.1) 1392(1420) bytes of data.
1400 bytes from 10.19.0.1: icmp_seq=1 ttl=64 time=0.819 ms
1400 bytes from 10.19.0.1: icmp_seq=2 ttl=64 time=0.690 ms
--- 10.19.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.690/0.754/0.819/0.064 ms
1392 bytes of payload plus the 28-byte IPv4+ICMP header is exactly the 1420 tunnel MTU: it passes. One byte more:
$ ping -M do -s 1393 -c 2 10.19.0.1
PING 10.19.0.1 (10.19.0.1) 1393(1421) bytes of data.
--- 10.19.0.1 ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1002ms
ping: sendmsg: Message too long
ping: sendmsg: Message too long
Message too long is the kernel refusing locally - the packet
exceeds wg0's MTU and DF forbids splitting it. This failure mode
explains the classic WireGuard symptom set: small packets (handshake,
ping, SSH keystrokes) work, large ones (HTTPS transfers stalling at
some byte count, package downloads) hang - the inner packet silently
or repeatedly needs one byte more than the tunnel allows. The cure
is MTU = 1380 or similar in the [Interface] section when the
underlay is itself reduced (PPPoE, another tunnel, some clouds), and
-M do sweeps like the one above are the measurement.
Roaming and NAT. The client in this chapter had no ListenPort
and no fixed endpoint on the server's side - that is the roaming
design. The server learns the client's current source address from
its first authenticated packet and updates the endpoint
automatically, so the client may change networks (wireless to LTE to
a hotel) and the tunnel follows with no configuration. NAT
cooperation is one line: a NAT middlebox forgets an outbound mapping
after a timeout, and a server behind one cannot send the first packet
- so the side behind the NAT sets PersistentKeepalive = 25 to keep
the mapping warm. Both configurations here carry it; a peer with a
stable public address does not need it.
The tunnel is down. The definitive test is the peer's state. Bring rtpeer's side down, on rtpeer as john:
$ sudo wg-quick down wg0
[#] ip link delete dev wg0
The router's packets now have nowhere to go, on rtrouter as john:
$ ping -c 2 -W 1 10.19.0.2
PING 10.19.0.2 (10.19.0.2) 56(84) bytes of data.
--- 10.19.0.2 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1017ms
WireGuard drops rather than errors - a tunnel with no peer is indistinguishable from a dead cable, only quieter. Bring it back, on rtpeer as john:
$ sudo wg-quick up wg0
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.19.0.2/24 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] ip -4 route add 192.168.219.0/24 dev wg0
And the path returns, on rtrouter as john:
$ ping -c 2 10.19.0.2
PING 10.19.0.2 (10.19.0.2) 56(84) bytes of data.
64 bytes from 10.19.0.2: icmp_seq=1 ttl=64 time=0.526 ms
64 bytes from 10.19.0.2: icmp_seq=2 ttl=64 time=0.728 ms
--- 10.19.0.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1031ms
rtt min/avg/max/mdev = 0.526/0.627/0.728/0.101 ms
The keepalive traffic re-establishes the session within seconds -
which is the last diagnostic worth naming: a tunnel that recovers by
itself after minutes of silence was never broken, it was idle, and
PersistentKeepalive plus traffic on demand is the whole difference.