Press ESC to close · Ctrl+K to open

How to Set Up a VPN in Your Home Lab with WireGuard

Access your local network from anywhere in the world, securely and in under 45 minutes

VPN Home Lab diagram with WireGuard — Secure connection from Internet to home local network

Choose how you want to follow this guide:

1
2
3
4
5
6
7

Planning and Requirements

Step 1 of 7 — What do you need to set up your VPN?

Before we start, let's understand what a VPN is and why you'd want one at home. A VPN (Virtual Private Network) creates an encrypted tunnel between your device and your local network, allowing you to access it as if you were at home from anywhere in the world.

  • Server: An always-on Linux machine at home (Raspberry Pi, mini PC, VM, old laptop with Ubuntu...)
  • Router: Access to your router settings to open ports (port forwarding)
  • Public IP: Your ISP assigns a public IP (it can be dynamic, we'll solve that with DDNS)
  • Client device: Phone, laptop or tablet to connect from (WireGuard has apps for everything)
📱 You (away from home) 🌐 Internet 📡 Your Router 🖥️ WireGuard Server 🏠 Local Network
Why WireGuard? It's faster, simpler and more secure than OpenVPN. Its source code has ~4,000 lines versus OpenVPN's ~100,000. It's been integrated into the Linux kernel since version 5.6.

Install WireGuard on the Server

Step 2 of 7 — Prepare the server
  • Update the operating system:
sudo apt update && sudo apt upgrade -y
  • Install WireGuard:
sudo apt install wireguard -y
  • Verify the kernel module is loaded:
sudo modprobe wireguard lsmod | grep wireguard
  • Enable IP forwarding (packet forwarding):
# Edit sysctl.conf sudo nano /etc/sysctl.conf # Uncomment or add this line: net.ipv4.ip_forward = 1 # Apply changes: sudo sysctl -p
On Raspberry Pi OS (Debian), the command is exactly the same. On Ubuntu 20.04+, WireGuard is already in the official repositories.

Generate Cryptographic Keys

Step 3 of 7 — The foundation of security

WireGuard uses elliptic curve cryptography (Curve25519) to generate key pairs. Each device (server and each client) needs its own pair.

  • Generate server keys:
# Create secure directory sudo mkdir -p /etc/wireguard/keys cd /etc/wireguard/keys # Generate server private key wg genkey | sudo tee server_private.key # Generate public key from private key sudo cat server_private.key | wg pubkey | sudo tee server_public.key # Protect key files sudo chmod 600 /etc/wireguard/keys/*
  • Generate client keys (repeat for each device):
# Client 1 keys (e.g.: my phone) wg genkey | sudo tee client1_private.key sudo cat client1_private.key | wg pubkey | sudo tee client1_public.key
  • Write down the keys (you'll need them in the next steps)
NEVER share private keys! Private keys are like passwords. Only public keys are shared between server and clients.

Configure the WireGuard Server

Step 4 of 7 — The main configuration file
  • Identify your network interface:
# See which interface your server uses for Internet ip route | grep default # Typical result: "default via 192.168.1.1 dev eth0" # Your interface is: eth0 (or wlan0 if using WiFi)
  • Create the configuration file /etc/wireguard/wg0.conf:
sudo nano /etc/wireguard/wg0.conf
  • Write the configuration (replace keys and interface):
[Interface] # Server IP within the VPN tunnel Address = 10.0.0.1/24 # Listening port (UDP) ListenPort = 51820 # Server PRIVATE key PrivateKey = <CONTENTS_OF_server_private.key> # Firewall rules (NAT) — replace eth0 with your interface 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 # --- Clients (Peers) --- [Peer] # Client 1: My phone PublicKey = <CLIENT_1_PUBLIC_KEY> AllowedIPs = 10.0.0.2/32
  • Start and enable WireGuard:
# Start the VPN interface sudo wg-quick up wg0 # Enable at system boot sudo systemctl enable wg-quick@wg0 # Check status sudo wg show
The 10.0.0.0/24 subnet is the VPN tunnel's virtual network. The server is 10.0.0.1, and clients go from 10.0.0.2 onwards.

Router: Port Forwarding and DDNS

Step 5 of 7 — Opening the door from the Internet

For you to connect from outside your home, your router needs to redirect VPN connections to the server.

  • Access your router panel (usually http://192.168.1.1)
  • Find the Port Forwarding / NAT / Virtual Servers section
  • Create a rule:
Port Forwarding Configuration: Protocol → UDP
External Port → 51820
Internal IP → Your server's IP (e.g.: 192.168.1.100)
Internal Port → 51820
  • Dynamic IP → Configure DDNS (if your public IP changes)
# Option 1: Duck DNS (free) — duckdns.org # Sign up and create a subdomain: yourdomain.duckdns.org # Install cron to update the IP automatically: echo "*/5 * * * * curl -s 'https://www.duckdns.org/update?domains=yourdomain&token=YOUR_TOKEN'" | crontab - # Option 2: No-IP (noip.com) — free with monthly renewal # Option 3: Some routers have built-in DDNS (TP-Link, ASUS, etc.)
  • Assign a static IP (DHCP reservation) to the server on the router
If your public IP is static, you don't need DDNS. You can check at whatismyip.com — if it changes daily, you need DDNS.

Configure the Clients

Step 6 of 7 — Connect your devices
  • Install WireGuard on the client:
Download WireGuard: 📱 AndroidGoogle Play Store
🍎 iOSApp Store
💻 Windowswireguard.com/install
🐧 Linuxsudo apt install wireguard
🍏 macOSMac App Store
  • Create the client configuration file:
[Interface] # Client IP within the tunnel (unique per device) Address = 10.0.0.2/32 # Client PRIVATE key PrivateKey = <CONTENTS_OF_client1_private.key> # DNS (use your network's or a public one) DNS = 1.1.1.1, 8.8.8.8 [Peer] # Server PUBLIC key PublicKey = <CONTENTS_OF_server_public.key> # Your home address (public IP or DDNS domain) Endpoint = yourdomain.duckdns.org:51820 # What traffic to route through the VPN: # Local network only: AllowedIPs = 10.0.0.0/24, 192.168.1.0/24 # All traffic (full tunnel): # AllowedIPs = 0.0.0.0/0 # Keep connection alive (important behind NAT) PersistentKeepalive = 25
  • Import the configuration in the app (.conf file or QR code)
Mobile trick: Generate a QR code on the server to import the config easily:
sudo apt install qrencode && qrencode -t ansiutf8 < client1.conf

Verification and Testing

Step 7 of 7 — Confirm everything works
  • Connect the client (activate VPN in the WireGuard app)
  • Verify on the server that the peer is connected:
sudo wg show # Should show: latest handshake, transfer, and client endpoint
  • Ping the server from the client:
# Ping the VPN server ping 10.0.0.1 # Ping a local network device (e.g.: other PC, NAS) ping 192.168.1.50 # Ping from server to client ping 10.0.0.2
  • Access local services (Home Assistant, NAS, cameras, etc.)
  • Verify the connection is stable and fast
VPN is working! You now have secure remote access to your local network from anywhere in the world. WireGuard will keep the tunnel encrypted with minimal latency.
Post-installation security: Configure a firewall (ufw), keep the system updated, and limit peers to only the devices you actually need.
Step 1 of 7