Tutorials 13 min read

Build Your Own WireGuard VPN on a VPS in 2026: The Complete Guide

Set up a WireGuard VPN on an Ubuntu 24.04 or Debian 12/13 VPS: choosing the VPS, forwarding and NAT, keys, phone via QR code, PC, IPv6 and DNS leaks, MTU, and common failures. Commands checked against the official documentation.

S By Selfhostr · no sponsored content
Build Your Own WireGuard VPN on a VPS in 2026: The Complete Guide
This article may contain affiliate links (no extra cost to you, it funds the site). As an Amazon Associate, Selfhostr earns from qualifying purchases. See the disclosure.

👍 What we like

  • You are the only operator of the tunnel and the exit IP
  • Short, readable configuration with one key pair per device
  • Add or revoke a device without dropping the others (systemctl reload or wg set)
  • Set up a phone in seconds with a QR code

👎 What to watch

  • No anonymity: the exit IP is rented in your name
  • UDP only, no obfuscation: blocked on some hotel and corporate Wi-Fi
  • Datacenter IP, sometimes rejected by services that filter those ranges
  • Updates, firewall, and key backups are up to you
📑 Contents ▾

If you want an Internet exit that belongs to you, whether to protect your traffic on public Wi-Fi, get a fixed IP you can allowlist, or reach services hosted on that same VPS, a WireGuard server on a small Linux VPS does the job well. All you need is an Ubuntu 24.04 or Debian 12/13 machine with a public IPv4, one open UDP port, and about forty lines of configuration. It is not an anonymity tool, though. It is also not the simplest option if all you want is to reach your NAS at home: in that case, a mesh VPN like Tailscale or WireGuard running on your router saves you the middleman server. This guide sets up a “full tunnel” VPN (all traffic goes through the VPS), connects a phone and a computer, then covers the typical WireGuard failures and how to fix them.

The commands and options here were checked in November 2026 against the official WireGuard documentation: the install page, the wg(8) and wg-quick(8) man pages, and the systemd unit that ships with wireguard-tools. This guide does not recommend any specific host and gives no throughput figures: those depend on the VPS and on your connection.

What a WireGuard VPN on a VPS does, and what it doesn’t

What it does well:

  • It encrypts all traffic between your device and the VPS. On hotel or train station Wi-Fi, the network operator sees only a UDP stream to a single IP.
  • It gives you a stable exit IP. That’s handy for allowing access to an admin panel, a database, or SSH from that one address only.
  • It gives you access to services on the VPS (dashboards, a filtering DNS resolver, etc.) without exposing them to the Internet.

What it doesn’t do:

  • Anonymity. The exit IP is rented in your name from the host, and you are its only user. Your ISP no longer sees where you go, but the VPS host can see the metadata of traffic leaving the server. You move trust around more than you remove it.
  • Getting around censorship or filtering networks. WireGuard runs over UDP only. It has no TCP mode and, according to its known limitations page, it “does not focus on obfuscation”: a corporate firewall or a Wi-Fi network that blocks UDP will block it.
  • Guaranteed geo-unblocking. Your IP belongs to a datacenter. Some services filter those ranges, and nothing guarantees a VPS IP will be accepted.

To compare these approaches (plain WireGuard, OpenVPN which can fall back to TCP, Tailscale which manages keys for you), see WireGuard vs OpenVPN vs Tailscale.

Choosing the VPS: what actually matters for WireGuard

WireGuard uses very few resources, so a host’s smallest plan is enough for personal use. The criteria that matter are elsewhere:

  • Location. Pick a datacenter close to you. Every packet takes a detour through the VPS, and that latency adds to all your browsing.
  • Traffic allowance. In full tunnel mode, everything you download passes through the VPS twice, once in and once out. Check how the plan counts traffic and what happens once you go over the allowance.
  • The host’s firewall. Many cloud hosts offer an upstream network firewall, configured in their console. If it’s on, you must allow the UDP port there as well as in UFW. This is a very common oversight.
  • A dedicated public IPv4. Some very cheap plans only give you IPv6 or a shared, NATed IPv4: they complicate or rule out this guide.

To compare hosts, see DigitalOcean vs Vultr vs Hetzner. Before you start, secure the machine (non-root user, SSH keys, UFW) with install and secure an Ubuntu VPS.

