UniFi WireGuard On-Demand VPN
Since swapping over to my UniFi Beast, I have been experimenting with ways to reach back to the lab I run at home. I settled on a WireGuard VPN that runs on the UniFi gateway itself and connects automatically on my iPhone and Mac. I needed a few features:
- Easy to configure
- Runs natively on the UniFi Beast
- Auto-connect on iPhone and Mac
- No DNS changes required when connecting the VPN
- Protects my remote devices
Table of Contents
Options Considered
UniFi supports a number of VPN types:
- L2TP
- OpenVPN
- Teleport
- WireGuard
I looked into the pros and cons of them all. L2TP is very legacy and support is dwindling, so that was immediately ruled out. OpenVPN was tempting but not as performant as WireGuard in both throughput and, more importantly, latency. Teleport was trivial to set up, but I couldn’t get the automatic connection with the DNS changes I wanted to work. Therefore, WireGuard was the clear winner.
WireGuard is a modern VPN that aims to be lighter and more performant than other VPN solutions, and it supports both IPv4 and IPv6. Development started in 2015, it was first released in 2016 and it was merged into the Linux kernel in 2020. It has great client support (although I only needed Mac and iPhone).
Design
I wanted each client to have its own IP, and this gives me the ability to firewall differently if needed. If it works smoothly, I may roll the capabilities out to my family.
I wanted traffic to egress through my lab, protected by my UniFi gateway; however, I wanted to be able to switch the tunnel off and route traffic locally if needed.
I also needed access to my internal systems via DNS. I use the same flat DNS zone internally and externally, and the majority of my services are not available externally at all. This was a conscious choice on my part, and it makes the transfer between local and remote (over VPN) seamless.
I wanted WireGuard to auto-connect back to the Beast on all Wi-Fi or cellular networks except my home SSIDs.
Prerequisites
Obviously, you need a UniFi gateway. It doesn’t need to be a Beast.
Static IP or dynamic DNS: clients connect to a fixed address, so you need either a static public IP or a dynamic DNS name for the endpoint.
DNS Setup
Before I built the VPN, I had to tweak how DNS was handled in my lab. Before this change, I had two DNS servers (Active Directory based), and every network was given the AD IP addresses, either statically or handed out by DHCP. I had firewall rules in place to allow this flow.
I amended this so that, rather than handing out the DNS servers, each network uses its gateway address as the DNS server, and UniFi then queries the AD servers. This was done by enabling Auto DNS Server on a per-network basis.
Then, in the Policy Table, I created a DNS Forward Domain record for the domain name pointing at my DNS servers.
I needed two records, one for each AD server.

How DNS Lookups Are Routed
With this working, DNS lookups go one of two ways:
| Query | Where it is sent |
|---|---|
| *.jameskilby.cloud | AD DNS servers |
| Anything else | Cloudflare over encrypted DNS (DNS over HTTPS) |
This is important because it unlocks the UniFi filtering features of the Beast. A lot of these are DNS-driven, so the gateway needs to be in the path.
WireGuard Server Setup
Although I am running this on my Beast, the setup should be similar on any UniFi gateway.
At the time of writing, it runs UniFi OS 6.0.19 and Network 11.0.104.
- Create the WireGuard server: UniFi Network → Settings → VPN → VPN Server → Create New → WireGuard.
- Interface: WAN2 (Zen). Port: 51820 (select your WAN interface; mine just happens to be WAN2).
- Subnet: for example 192.168.5.0/24. Choose a subnet that’s not in use.
- Add one client per device (Mac, iPhone, etc.). Download each config, or show its QR code. To be honest, this was one of my favourite features.
DNS Filtering

Once the WireGuard server is enabled, its network can be added as a source in the CyberSecure Content Filter, as shown above.
This gives me the same filtering when I’m out and about as I have at home. Unfortunately, I wasn’t able to add the WireGuard network to IDS/IPS (Intrusion Detection and Prevention) in the same way.
WireGuard Client Setup

Download the WireGuard app from the App Store. On iPhone, it needs Location permission for the “Except these SSIDs” option to work.
Then select “Add a Tunnel” and choose “Create from QR Code”.
If it imports correctly, it will ask you to name the tunnel and then to allow WireGuard to add VPN configurations.
When it’s successfully imported, go into the tunnel and select Edit. I then enable On-Demand for Cellular and Wi-Fi, change the SSID option to “Except these SSIDs” and select my home networks. It will then connect the VPN whenever I leave the house.
Example Config File
Don’t worry, the keys have been messed with :p
[Interface]
PrivateKey = CD4GYUKUITFDSTPPbVsV7jfWjYPBZGGHJcmhX8=
Address = 192.168.5.3/32,fdc3:6935:f58a:c05::3/128
DNS = 192.168.5.1,fdc3:6935:f58a:c05::1
[Peer]
PublicKey = tyV9MebFgjDRe2lb8eaVFCRTOLIOSqICetCQw=
AllowedIPs = 0.0.0.0/0,::/0
Endpoint = 203.0.113.10:51820
Switching to a Split Tunnel
The config above is a full tunnel: AllowedIPs = 0.0.0.0/0,::/0 sends all the traffic through the VPN. I wanted to be able to go split tunnel if needed. You can do this directly on the client by replacing AllowedIPs with only the networks you want to reach through the tunnel, for example:
[Peer]
AllowedIPs = 192.168.5.0/24,192.168.10.0/24,fdc3:6935:f58a:c05::/64Here, 192.168.10.0/24 is an example home network that would be routed over the VPN. Make sure the WireGuard subnet (192.168.5.0/24) stays in the list, or the DNS server at 192.168.5.1 can’t be reached and internal names will stop resolving.
Experience
It worked much better than expected. I have just come back from a weekend in Germany and was using my phone a lot, so I can’t really determine if battery usage was higher; however, the network experience was seamless. At no point did I not have a working connection, and it also coped with guest Wi-Fi login portals at hotels and airports.
Overall, the gateway does the hard work, each device has its own peer, and On-Demand means I never have to think about connecting. If it keeps running this smoothly, the next step is rolling it out to my family’s devices, each with its own peer and firewall rules.




