On this page
What you need
- A VPS with a public IPv4 and root over SSH. A server at home works too if its router has a public address.
- Ubuntu 20.04 or newer, Debian 11 or newer. WireGuard is in their kernel, nothing is compiled. Memory hardly matters.
- A virtual machine (KVM), not a container. On OpenVZ and many LXC plans the kernel is the host's, and WireGuard may not be in it.
- A network that lets UDP through. WireGuard has no TCP mode and no disguise: where VPNs are filtered, it is among the first to be cut.
Install and make the keys
Log in as root. The private keys must not leave the server, except the device's own one, which goes into its file.
apt update &&
apt install -y wireguard iptables qrencodeumask 077
cd /etc/wireguard
wg genkey | tee server.key | wg pubkey > server.pub
wg genkey | tee phone.key | wg pubkey > phone.pubOne pair per device: the same key on two devices at once makes them throw each other off.
echo net.ipv4.ip_forward=1 > /etc/sysctl.d/99-wireguard.conf
sysctl --systemip route show defaultThe word after dev, for example eth0 or ens3. Put it in place of eth0 below.
The server file wg0.conf
The file is /etc/wireguard/wg0.conf. SERVER_PRIVATE_KEY is what's in server.key, PHONE_PUBLIC_KEY what's in phone.pub. PostUp turns on NAT, without which devices connect but have no internet.
[Interface]
PrivateKey = SERVER_PRIVATE_KEY
Address = 10.8.0.1/24
ListenPort = 51820
PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
[Peer]
PublicKey = PHONE_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32The device file phone.conf
PHONE_PRIVATE_KEY is phone.key, SERVER_PUBLIC_KEY is server.pub, IP is the server's address. ::/0 sends IPv6 into the tunnel as well: the server has none there, so it stops instead of going past the VPN.
[Interface]
PrivateKey = PHONE_PRIVATE_KEY
Address = 10.8.0.2/32
DNS = 1.1.1.1
[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = IP:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25Start it and show the QR code
systemctl enable --now wg-quick@wg0
wg showqrencode -t ansiutf8 < phone.confScan it in the WireGuard app or any client that reads its files. Then delete phone.conf and phone.key from the server: it only needs the public key.
wg genkey | tee laptop.key | wg pubkey > laptop.pubThen add a [Peer] with the new public key and 10.8.0.3/32 to wg0.conf.
wg syncconf wg0 <(wg-quick strip wg0)Connected devices don't drop.
ufw: which rules WireGuard needs
Two: let the port in and let traffic through from the tunnel to the internet. The first alone gives a handshake without internet. Allow SSH before you turn ufw on, or you lock yourself out.
ufw allow OpenSSH
ufw allow 51820/udp
ufw route allow in on wg0 out on eth0
ufw enable
ufw status verboseIf SSH isn't on port 22, allow that port instead of OpenSSH. NAT stays in PostUp.
nftables instead of iptables
If you keep the rules in nftables, delete the PostUp and PostDown lines from wg0.conf and put this in /etc/nftables.conf. flush ruleset clears everything that was there, Docker's and Fail2ban's rules too, so this is for a server with nothing else on it. Check that the SSH port is yours.
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority filter; policy drop;
ct state established,related accept
iif "lo" accept
meta l4proto { icmp, ipv6-icmp } accept
tcp dport 22 accept
udp dport 51820 accept
}
chain forward {
type filter hook forward priority filter; policy drop;
ct state established,related accept
iifname "wg0" oifname "eth0" accept
}
}
table ip nat {
chain postrouting {
type nat hook postrouting priority srcnat;
ip saddr 10.8.0.0/24 oifname "eth0" masquerade
}
}nft -c -f /etc/nftables.conf
systemctl enable --now nftables
systemctl restart nftablesThe first line only checks the syntax and prints nothing when it's fine.
It doesn't work: what to look at
wg show on the server is the first check: with a connected device it shows latest handshake.
No
latest handshakeCauseUDP 51820 is closed at the host or in ufw, the key or the Endpoint is wrong, or the network cuts WireGuard
What to doOpen the port in the host's panel too;
tcpdump -ni any udp port 51820shows if packets arriveA handshake, but no internet
CauseForwarding is off, or NAT has the wrong interface
What to do
sysctl net.ipv4.ip_forwardmust be 1; compareeth0withip route show defaultA handshake, no internet, Docker on the server
CauseDocker drops forwarded packets
What to doAdd
PostUp = iptables -I FORWARD -i wg0 -j ACCEPTand the same with-o wg0Addresses open, names don't
CauseDNS
What to do
DNS = 1.1.1.1in the device's file; port 53 isn't blockedSmall sites open, big ones hang
CausePackets are too large
What to do
MTU = 1280in the device's[Interface]Unknown device typeorOperation not supportedat startCauseThe kernel has no WireGuard: a container or a very old system
What to doA KVM server, or update the system and reboot
Works until a second device connects
CauseBoth have one key
What to doA key for each device