The addressing plan used in this guide

ItemValue in this guide
VPN subnet10.8.0.0/24
Server (VPS)10.8.0.1
Phone10.8.0.2
Computer10.8.0.3
Listen port51820/udp
VPS public IP (example)203.0.113.10

Port 51820 is a convention used throughout the documentation, not a required value. According to wg(8), if ListenPort is missing, WireGuard picks a random port every time the interface comes up. That’s why we set it explicitly. Pick a subnet that doesn’t overlap with the networks you’ll connect from. For example, avoid 192.168.1.0/24, which many home routers use.

Step 1: Install WireGuard and the tools you need

All server commands run in a root shell, with a restrictive umask so the key files you create are readable by root only:

sudo -i
umask 077
apt update
apt install -y wireguard iptables qrencode
wg --version

The wireguard package installs wg and wg-quick. The kernel module has been part of Linux since version 5.6: the Ubuntu 24.04 and Debian 12/13 kernels include it, so there’s nothing to compile. iptables is already on Ubuntu but missing from some minimal Debian images. This guide uses it for NAT. qrencode will be used to set up the phone.

Step 2: Enable IPv4 forwarding

For the VPS to relay your clients’ traffic to the Internet, turn on forwarding:

echo 'net.ipv4.ip_forward = 1' > /etc/sysctl.d/99-wireguard.conf
sysctl --system
sysctl net.ipv4.ip_forward

The last command should print net.ipv4.ip_forward = 1.

Don’t turn on net.ipv6.conf.all.forwarding out of habit. On Linux, a machine that forwards IPv6 stops honoring router advertisements by default. If your VPS gets its IPv6 that way, it can lose IPv6 connectivity at the next reboot. This guide runs the VPN over IPv4 and blocks IPv6 on the client side (see Step 8). Only enable IPv6 forwarding if you also set up IPv6 addressing inside the tunnel and you know how your host configures IPv6.

Step 3: Generate the server keys

cd /etc/wireguard
wg genkey | tee server_private.key | wg pubkey > server_public.key
cat server_public.key

Thanks to the umask 077 from Step 1, both files are mode 600. The private key never leaves the server. Only the public key gets copied into client configs.

Step 4: Identify the outbound interface

NAT needs to know which interface leads to the Internet:

ip route get 1.1.1.1 | grep -oP 'dev \K\S+'

The result is often something like eth0, ens3, or enp1s0. The rest of this guide assumes eth0: replace it everywhere with your value. A wrong interface name in the NAT rule is the number one cause of “handshake OK but no Internet.”

Step 5: Write the server configuration

Create /etc/wireguard/wg0.conf and paste in your server private key (cat server_private.key):

