In this guide you'll set up your own WireGuard VPN server on Ubuntu and connect a phone and a laptop to it. You'll make two kinds of client profile: a full tunnel that sends all your traffic home (handy on public Wi-Fi), and a split tunnel that only sends traffic for your home network (handy for reaching your homelab).
Why self-host? A commercial VPN sends your traffic to a server you don't control, with logging policies you can't check. With your own WireGuard server it's your machine, your keys and your rules. The setup takes me about 15 minutes.
What you need
- An Ubuntu 22.04 or newer server: a cheap cloud VPS, or a machine in your homelab.
- A public IP address for that server. At home, your router needs to forward UDP port 51820 to the server. If your home IP changes, see the Dynamic DNS step.
sudoaccess and basic comfort with the terminal and a text editor.- The WireGuard app on each device you want to connect.
A few terms first
- VPN: an encrypted tunnel between your device and another network. Traffic inside the tunnel can't be read by the Wi-Fi or ISP in between.
- Peer: WireGuard has no strict client/server roles. Every machine in a tunnel is a "peer". I'll still call the always-on machine the server and the phone or laptop a client.
- Key pair: each peer has a private key (secret, never leaves the device) and a public key (safe to share). Peers identify each other by public key.
wg0: the name of the virtual network interface WireGuard creates. It works like a network card with the tunnel's IP address on it.- AllowedIPs: the most important setting. On a client, it lists which destination addresses go through the tunnel. On the server, it lists which source addresses a given peer may use.
- Full tunnel:
AllowedIPs = 0.0.0.0/0, ::/0. All traffic goes through the VPN. - Split tunnel:
AllowedIPslists only your home ranges. Only that traffic uses the VPN; everything else goes out normally. - NAT / masquerade: rewriting the source address of packets so traffic from VPN clients looks like it came from the server. Without it, replies from the internet can't find their way back.
Overview
| Protocol | WireGuard (kernel-space VPN) |
| Purpose | Encrypted remote access to your network |
| Platform | Linux server + any client (iOS, Android, Windows, macOS) |
| License | GPL v2 |
| Repository | wireguard.com |
Why WireGuard over OpenVPN or IPsec
- WireGuard
- ~4,000 lines of code (auditable)
- OpenVPN
- ~100,000 lines of code
- IPSec
- ~400,000 lines of code (many implementations)
- Speed
- runs in the kernel, usually faster than OpenVPN
- Roaming
- Built-in (WireGuard) vs Manual (OpenVPN)
A small codebase is easier to audit. WireGuard runs inside the Linux kernel and its handshake is quick, so it's generally faster than OpenVPN and adds little latency. How much faster depends on your hardware. It also handles roaming: switch your phone from Wi-Fi to mobile data and the tunnel keeps working.
Which tunnel for which job
| Scenario | Problem | WireGuard Solution |
|---|---|---|
| Coffee shop Wi-Fi | All traffic visible to network owner | Full tunnel encrypts everything |
| Traveling | Can't access homelab services | Split tunnel reaches internal IPs |
| Remote work | Need office network access | Site-to-site tunnel connects offices |
| Mobile device | Cellular data monitored by ISP | Full tunnel hides all traffic |
- Full Tunnel
- Split Tunnel
A full tunnel moves trust rather than removing it: your home ISP now sees the traffic that leaves your home connection.
Step 0: Server requirements
| Component | Minimum |
|---|---|
| Server | Ubuntu 22.04+ (any cloud VPS or homelab box) |
| RAM | 256 MB |
| CPU | 1 core |
| Bandwidth | Depends on usage (1 Mbps per connected client minimum) |
| Public IP | Required for server (or use Dynamic DNS) |
Step 1: Install WireGuard
On the Ubuntu server
sudo apt update
sudo apt install wireguard wireguard-tools -y
# Verify
wg --version
# wireguard-tools v1.0.20210914
Turn on IP forwarding
By default Linux drops packets that arrive on one interface and are meant for another. The server needs to pass traffic from wg0 out to your LAN and the internet, so turn forwarding on:
# Enable IPv4 forwarding
echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl -p /etc/sysctl.d/99-wireguard.conf
# Verify
sysctl net.ipv4.ip_forward
# net.ipv4.ip_forward = 1
A file in /etc/sysctl.d/ survives reboots and keeps your change separate from the system defaults.
Step 2: Generate the server keys
# Generate server private key (umask 077 makes the file readable by root only)
sudo sh -c 'umask 077; wg genkey > /etc/wireguard/server_private.key'
# Derive public key
sudo cat /etc/wireguard/server_private.key | wg pubkey | sudo tee /etc/wireguard/server_public.key
# Display keys
echo "Server Private Key: $(sudo cat /etc/wireguard/server_private.key)"
echo "Server Public Key: $(sudo cat /etc/wireguard/server_public.key)"
Setting umask 077 before writing the key means the file is created private, rather than created readable and locked down a moment later. That's the method WireGuard's own quick start uses. Never paste the private key into chat, email or a screenshot.
Check it: sudo ls -l /etc/wireguard/ shows server_private.key as -rw-------.
Step 3: Configure the server
First find the name of the server's internet-facing network interface. It isn't always eth0:
ip route show default
# default via 203.0.113.1 dev eth0 ...
The word after dev is your interface. If it's ens3 or enp1s0, use that instead of eth0 in the config below.
Create the config:
sudo nano /etc/wireguard/wg0.conf
[Interface]
# Server settings
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <SERVER_PRIVATE_KEY>
# Firewall rules (run on interface up)
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
# Optional: DNS for clients
# DNS = 1.1.1.1, 8.8.8.8
# --- Client 1 (Phone) ---
[Peer]
PublicKey = <CLIENT1_PUBLIC_KEY>
AllowedIPs = 10.0.0.2/32
# --- Client 2 (Laptop) ---
[Peer]
PublicKey = <CLIENT2_PUBLIC_KEY>
AllowedIPs = 10.0.0.3/32
What each part does:
Address = 10.0.0.1/24gives the server the first address in a private tunnel subnet. Pick a range that doesn't clash with your LAN or with networks you'll connect from.ListenPort = 51820is the UDP port clients connect to.PostUp/PostDownrun when the tunnel starts and stops. They allow forwarding fromwg0and add masquerading on the internet-facing interface.- Each
[Peer]block is one client.AllowedIPs = 10.0.0.2/32means "this peer is 10.0.0.2 and nothing else". Every client needs its own address.
You'll fill in the client public keys in Step 4. Lock the file down, since it contains the private key:
sudo chmod 600 /etc/wireguard/wg0.conf
Start WireGuard
sudo wg-quick up wg0
# Enable on boot
sudo systemctl enable wg-quick@wg0
# Check status
sudo wg show
# interface: wg0
# public key: <key>
# private key: (hidden)
# listening port: 51820
Whenever you edit wg0.conf later (for example to add a peer), apply it with:
sudo systemctl restart wg-quick@wg0
Step 4: Configure the clients
Generate client keys
You can do this on the server and hand the finished config to each device. Set a private umask first so the key files aren't world-readable:
umask 077
# Client 1 (Phone)
wg genkey | tee client1_private.key | wg pubkey > client1_public.key
# Client 2 (Laptop)
wg genkey | tee client2_private.key | wg pubkey > client2_public.key
Copy each clientN_public.key into the matching [Peer] block in the server's wg0.conf, then restart wg-quick@wg0. Delete the client private key files from the server once the clients are set up.
Full-tunnel client config
All traffic goes through the VPN. Save this as client1_full.conf:
[Interface]
PrivateKey = <CLIENT_PRIVATE_KEY>
Address = 10.0.0.2/24
DNS = 1.1.1.1, 8.8.8.8
[Peer]
PublicKey = <SERVER_PUBLIC_KEY>
Endpoint = YOUR_SERVER_IP:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
Addressmust match theAllowedIPsthe server gave this peer (10.0.0.2 for the phone, 10.0.0.3 for the laptop).Endpointis the server's public IP or hostname and port.AllowedIPs = 0.0.0.0/0, ::/0sends every IPv4 and IPv6 destination through the tunnel. Since this server doesn't route IPv6, IPv6 traffic simply stops instead of leaking around the VPN.PersistentKeepalive = 25sends a tiny packet every 25 seconds so routers and firewalls between you and the server don't forget the connection.
Split-tunnel client config
Only traffic for the tunnel and your home network goes through the VPN. Everything else uses your normal connection, which is faster for general browsing.
[Interface]
PrivateKey = <CLIENT_PRIVATE_KEY>
Address = 10.0.0.2/24
DNS = 192.168.40.1 # Your home DNS server (router or Pi-hole)
[Peer]
PublicKey = <SERVER_PUBLIC_KEY>
Endpoint = YOUR_SERVER_IP:51820
AllowedIPs = 10.0.0.0/24, 192.168.0.0/16
PersistentKeepalive = 25
192.168.0.0/16 covers every 192.168.x.x network. That's convenient, but it also captures the hotel or café network you're sitting on if it uses 192.168 addresses, which can break local printing or captive portals. If that bites you, list only your real home subnets instead, for example 10.0.0.0/24, 192.168.40.0/24.
QR code for phones
Typing a key on a phone is miserable. Turn the config into a QR code instead:
# Install qrencode
sudo apt install qrencode -y
# Generate QR code
qrencode -t ansiutf8 < client1_full.conf
# Or save as image
qrencode -o client1.png < client1_full.conf
The QR code contains the client's private key. Scan it and then delete the PNG.
Step 5: Firewall rules
With UFW
# Allow SSH first (don't lock yourself out)
sudo ufw allow ssh
# Allow WireGuard port
sudo ufw allow 51820/udp
# Allow WireGuard traffic
sudo ufw allow in on wg0
# Enable UFW
sudo ufw enable
sudo ufw status
I moved allow ssh to the top. If you're connected over SSH, enabling UFW before allowing SSH can cut you off.
ufw allow in on wg0 lets VPN clients reach services on the server itself. Forwarding through the server is handled by the PostUp rules in wg0.conf.
On a VPS, also check your provider's own firewall or security group allows UDP 51820. At home, forward UDP 51820 on your router to the server.
With plain iptables (alternative)
# Allow WireGuard port
iptables -A INPUT -p udp --dport 51820 -j ACCEPT
# Allow forwarded traffic through wg0
iptables -A FORWARD -i wg0 -j ACCEPT
iptables -A FORWARD -o wg0 -j ACCEPT
# NAT for internet access through VPN
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Run these as root, and swap eth0 for your interface. Rules added this way disappear on reboot unless you save them (for example with the iptables-persistent package).
Step 6: Connect your devices
iOS and Android
- Install WireGuard from the App Store or Play Store.
- Open the app → Add Tunnel → Scan QR Code.
- Scan the QR code from Step 4.
- Toggle the VPN on.
Windows and macOS
- Download the app from wireguard.com/install.
- Import the
.conffile. - Activate the tunnel.
Check it worked
On the server, run sudo wg show. A connected peer shows a latest handshake a few seconds ago and growing transfer numbers.
On the client:
- Full tunnel: visit a "what is my IP" site. It should show your server's public IP.
- Split tunnel:
ping 10.0.0.1should answer, and a home device like192.168.40.10should be reachable, while your public IP stays the same as without the VPN.
Step 7: Site-to-site tunnel
A site-to-site tunnel joins two whole networks, say home and office, so devices on each side can reach the other. Each side runs WireGuard on a gateway machine, and each side's AllowedIPs lists the other side's networks.
Site A (Home)
[Interface]
Address = 10.0.0.1/24
PrivateKey = <SITE_A_PRIVATE_KEY>
ListenPort = 51820
[Peer]
PublicKey = <SITE_B_PUBLIC_KEY>
Endpoint = SITE_B_IP:51820
AllowedIPs = 10.0.0.0/24, 192.168.20.0/24 # Site B's networks
PersistentKeepalive = 25
Site B (Office)
[Interface]
Address = 10.0.0.2/24
PrivateKey = <SITE_B_PRIVATE_KEY>
ListenPort = 51820
[Peer]
PublicKey = <SITE_A_PUBLIC_KEY>
Endpoint = SITE_A_IP:51820
AllowedIPs = 10.0.0.0/24, 192.168.10.0/24 # Site A's networks
PersistentKeepalive = 25
A few things this needs to work:
- The two LANs must use different subnets (here
192.168.10.0/24at home and192.168.20.0/24at the office). - Both gateways need IP forwarding turned on (Step 1).
- Other devices on each LAN must know to send traffic for the far network to the WireGuard gateway. The usual way is a static route on each site's main router.
- If the same machine also serves your phone and laptop, put the site-to-site link on its own interface (
wg1.conf) with a different port and tunnel subnet.
Step 8: Dynamic DNS (optional)
Most home internet connections get a new public IP from time to time, which breaks the Endpoint in your client configs. Dynamic DNS keeps a hostname pointed at your current IP, so you can use Endpoint = vpn.example.com:51820 instead.
# Install ddclient
sudo apt install ddclient -y
# Configure /etc/ddclient.conf
protocol=cloudflare
use=web
login=your@email.com
password=your_api_token
server=api.cloudflare.com
zone=yourdomain.com
yourdomain.com
The exact login settings for Cloudflare depend on your ddclient version and on whether you use an API token or the older global API key. Check the example in your installed /usr/share/doc/ddclient/ or the ddclient docs before relying on it. /etc/ddclient.conf holds a credential, so keep it readable by root only.
Troubleshooting
No handshake (sudo wg show lists the peer but no "latest handshake"). The client can't reach UDP 51820. Check the router port forward, the VPS firewall, sudo ufw status, and the Endpoint IP. Also double-check each side has the other side's public key.
Handshake works but no internet on full tunnel. Usually IP forwarding is off (sysctl net.ipv4.ip_forward shows 0) or the PostUp rules name the wrong interface. ip route show default tells you the right one.
Websites don't load but IPs do. A DNS problem. The DNS = server in the client config must be reachable through the tunnel.
Split tunnel can't reach home devices. Check that AllowedIPs on the client includes the home subnet, and that the server can reach that subnet itself (ping 192.168.40.10 from the server).
Two clients keep knocking each other off. They share the same key or the same Address. Every device needs its own key pair and its own tunnel IP.
The tunnel drops after a while of no traffic. Add PersistentKeepalive = 25 to the client's [Peer] section.
What I'd tell anyone building one
-
Full tunnel on public Wi-Fi, split tunnel otherwise. At a coffee shop, send everything home so the network owner sees only encrypted packets. On a trusted network, only route homelab traffic, so your streaming doesn't detour through a home upload link.
-
WireGuard uses UDP, not TCP. Tunnelling TCP inside TCP causes nasty slowdowns when packets get lost, and UDP avoids that. The downside: some restrictive networks block UDP. If that's your situation, look at alternatives such as Outline or Cloudflare WARP.
-
Use
PersistentKeepalive = 25on clients behind NAT. WireGuard's own docs give 25 seconds as an interval that works with most firewalls. Without it, the NAT mapping can expire during quiet periods. -
AllowedIPscontrols routing.0.0.0.0/0routes everything (full tunnel).10.0.0.0/24, 192.168.0.0/16routes only internal networks (split tunnel). It's the setting to check first when something doesn't route. -
Use QR codes for phones. WireGuard keys are 32 bytes, written as 44 base64 characters.
qrencode -t ansiutf8 < client.confgets them onto a phone in seconds, without typos. -
Allow SSH before you enable the firewall. Run
sudo ufw allow sshfirst. Locking yourself out of a remote server is a rite of passage you can skip.
Next steps
- Point the client
DNSat a Pi-hole at home to get ad blocking on the road. - Add a peer per device rather than sharing one config. You can then revoke a lost phone by deleting its
[Peer]block. - Add IPv6 to the tunnel if your server has it.
Get it
- WireGuard: wireguard.com
- Installation: wireguard.com/install
- Quick Start: wireguard.com/quickstart
- Architecture: wireguard.com/protocol
Last updated: 2026-09-01. Tested on Ubuntu 24.04 LTS with WireGuard kernel module, iOS/Android clients, site-to-site tunnels.