A WireGuard VPN for work, step by step

Also available in Italian.

At the end of October I set up a WireGuard VPN for the company I work for. I built it in steps, each one on top of the last: the tunnel, a fixed exit IP, a repo with a few scripts to manage it, a page in our internal admin panel, and the logs.

The problem was a familiar one. Our servers are a handful of VPSes, and the sensitive doors on them (SSH, the admin panels) only open to the office’s IP addresses. That’s fine until someone works from home, from a train or from their phone. The options were opening those doors to the whole internet, or adding home IPs to whitelists that nobody would ever clean up. A VPN gives a third option: one more address in every whitelist, and anyone with a valid key comes in from there.

Step 1: a split tunnel, limited on the server

laptop / phone
 ├─ company servers ─▶ tunnel ─▶ VPN server ─▶ out from one fixed IP
 └─ everything else (web, mail, DNS) ─▶ the normal connection

It’s a split tunnel: only traffic to the company’s machines goes through the VPN. Browsing, mail and DNS stay on the normal connection whether the VPN is on or off, so turning it on doesn’t slow down anything else.

The client’s config decides what goes into the tunnel (AllowedIPs), but that config is a text file on someone else’s computer, and anyone can edit it. So the real limit lives on the server: the firewall (ufw) forwards traffic from the tunnel only to the machines that person is allowed to reach, with one rule per user and destination, and the forwarding policy is deny, so clients can’t see each other either. You can change your .conf all you want; it won’t get you to the internet, or to anything else.

Step 2: one IP that survives everything

The whole point is that servers whitelist the VPN’s address, so that address must never change. A droplet’s own IP changes if you rebuild it; a DigitalOcean reserved IP doesn’t, and it can be moved to a new droplet. Having clients connect to the reserved IP is easy. Getting traffic to leave from it took more work, because by default a droplet sends everything out from its own IP. The fix: a default route through the reserved IP’s gateway in netplan, a second routing table so whoever connects to the droplet’s own IP still gets answers from it, and cloud-init told to stop rewriting the network config on boot.

The reserved IP itself has one rule in the README: never release it. Assigned, it’s free; released, it’s gone, and every whitelist with it.

Every server also needed the VPN’s IP in fail2ban’s ignoreip. Everyone comes in from the same address, so a few failed logins from one person would otherwise ban the whole team.

Step 3: the repo as the source of truth

Before the panel, everything lived in a git repo: a list of users, a list of reachable machines, and a few shell scripts I ran from my Mac to add a user, rotate their keys, remove them, regenerate the configs, and compare the server with the repo (and, with a flag, bring it in line). One user is one device: the same config on two devices makes them kick each other off.

Keys are generated on the Mac, inside a throwaway Alpine container if wg and qrencode aren’t installed. The server only ever receives public keys and preshared keys, on stdin, never as command-line arguments, which end up in ps and in shell history. On the server a small script, wg-peer, edits wg0.conf, keeps the last 30 copies, and applies the change with wg syncconf, so adding someone doesn’t disconnect everyone else.

Each person gets a .conf for the computer or a QR code for the phone, sent privately: it contains their private key, so never in a group chat.

Step 4: moving it into the panel

Once the scripts were solid, I moved user management into the company’s internal admin panel, so it no longer depends on my laptop. The panel creates the keys in memory, shows the .conf and QR once (on a page that isn’t cached) and never stores them: giving someone their config again means new keys. Each user gets a profile: the usual split tunnel, a custom one with only the machines assigned to them (for an external consultant, say), or a full tunnel for the occasional job that needs all traffic to come out of the company IP.

Even with the full tunnel, some networks are never reachable: private ranges, loopback, CGNAT and link-local, which on a cloud VPS includes the metadata service at 169.254.169.254. Instead of “allow everything” plus deny rules that only work while they stay at the top of the list, the full profile gets one allow rule per block of public internet, 51 of them, computed by subtracting the forbidden ranges from 0.0.0.0/0. When rules change, the new ones go in first and the old ones come out after, so nobody loses access halfway.

The part I thought about most was how the panel talks to the VPN server. A shell on that server is a shell with the IP every whitelist trusts, so the panel’s SSH key can’t open one:

restrict,command="/usr/local/sbin/wg-pannello-ssh",from="198.51.100.20" ssh-ed25519 AAAA… panel