[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = CLE_PRIVEE_SERVEUR

# Relayer le trafic des clients vers Internet, et le retour vers les clients
PostUp = iptables -A FORWARD -i %i -o eth0 -j ACCEPT
PostUp = iptables -A FORWARD -i eth0 -o %i -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -o eth0 -j ACCEPT
PostDown = iptables -D FORWARD -i eth0 -o %i -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

# Les blocs [Peer] des clients sont ajoutés à la suite

(The comments in the file mean “relay client traffic to the Internet and the replies back to the clients” and “client [Peer] blocks are added below.”) A few notes from wg-quick(8):

  • %i is replaced with the interface name (wg0). The same config still works if you rename the file.
  • PostUp and PostDown can be repeated, and the commands run in order. That’s easier to read than one long line full of semicolons.
  • The second rule lets replies come back in. It means you don’t depend on UFW’s or Docker’s default settings for forwarded traffic.
  • These rules only allow wg0 to the Internet, so clients can’t reach each other through the tunnel. If you want them to, add PostUp = iptables -A FORWARD -i %i -o %i -j ACCEPT and the matching PostDown.
  • Don’t enable SaveConfig = true if you edit the file by hand. With that option, wg-quick rewrites the file from the interface’s state at shutdown, and any edits made in the meantime are lost.
chmod 600 /etc/wireguard/wg0.conf

Step 6: Open the port in the firewall

With UFW:

ufw allow 51820/udp
ufw status | grep 51820

If UFW wasn’t active yet, allow SSH (ufw allow OpenSSH) before running ufw enable. If your host has a firewall in its console, open 51820/udp inbound there too.

Step 7: Bring the interface up

systemctl enable --now wg-quick@wg0
wg show

wg show lists wg0, its public key, and the listen port. No peers yet is normal at this point. The service will start automatically at boot.

Step 8: Add a phone

Each device gets its own key pair. Still in /etc/wireguard:

wg genkey | tee phone_private.key | wg pubkey > phone_public.key

Declare the phone on the server

Append this to the end of wg0.conf, using the contents of phone_public.key:

[Peer]
# Téléphone
PublicKey = CLE_PUBLIQUE_PHONE
AllowedIPs = 10.8.0.2/32

On the server, AllowedIPs acts as both a routing table and an access list: WireGuard only accepts packets from this peer whose source is 10.8.0.2. Apply it without dropping other connections:

systemctl reload wg-quick@wg0

The unit that ships with wireguard-tools then runs wg syncconf on a stripped-down version of the file. That command updates peers and keys, but it does not reapply Address, PostUp, or the other [Interface] settings. After changing that section, run systemctl restart wg-quick@wg0.

Write the phone’s configuration

Create a temporary file in root’s home directory (not in /tmp):

cat > /root/phone.conf <<EOF
[Interface]
PrivateKey = $(cat /etc/wireguard/phone_private.key)
Address = 10.8.0.2/32
DNS = 1.1.1.1

[Peer]
PublicKey = $(cat /etc/wireguard/server_public.key)
Endpoint = 203.0.113.10:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
EOF

Replace 203.0.113.10 with the VPS’s public IP. Two lines need explaining:

  • AllowedIPs = 0.0.0.0/0, ::/0 sends all traffic into the tunnel, IPv6 included. The client has no IPv6 address inside the tunnel, so IPv6 traffic doesn’t get out at all and the OS falls back to IPv4. That’s intentional: without ::/0, a phone on 4G/5G with IPv6 would send part of its traffic outside the VPN.
  • PersistentKeepalive = 25 sends a tiny packet every 25 seconds to keep the carrier’s or home router’s NAT mapping open. According to wg(8), most setups don’t need it. On a phone that switches networks a lot, its main benefit is avoiding dead air when the phone wakes up. Remove it if you want to save a bit of battery.

Transfer it with a QR code

qrencode -t ansiutf8 < /root/phone.conf

In the WireGuard app (iOS or Android): tap ”+”, then “Scan from QR code.” Then turn the tunnel on. Delete the file right away, since it contains the phone’s private key:

shred -u /root/phone.conf

Step 9: Add a computer

Same procedure with different file names: a laptop_private.key / laptop_public.key pair, a [Peer] block with AllowedIPs = 10.8.0.3/32 on the server, a reload, and a client file with Address = 10.8.0.3/32.

  • Windows and macOS: install the official WireGuard app and import the .conf. On Windows, when AllowedIPs contains 0.0.0.0/0, the app offers a “Block untunneled traffic (kill-switch)” checkbox.
  • Linux: install wireguard, copy the file to /etc/wireguard/wg0.conf, then run sudo wg-quick up wg0. To apply the DNS = line, wg-quick calls resolvconf. Without a compatible tool (openresolv or resolvconf depending on the distribution), the tunnel won’t come up. Alternatively, remove DNS = and handle DNS in PostUp/PostDown lines, as the man page suggests.

Copy the client file over a secure channel (for example, scp from the server). Never send it by chat or email.

Checking that everything really goes through the tunnel

On the server:

wg show

Under each connected peer, you should see a recent latest handshake line and transfer counters going up. On the client:

curl -s https://api.ipify.org && echo

The IP shown should be the VPS’s. Also open a DNS leak test site: only the resolver from the DNS = line should show up. An IPv6 test should fail (no IPv6) instead of showing your carrier’s IPv6 address.

When it doesn’t work: the usual causes

  • No handshake. The UDP port is closed somewhere: UFW, the host’s firewall, or the network you’re on. Otherwise, the Endpoint is wrong or a public key was pasted in the wrong place (the client’s public key goes on the server, the server’s goes on the client). To narrow it down, try over 4G with Wi-Fi off.
  • Handshake OK, but no Internet. It’s almost always forwarding or NAT. Check that sysctl net.ipv4.ip_forward really returns 1, that the MASQUERADE rule uses the right interface, and that iptables -t nat -L POSTROUTING -v shows counters going up.
  • Some sites load, others hang. That’s a classic MTU symptom. wg-quick works out the MTU automatically, but a PPPoE link or some mobile networks need a lower value. Add MTU = 1280 to the client’s [Interface] section, then raise it gradually if everything works.
  • Works on 4G but not on hotel or office Wi-Fi. The network blocks UDP or that specific port. Changing the port sometimes helps. WireGuard can’t fall back to TCP: on those networks, you need a different tool.
  • The tunnel won’t come up on a Linux client and mentions resolvconf. See Step 9.

Managing your devices over time

  • One key per device, always. If a phone is lost, you revoke only that peer. To cut it off immediately: wg set wg0 peer CLE_PUBLIQUE remove. Also remove its block from wg0.conf, or it will come back at the next restart. Finally, delete its key files.
  • Split tunneling. To send only traffic bound for the VPS through the VPN, set AllowedIPs = 10.8.0.0/24 on the client instead of 0.0.0.0/0, ::/0. A home network is only reachable this way if a machine on that network is itself a peer of the VPS and routes for it: adding 192.168.1.0/24 on the client isn’t enough.
  • Private DNS. You can host AdGuard Home or Pi-hole on the VPS and set DNS = 10.8.0.1 on the clients. Make the resolver listen on 10.8.0.1 only and allow it in UFW (ufw allow in on wg0 to any port 53). Never open port 53 on the public IP: you’d create an open resolver that can be abused for amplification attacks.
  • Backups. /etc/wireguard holds every key. Back it up encrypted, for example with restic to Backblaze B2. Without it, you’ll have to regenerate the config for every device.
  • Updates. WireGuard gets updated along with the kernel and the wireguard-tools package. Ubuntu’s and Debian’s automatic security updates are enough, as long as you reboot after a kernel update.

Frequently asked questions

How many devices can a small VPS handle?

For a household, CPU and memory won’t be the limit. The plan’s bandwidth and monthly allowance decide. With this guide’s /24 subnet, you have more than 250 client addresses.

Do I need a domain name?

No. A fixed public IP is enough for Endpoint. A DNS name becomes useful if you plan to switch VPS: you then just update the DNS record without touching every client. The systemd unit retries DNS resolution of the endpoint indefinitely at startup.

Can my own VPN replace a commercial VPN?

For securing your connections and reaching your services, yes. For blending your traffic with thousands of other users, switching exit countries in one click, or using protocols built to get past filtering, no. The best VPN comparison explains these differences.

If you’d rather not administer anything

A home-built VPN means monitoring the server, keeping it updated, and managing keys. If what you mostly want is an encrypted exit on public Wi-Fi and several exit countries, with no server to maintain, a commercial service like NordVPN provides apps for all your devices. In exchange, you’re trusting it with your traffic. See the NordVPN offer

Key facts

UDP only
Transport
51820/udp (convention)
Guide's port
Built in since 5.6
Linux kernel
Ubuntu 24.04, Debian 12/13
Server OS
Tags: VPNWireGuardVPSLinuxSelf-hostingTutorialNetwork SecurityPrivacy

Related

Tutorials

Access Your NAS Remotely in 2026: Tailscale Step by Step, and Which VPN to Choose

Reach your Synology or Linux NAS from outside with no port forwarding: Tailscale installation, TUN mode on DSM 7, MagicDNS, SMB and access rules. Plus why a commercial VPN is not enough, compared with WireGuard, Headscale or ZeroTier.

Read
Tutorials

Self-Hosting Your Website in 2026: Complete Guide (VPS, Docker, HTTPS)

2026 technical guide to self-hosting on a VPS: choosing plans, Docker setup, Let's Encrypt HTTPS, security, and budget. Compare self-hosting vs. cloud.

Read
Tutorials

Install and Secure an Ubuntu 24.04 VPS in 2026: The Complete A to Z Guide

Master your server with this 2026 guide to installing and hardening an Ubuntu 24.04 VPS. Covers SSH keys, root disablement, UFW, fail2ban, auto-updates, and essential security best practices with ready-to-copy commands.

Read