---
title: "UniFi WireGuard On-Demand VPN"
description: "How I set up a WireGuard VPN on a UniFi gateway that auto-connects on iPhone and Mac with On-Demand, plus split DNS so internal names resolve anywhere."
date: 2026-12-04T23:11:33+00:00
modified: 2026-12-05T00:10:53+00:00
author: James Kilby
categories:
  - Homelab
  - Networking
  - UniFi
  - VMware
  - Storage
  - Ansible
  - Artificial Intelligence
tags:
  - #Homelab
  - #VPN
  - #WireGuard
url: https://jameskilby.co.uk/2026/12/unifi-wireguard-on-demand-vpn/
image: https://jameskilby.co.uk/wp-content/uploads/2026/12/unifi-wireguard-on-demand-diagram-1024x538.png
---

![Diagram: an iPhone and Mac with the WireGuard app's On-Demand mode connect over WireGuard \(UDP 51820\) to a UniFi gateway, which sends internal-domain DNS to the Active Directory DNS servers, other DNS to Cloudflare over HTTPS, and traffic to the home lab](https://jameskilby.co.uk/wp-content/uploads/2026/12/unifi-wireguard-on-demand-diagram.png)

[Homelab](https://jameskilby.co.uk/category/homelab/) | [Networking](https://jameskilby.co.uk/category/networking/) | [UniFi](https://jameskilby.co.uk/category/unifi/)

# UniFi WireGuard On-Demand VPN

By[James Kilby](https://jameskilby.co.uk) December 4, 2026 · Updated December 5, 2026 • 📖5 min read(1,092 words)

Since swapping over to my [UniFi Beast](https://jameskilby.co.uk/2026/06/ubiquiti-udm-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](https://jameskilby.co.uk/2024/09/home-network-upgrade/). 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.

![UniFi Policy Table with two Forward Domain DNS records sending the internal domain to the Active Directory DNS servers](https://jameskilby.co.uk/wp-content/uploads/2026/12/ForwardDNS-1024x92.png)

### 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.

  1. Create the WireGuard server: UniFi Network → Settings → VPN → VPN Server → Create New → WireGuard. 
     * Interface: WAN2 ([Zen](https://jameskilby.co.uk/2023/11/configuring-a-zen-internet-and-city-fibre-connection-with-a-3rd-party-router/)). 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.
  2. 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

![UniFi CyberSecure Content Filter rule with the WireGuard network added as a source](https://jameskilby.co.uk/wp-content/uploads/2026/12/DNS-Filtering-1-1024x222.png)

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

![UniFi WireGuard client configuration dialog with a link to download the config file \(QR code blurred\)](https://jameskilby.co.uk/wp-content/uploads/2026/12/WireGuardClientConfig-qr-blurred-1024x502.png)

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
    

📋 Copy

## 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::/64

📋 Copy

Here, `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.

## 📚 Related Posts

  * [UniFi UDM Beast Review: My Homelab Firewall Upgrade](https://jameskilby.co.uk/2026/06/ubiquiti-udm-beast/)
  * [MikroTik CRS504 Review: 100Gb/s Networking in My Homelab](https://jameskilby.co.uk/2022/12/100gb-s-in-my-homelab-sort-of/)
  * [Homelab Network Upgrade: DACs, 40Gb/s vMotion &#038; pfSense](https://jameskilby.co.uk/2022/01/lab-update-part-3-network/)

## Similar Posts

  * [ ![VMware vSphere 8 logo](https://jameskilby.co.uk/wp-content/uploads/2022/12/Screenshot-2022-12-14-at-21.45.23.png) ](https://jameskilby.co.uk/2022/12/forcing-an-upgrade-to-vsphere-8/)

[Homelab](https://jameskilby.co.uk/category/homelab/) | [VMware](https://jameskilby.co.uk/category/vmware/)

### [Forcing an Upgrade to vSphere 8](https://jameskilby.co.uk/2022/12/forcing-an-upgrade-to-vsphere-8/)

By[James Kilby](https://jameskilby.co.uk) December 14, 2022 · Updated November 16, 2026

I run a reasonably extensive homelab that is of course built around the VMware ecosystem.

  * [ ![CityFibre and Zen Internet logos over fibre optic strands](https://jameskilby.co.uk/wp-content/uploads/2023/11/cityfibre-zen-768x403.jpg) ](https://jameskilby.co.uk/2023/11/configuring-a-zen-internet-and-city-fibre-connection-with-a-3rd-party-router/)

[Networking](https://jameskilby.co.uk/category/networking/)

### [Configuring a Zen Internet and City Fibre connection with a 3rd party router](https://jameskilby.co.uk/2023/11/configuring-a-zen-internet-and-city-fibre-connection-with-a-3rd-party-router/)

By[James Kilby](https://jameskilby.co.uk) November 15, 2023 · Updated June 1, 2026

Back in July I bought a new house and one of the best things about the property was that it was already in a City Fibre location.

  * [Homelab](https://jameskilby.co.uk/category/homelab/) | [Networking](https://jameskilby.co.uk/category/networking/)

### [Homelab Network Upgrade: DACs, 40Gb/s vMotion & pfSense](https://jameskilby.co.uk/2022/01/lab-update-part-3-network/)

By[James Kilby](https://jameskilby.co.uk) January 6, 2022 · Updated May 31, 2026

I have retired the WatchGuard Devices with the migration to PFSense running bare-metal in one of the Supermicro Nodes.

  * [Homelab](https://jameskilby.co.uk/category/homelab/) | [Storage](https://jameskilby.co.uk/category/storage/)

### [My First Homelab Storage Setup: HP Gen8 & Xpenology](https://jameskilby.co.uk/2018/01/lab-storage/)

By[James Kilby](https://jameskilby.co.uk) January 6, 2018 · Updated June 1, 2026

I have been meaning to post around some of the lab setup for a while. Although it changes frequently at present it’s as below.

  * [ ![NVIDIA and VMware logos](https://jameskilby.co.uk/wp-content/uploads/2026/01/VMware-NVIDIA-logos_ee2f18dc-615d-4c9e-8f11-9c3c2ce2bf37-prv-768x432.png) ](https://jameskilby.co.uk/2026/02/automating-the-deployment-of-my-ai-homelab-and-other-improvements/)

[Ansible](https://jameskilby.co.uk/category/ansible/) | [Artificial Intelligence](https://jameskilby.co.uk/category/artificial-intelligence/)

### [Automating the Deployment of my Homelab AI Infrastructure](https://jameskilby.co.uk/2026/02/automating-the-deployment-of-my-ai-homelab-and-other-improvements/)

By[James Kilby](https://jameskilby.co.uk) February 9, 2026 · Updated November 17, 2026

Learn how to use Ansible to configure an Ubuntu VM for use with NVIDIA based AI workloads in vSphere

  * [ ![TrueNAS Scale logo](https://jameskilby.co.uk/wp-content/uploads/2022/01/maxresdefault-768x432.jpeg) ](https://jameskilby.co.uk/2022/01/lab-update-part-2-storage/)

[Homelab](https://jameskilby.co.uk/category/homelab/) | [Storage](https://jameskilby.co.uk/category/storage/)

### [Lab Update – Part 2 Storage Truenas Scale](https://jameskilby.co.uk/2022/01/lab-update-part-2-storage/)

By[James Kilby](https://jameskilby.co.uk) January 11, 2022 · Updated November 16, 2026

The HP Z840 has changed its role to a permanent storage box running Truenas Scale.