$ run as a normal user# run as root (or with sudo)
The quick answer
Step by step
- Plan the tunnel: a small private network only for the tunnel,
10.8.0.0/24— server A (public IP203.0.113.10) gets10.8.0.1, server B gets10.8.0.2. Only A needs an open UDP port (51820); B connects out to it. - The configs — each file has its own private key and the other side's public key:
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <A private key>
[Peer]
PublicKey = <B public key>
AllowedIPs = 10.8.0.2/32[Interface]
Address = 10.8.0.2/24
PrivateKey = <B private key>
[Peer]
PublicKey = <A public key>
Endpoint = 203.0.113.10:51820
AllowedIPs = 10.8.0.1/32
PersistentKeepalive = 25chmod 600 /etc/wireguard/wg0.confAllowedIPs is both the routing and the access list: only these source addresses are accepted from that peer. PersistentKeepalive keeps the tunnel open through NAT.
- Open the port on A (and in the cloud security group):
firewall-cmd --permanent --add-port=51820/udp && firewall-cmd --reload- Start at boot on both:
systemctl enable --now wg-quick@wg0- Reach a whole network behind the other side (for example the office LAN
192.168.10.0/24behind B): add it to B's entry on A (AllowedIPs = 10.8.0.2/32, 192.168.10.0/24), turn on forwarding on B (net.ipv4.ip_forward = 1), and route/NAT there. - Use the tunnel: let the database listen on, or allow only, the tunnel IP — for example PostgreSQL on A allows
10.8.0.2/32inpg_hba.conf, and the firewall opens 5432 only onwg0.
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
No latest handshake | UDP 51820 blocked, wrong endpoint, or wrong public key. | Open the port (host + cloud); check Endpoint and keys. |
| Handshake OK, no ping | AllowedIPs does not include the address, or a firewall on wg0. | Fix AllowedIPs on both sides; allow traffic on the wg0 zone/interface. |
| Tunnel drops after idle time | NAT forgets the mapping. | PersistentKeepalive = 25 on the side behind NAT. |
| Network behind B not reachable | No forwarding or return route. | ip_forward=1 on B; route back to 10.8.0.0/24 in the office network. |
RTNETLINK answers: Operation not supported | Kernel module missing (old kernel / container). | Update the kernel; WireGuard is built in since Linux 5.6. |
| Config readable by others | Private key leaked risk. | chmod 600; rotate keys if it leaked. |
Go further
- Command pages: wg, ip, firewall-cmd, ping.
- Next how-tos: Add a network route, Open a port in the firewall, Test if a remote port is open, Set up an OpenVPN server for remote users.
- Full guide: WireGuard server and clients. Other VPN: OpenVPN. Learn it: CKS — WireGuard between nodes.
1What does AllowedIPs do in WireGuard?
It is the routing for that peer and its access list: only these source addresses are accepted from it.
2The handshake works but ping fails. What do you check?
AllowedIPs on both sides and the firewall rules on the wg0 interface.