I want to look at Grafana and SSH into my Pis when I am not at home. Until now I opened single ports on the router, which I never liked. I tried OpenVPN some years ago and gave up on the certificate setup. This month I set up WireGuard on the small Ubuntu 18.04 server at home, and it took one evening.
Before you start: WireGuard is not in the mainline Linux kernel yet. You install it as a kernel module from a PPA, and the project’s own website still says it is a work in progress and not yet complete. I use it for my home network, where the risk is mine. I would not put it in front of a company network today without more thought.
The setup
- Home server: Ubuntu 18.04, on the LAN at
192.168.1.10. - Home LAN:
192.168.1.0/24, with the Pis and Grafana. - VPN network:
10.10.0.0/24. The server is10.10.0.1, the laptop10.10.0.2, the phone10.10.0.3. - The router forwards one UDP port (51820) to the server.
Pick a VPN subnet that is different from your home LAN and from typical hotel and office networks. 192.168.1.0/24 as a VPN subnet will collide with half the Wi-Fi networks you join.
Install
sudo add-apt-repository ppa:wireguard/wireguard
sudo apt update
sudo apt install wireguard
This builds the kernel module with DKMS, so it is rebuilt after each kernel update. Make sure the kernel headers are installed. Check after the next kernel upgrade that sudo modprobe wireguard still works. I check it after reboots now, because a VPN that is down is something you only notice when you are away.
Keys
Each peer has a key pair. Generate the server keys:
sudo -i
cd /etc/wireguard
umask 077
wg genkey | tee server.key | wg pubkey > server.pub
Do the same for each client, on the client if possible, so the private key never leaves the device. For the phone I generated the keys in the app.
Server config
/etc/wireguard/wg0.conf:
[Interface]
Address = 10.10.0.1/24
ListenPort = 51820
PrivateKey = <server private key>
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o enp2s0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o enp2s0 -j MASQUERADE
[Peer]
# laptop
PublicKey = <laptop public key>
AllowedIPs = 10.10.0.2/32
[Peer]
# phone
PublicKey = <phone public key>
AllowedIPs = 10.10.0.3/32
Replace enp2s0 with the name of your LAN interface (ip -br addr shows it). I first copied eth0 from a guide and spent some time wondering why I could reach the server but nothing behind it.
The NAT rule makes traffic from VPN clients look like it comes from the server. Then the Pis need no route back to 10.10.0.0/24. The other way is a static route on the router, which is cleaner if your router supports it. Mine does not.
Turn on IP forwarding:
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl --system
Start it, and start it at boot:
sudo wg-quick up wg0
sudo systemctl enable wg-quick@wg0
If you use ufw, allow the port: sudo ufw allow 51820/udp. ufw also has its own forward policy, which may drop the forwarded traffic. Check /etc/default/ufw if clients connect but cannot reach the LAN.
Client config
On the laptop, /etc/wireguard/wg0.conf:
[Interface]
Address = 10.10.0.2/24
PrivateKey = <laptop private key>
[Peer]
PublicKey = <server public key>
Endpoint = home.example.com:51820
AllowedIPs = 10.10.0.0/24, 192.168.1.0/24
PersistentKeepalive = 25
AllowedIPs on the client is the list of networks that go through the tunnel. Here only the VPN and the home LAN do. All other traffic goes out normally. If you want all traffic through home, use 0.0.0.0/0, but then you also need DNS settings, and on Ubuntu wg-quick needs resolvconf for the DNS = line.
PersistentKeepalive sends a small packet every 25 seconds. Without it, NAT on a hotel or mobile network forgets the connection after a while, and the server cannot reach the client. It costs a bit of battery on the phone. I keep it on.
For the endpoint you need a name or address that points to your home. If the IP address changes, a dynamic DNS name works. WireGuard resolves the name when the interface starts, so after an IP change the client has to restart the tunnel.
Check that it works
sudo wg show
shows each peer, the last handshake and the bytes sent and received. No handshake means the packets do not arrive: check the port forward, the firewall, and that the keys are in the right places. WireGuard is silent by design, so there is no error message to read. A wrong key looks the same as a closed port.
Then from the laptop:
ping 10.10.0.1
ssh pi@192.168.1.21
curl -I http://192.168.1.10:3000
What I like and what I do not
The configuration is short and I can read all of it. Adding a device is one [Peer] block. Roaming works well: the laptop moves between networks and the tunnel continues without a reconnect.
What I miss: there is no central way to revoke or rotate keys, you edit the file. There are no users or groups, so if the phone is lost, I remove its peer and restart. With three devices that is fine. With thirty it would not be.