WireGuard site-to-site on Ubuntu 22.04 with systemd: a working config
We needed two offices to talk to each other without exposing anything to the internet. Site A is 10.10.0.0/24 behind a router at 203.0.113.10, site B is 10.20.0.0/24 behind 198.51.100.20. Both edge machines are Ubuntu 22.04 with WireGuard 1.0.20210901 from apt. This is the config that ended up working after a fair bit of swearing.
## Install and keys
On each machine:
sudo apt update
sudo apt install -y wireguard
sudo mkdir -p /etc/wireguard && chmod 700 /etc/wireguard
cd /etc/wireguard
wg genkey | tee privatekey | wg pubkey > publickey
chmod 600 privatekey
Do not try to paste keys with line breaks. I did that once and wg-quick up accepted it, but the handshake never happened and wg show showed no peer at all.
## Site A config
/etc/wireguard/wg0.conf on the first box:
[Interface]
PrivateKey = <site A private key>
Address = 10.9.0.1/32
ListenPort = 51820
MTU = 1380
PostUp = sysctl -w net.ipv4.ip_forward=1
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT[Peer]
PublicKey = <site B public key>
Endpoint = 198.51.100.20:51820
AllowedIPs = 10.20.0.0/24, 10.9.0.2/32
PersistentKeepalive = 25
The PersistentKeepalive matters when one side is behind NAT, otherwise the mapping expires and the tunnel only works after a ping from the inside.
## Site B config
[Interface]
PrivateKey = <site B private key>
Address = 10.9.0.2/32
ListenPort = 51820
MTU = 1380
PostUp = sysctl -w net.ipv4.ip_forward=1
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT[Peer]
PublicKey = <site A public key>
Endpoint = 203.0.113.10:51820
AllowedIPs = 10.10.0.0/24, 10.9.0.1/32
PersistentKeepalive = 25
AllowedIPs is both a routing rule and an identity check. That second meaning is what got me. I had put 0.0.0.0/0 on the peer at site B because I wanted "all traffic" and could not be bothered listing subnets. WireGuard then refused to accept packets sourced from 10.10.0.0/24, because that range was not in the peer's allowed set, and the handshake looked like it was happening but nothing crossed.
## Starting it with systemd
sudo systemctl enable --now wg-quick@wg0
sudo wg show
A healthy output looks like this:
peer: <site B public key>
endpoint: 198.51.100.20:51820
allowed ips: 10.20.0.0/24, 10.9.0.2/32
latest handshake: 24 seconds ago
transfer: 184 KiB received, 212 KiB sent
If latest handshake never appears, check UDP 51820 first:
sudo ufw allow 51820/udp
I also hit this on one box after a kernel update:
$ sudo wg-quick up wg0
[#] ip link add wg0 type wireguard
RTNETLINK answers: Operation not supported
The wireguard kernel module had not loaded. sudo modprobe wireguard fixed it for that boot, and adding wireguard to /etc/modules-load.d/wireguard.conf fixed it for good.
## Routing the LANs
The tunnel being up is not enough; the LAN clients need a route. On each site's router, add 10.20.0.0/24 via 10.10.0.5 (or whatever the WireGuard box's LAN IP is) and the reverse on the other side. Without that, only the two edge boxes can reach each other. I forgot this part and spent twenty minutes convinced WireGuard was broken.
It is a small config and it has been stable for months. The two gotchas are AllowedIPs doing double duty and the module not loading after a reboot.