restrict turns off forwarding and terminals, from= only accepts the key from the panel’s server, and command= replaces whatever the client asks for with a small script that checks the request against a fixed list of wg-peer subcommands and refuses (and logs) everything else, interactive shell included. The sudoers file allows exactly those subcommands and nothing more. Data goes in as JSON on stdin, and one line of JSON comes back.

Step 5: logs, and what they leave out

Every minute a systemd timer reads wg show and writes CONNECTED, DISCONNECTED and IP-CHANGE lines (a phone moving from Wi-Fi to 4G, say). WireGuard has no “disconnect”: a peer counts as connected if its last handshake is less than three minutes old, so the disconnection time is accurate to the minute. An iPhone in standby drops off and comes back on its own, and that’s normal.

The same timer also samples the server, and both end up on the panel’s Connected now page: who has the tunnel up, from where and since when, and a Server card with CPU, memory, disk, VPN traffic and blocked attempts over the last 24 hours. CPU, memory and disk are the whole VPS’s: WireGuard runs in the kernel and isn’t a process, so its cost only shows up as CPU and tunnel traffic. The page refreshes itself every 30 seconds, but only for half an hour, so a tab left open doesn’t keep the session alive forever.

A mockup of the admin panel’s VPN page, in Italian: a table of five connected users with their tunnel address, public IP, connection time and traffic, then a Server card with WireGuard active, VPN traffic, firewall blocks, bars for CPU, memory and disk, and two 24-hour charts of traffic and CPU
A mockup of the panel's Connected now page: the real page, with made-up users, addresses and traffic

The log records who connects, when and from where, never what they open, and it’s kept for about 90 days. Every change, from the panel or the scripts, adds a line to a separate audit log with who made it, and never a key. It’s a log of employees’ access, so the team has to be told what’s in it and for how long. I’m a strong believer in a private internet, and that applies at work too: this part mattered to me.

Things that break in silence

Through all five steps, the VPN itself stayed a dozen lines of config. Most of the work went into details that don’t throw an error; they just quietly don’t work:

  • MTU. WireGuard’s default of 1420 worked at the office and stalled on 4G: the connection opened, then TLS handshakes hung halfway. MTU = 1280 in every config fixed it.
  • Testing in Docker. A container’s eth0 has an MTU of 65535, so wg-quick without an explicit MTU picks 65455, large replies get lost, and it looks exactly like a broken VPN. It wasn’t one.
  • QR codes and accents. With an accented letter in the config (Italian names, for example) some QR readers decode a slightly different text. The scripts now refuse to write a config that isn’t plain ASCII.
  • Interface names. On Linux the tunnel’s name comes from the file name and can be at most 15 characters, so a long username makes wg-quick up fail.
  • The endpoint can’t be in the tunnel. If the VPN server’s own address is in AllowedIPs, the tunnel tries to send its own packets through itself and never connects. From inside the VPN, the server is reached at its tunnel address.
  • wg syncconf wg0 <(wg-quick strip wg0) is the one-liner everyone uses. If strip fails, the process substitution hands syncconf an empty config, and syncconf dutifully removes every peer. I run the two steps separately and check the output in between.
  • A regex ending in $. In Python, re.match lets $ match before a trailing newline, so "thomas\n" passes validation and lands inside wg0.conf. re.fullmatch doesn’t.

Looking back

WireGuard was the easy part: it’s fast, the config fits on one screen and it just works. The work was everything around it: the whitelists, the firewall, where keys live and where they never go, and the details that fail without an error. Writing all of it down in the repo as I went, including why each rule exists, is what made handing it over to the panel easy.

To be continued

Filed under#wireguard#linux#self-hosting

Keep reading

The project behind this postCompany VPN on WireGuardA split-tunnel WireGuard VPN on a small VPS: one fixed exit IP every server trusts, per-person firewall rules, keys that never reach the server.Maintained
  1. Also on #self-hostingRunning a Tor relay

Here, live

Presence

Thomas’s avatarFrieren, as Thomas’s avatarAsuka, as Thomas’s avatarRei, as Thomas’s avatarStatus unknown

Bari, Apulia
Bari, Apulia ·  local time
Now editing
—in—

Connecting…Blooming… (connecting)Synchronising… 82.4%. Obviously.Connecting. I will wait.

Viewers

Right now on site
—
On this page
—
All-time, site
—
All-time, this page
—

last 14 days · No tracking cookies, no IPs stored.

Connecting…Blooming… (connecting)Synchronising… 82.4%. Obviously.Connecting. I will wait.

Contents