Prerequisites
- A Linux server reachable from the Internet (or a UDP port forward)
- The wireguard-tools package installed
- Root access
01The principle
WireGuard identifies each peer by a key pair, like SSH. No certificate, no password: a peer is only accepted if its public key is in the configuration, and only the addresses listed in AllowedIPs may pass through it. This is “cryptokey routing”: AllowedIPs acts both as the outbound routing table and as the inbound filter.
Without the right key, the server simply doesn’t answer: seen from outside, the UDP port looks closed.
02Generate the keys
Do this on the server, then on each device. umask 077 makes the created files readable by their owner only. The private key never leaves the machine that generated it; only the public key is exchanged.
umask 077
wg genkey | tee privatekey | wg pubkey > publickey03Configure the server
Each device gets a /32 address in the tunnel: that is what identifies it on the server side.
For the device to also reach the local network behind the server, two things are needed: IP forwarding on the server, and the local machines knowing how to answer the tunnel network. The simplest is address translation (masquerade) on the server’s way out; the cleanest, a static route to 10.8.0.0/24 on the local network’s gateway.
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <server private key>
# LAN replies come back to the server (otherwise: static route on the gateway)
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
# Laptop
PublicKey = <laptop public key>
AllowedIPs = 10.8.0.2/3204Enable IP forwarding
echo 'net.ipv4.ip_forward = 1' | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl -p /etc/sysctl.d/99-wireguard.conf05Configure the device
On the device, AllowedIPs decides what goes into the tunnel: here, only the tunnel and the home network (split tunneling). With 0.0.0.0/0, all traffic would. DNS pushes the internal resolver while the tunnel is up. PersistentKeepalive keeps the tunnel open behind NAT.
For a phone, the configuration is transferred as a QR code, generated on the server and then deleted: it contains the device’s private key.
[Interface]
Address = 10.8.0.2/32
PrivateKey = <laptop private key>
DNS = 192.168.10.53
[Peer]
PublicKey = <server public key>
Endpoint = vpn.example.com:51820
AllowedIPs = 10.8.0.0/24, 192.168.10.0/24
PersistentKeepalive = 2506Start and verify
If “latest handshake” doesn’t show up: check that the UDP port is open or forwarded, that the Endpoint is right, and that each public key really is the other peer’s. If the handshake is there but the local network doesn’t answer, it’s IP forwarding or the return path.
systemctl enable --now wg-quick@wg0 # start now and at boot
wg show # latest handshake, bytes exchanged
qrencode -t ansiutf8 < phone.conf # QR code for the mobile appKey takeaways
- One key pair per device; the private key never travels.
- AllowedIPs: who is who on the server, what goes into the tunnel on the device.
- IP forwarding + a return path (masquerade or static route) to reach the LAN.
- A single UDP port, silent without the right key.
- wg show: the latest handshake tells you everything.
Example domain and addresses. Replace eth0 with the server’s network interface.