WireGuard VPN Setup Guide: Server and Clients on Ubuntu 24.04 and Debian 12/13

Set up your own WireGuard VPN server on Ubuntu 24.04 or Debian 12/13 and connect Linux, phone and desktop clients. Full config examples, IP forwarding and NAT, adding peers without downtime, and fixes for the usual "handshake but no internet" problems.

Share
WireGuard VPN server and clients setup on Ubuntu and Debian
Updated November 2026: rewritten for Ubuntu 24.04 and Debian 12/13, with tested server and client configs, nftables NAT, QR codes for phones, adding peers without a restart, and a troubleshooting section.

This guide sets up your own WireGuard VPN: one server on Ubuntu 24.04 or Debian 12/13 (a cheap VPS is plenty), and clients on Linux, Android, iOS, Windows or macOS. When you're done, a client can either send all its traffic through the server, which is useful on public Wi-Fi or to get the server's IP, or only reach your private network.

The whole thing is about 15 minutes of work. Everything below was tested end to end in a lab with a server, a client and a separate "internet" host, including the failure cases in the troubleshooting section.

How WireGuard works in one minute

WireGuard has no users, passwords or certificates. Every machine (a peer) has a key pair. The server's config lists each client's public key, and each client's config lists the server's public key and address. That's the whole trust model.

The setting people get wrong is AllowedIPs. It does two jobs at once:

  • Outgoing: which destinations get routed into the tunnel to that peer.
  • Incoming: which source IPs are accepted from that peer.

So on the server, a client's AllowedIPs = 10.8.0.2/32 means "packets from this key must come from 10.8.0.2". On the client, AllowedIPs = 0.0.0.0/0 means "send everything through the server". Keep that in mind and most WireGuard configs make sense.

What you need

  • A server with a public IP running Ubuntu 24.04 or Debian 12/13. Kernels 5.6 and later have WireGuard built in, so there's no module to compile.
  • One open UDP port. This guide uses the default, 51820.
  • Root or sudo on the server and on any Linux client.

If the server is at home behind a router, forward UDP 51820 to it. If your ISP uses CGNAT (your router's WAN IP is in 100.64.0.0/10 or doesn't match what curl ifconfig.me says), incoming connections won't reach you. In that case, use a small VPS as the server instead.

Addresses used below:

Machine Tunnel IP
Server 10.8.0.1
Laptop (Linux) 10.8.0.2
Phone 10.8.0.3

Step 1: install WireGuard

On the server and on every Linux client:

sudo apt update
sudo apt install -y wireguard

This installs wireguard-tools, which provides wg and wg-quick. The official quick start covers the same tools if you want the upstream view.

Step 2: generate keys

On the server:

sudo -i
cd /etc/wireguard
umask 077
wg genkey | tee server.key | wg pubkey > server.pub
wg genkey | tee laptop.key | wg pubkey > laptop.pub
wg genkey | tee phone.key | wg pubkey > phone.pub

umask 077 makes the key files readable by root only. Private keys are the only secret in WireGuard, so treat them like SSH keys.

Generating client keys on the server is the convenient way when you'll hand out config files or QR codes. If you prefer, generate each client's key on the client itself and only copy its public key to the server.

Step 3: find the server's public interface

NAT needs the name of the interface that faces the internet. It's often not eth0 anymore:

ip route show default
default via 203.0.113.254 dev eth0

The name after dev is what you need: eth0, ens3, enp1s0 and so on. Replace eth0 in the next step with yours.

Step 4: the server config

Create /etc/wireguard/wg0.conf:

[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <contents of server.key>

# NAT so clients can reach the internet through the server
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
# laptop
PublicKey = <contents of laptop.pub>
AllowedIPs = 10.8.0.2/32

[Peer]
# phone
PublicKey = <contents of phone.pub>
AllowedIPs = 10.8.0.3/32

A few things that matter here:

  • Address = 10.8.0.1/24, not /8. A /8 claims all of 10.0.0.0/8, which collides with most cloud and home networks.
  • %i is replaced by the interface name (wg0), so the rules follow if you rename the file.
  • Each client gets a /32 on the server. Two peers with overlapping AllowedIPs will steal each other's traffic.

No iptables on the server? Minimal Debian 12/13 installs often only have nftables. Use these two lines instead of the iptables ones:

PostUp = nft add table inet wg-nat; nft add chain inet wg-nat postrouting '{ type nat hook postrouting priority srcnat; }'; nft add rule inet wg-nat postrouting ip saddr 10.8.0.0/24 oifname "eth0" masquerade
PostDown = nft delete table inet wg-nat

This only adds the NAT rule. If your nftables ruleset has a forward chain with policy drop, allow traffic in and out of wg0 there as well.

Step 5: enable IP forwarding

Without forwarding, clients connect fine and can ping 10.8.0.1, but nothing past the server works. That's the most common "it connects but there's no internet" cause. Enable it permanently:

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

Check it:

sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1

Step 6: open the firewall

UFW (common on Ubuntu):

sudo ufw allow 51820/udp
sudo ufw route allow in on wg0 out on eth0

The second line lets UFW forward tunnel traffic out to the internet. Without it, UFW's default forward policy drops it.

Cloud providers (Hetzner, AWS, DigitalOcean, Oracle and so on) usually have their own firewall or security group in the web panel. Allow UDP 51820 there too, or the packets never reach your server.

If you run a plain iptables setup, see how to install and configure iptables on Ubuntu 24.04 and add an ACCEPT rule for UDP 51820.

Step 7: start the server

sudo systemctl enable --now wg-quick@wg0
sudo wg show
interface: wg0
  public key: <server public key>
  private key: (hidden)
  listening port: 51820

peer: <laptop public key>
  allowed ips: 10.8.0.2/32

peer: <phone public key>
  allowed ips: 10.8.0.3/32

The peers have no "latest handshake" yet. That shows up once a client connects.

Step 8: Linux client

On the laptop, create /etc/wireguard/wg0.conf:

[Interface]
Address = 10.8.0.2/24
PrivateKey = <contents of laptop.key>
DNS = 1.1.1.1

[Peer]
PublicKey = <contents of server.pub>
Endpoint = YOUR_SERVER_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

Bring it up:

sudo wg-quick up wg0

To start it at boot, use sudo systemctl enable --now wg-quick@wg0 instead.

AllowedIPs = 0.0.0.0/0 sends all IPv4 traffic through the VPN. wg-quick handles the routing so that the tunnel's own packets to the server still go out directly. To reach only the VPN network and keep normal browsing local (a split tunnel), use:

AllowedIPs = 10.8.0.0/24

Add your home LAN too if the server sits there and you want to reach it, for example AllowedIPs = 10.8.0.0/24, 192.168.1.0/24.

PersistentKeepalive = 25 sends a small packet every 25 seconds. That keeps NAT on home routers and mobile networks from forgetting the connection. On a client behind NAT you want it. The server doesn't need it.

Step 9: phones and desktops (QR code)

The official WireGuard apps for Android, iOS, Windows and macOS all import a normal config file, and the mobile apps can scan a QR code. Create phone.conf on the server:

[Interface]
Address = 10.8.0.3/24
PrivateKey = <contents of phone.key>
DNS = 1.1.1.1

[Peer]
PublicKey = <contents of server.pub>
Endpoint = YOUR_SERVER_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

Show it as a QR code in the terminal:

sudo apt install -y qrencode
qrencode -t ansiutf8 < /etc/wireguard/phone.conf

In the app, tap +, choose Scan from QR code, and point the phone at the screen. On Windows and macOS, use Import tunnel(s) from file with the .conf file.

The QR code contains the phone's private key. Don't screenshot it into a chat or a ticket.

Check that it works

On the server, sudo wg show should now show a recent handshake and traffic for the peer:

peer: <laptop public key>
  endpoint: 198.51.100.23:41643
  allowed ips: 10.8.0.2/32
  latest handshake: 4 seconds ago
  transfer: 604 B received, 692 B sent

On the client:

ping -c 3 10.8.0.1        # tunnel works
curl ifconfig.me          # should print the server's public IP in full-tunnel mode

Add a new peer without dropping everyone

Restarting wg-quick@wg0 disconnects every client for a moment. To add a peer on a running server, append its [Peer] block to /etc/wireguard/wg0.conf, then reload only the peer list:

sudo bash -c 'wg syncconf wg0 <(wg-quick strip wg0)'

wg-quick strip (see the wg-quick man page) turns the file into the format wg understands, without Address, PostUp and so on. syncconf applies only the differences, so existing sessions stay up.

Troubleshooting

No handshake at all (latest handshake never appears):

  • UDP 51820 is blocked somewhere: UFW, the cloud firewall, or the home router's port forward. Check each one.
  • The Endpoint IP or port in the client config is wrong.
  • The keys are swapped. The server needs the client's public key, and the client needs the server's public key. Check with wg pubkey < laptop.key and compare.

Handshake works, can ping 10.8.0.1, but no internet:

  • IP forwarding is off. Run sysctl net.ipv4.ip_forward and you'll see 0. See step 5.
  • The NAT rule points at the wrong interface. eth0 in PostUp must match ip route show default. Check the counters with sudo iptables -t nat -L POSTROUTING -v -n. If the MASQUERADE line stays at 0 packets, it isn't matching.
  • UFW forward policy. See step 6.

resolvconf: command not found when the client starts:

wg-quick uses resolvconf to apply the DNS = line. Ubuntu 24.04 has it through systemd-resolved. Minimal Debian may not. Install openresolv (or systemd-resolved), or remove the DNS line.

Connects, but some sites hang or load half-way:

That's usually an MTU problem. wg-quick sets 1420 by default, which is too large on some PPPoE and mobile links. Add MTU = 1380 under [Interface] on the client and reconnect.

Works for a while, then stops on mobile data:

Add PersistentKeepalive = 25 to the client's [Peer] section if it's missing.

Extra security: preshared key

WireGuard can add a symmetric preshared key on top of the key pairs, as an extra layer against future quantum attacks on the key exchange. Generate one per client:

wg genpsk > laptop.psk

Add PresharedKey = <contents> to that client's [Peer] block on the server, and the same line to the [Peer] block in the client's config.

FAQ

WireGuard vs OpenVPN: which should I use?

For a new setup, WireGuard. It's in the Linux kernel, the config is a dozen lines, and it's usually faster and reconnects instantly when you switch networks. OpenVPN still makes sense if you need TCP 443 to get through strict firewalls, or username/password logins.

Is there a web UI?

Not built in. If you want one, wg-easy runs as a Docker container and manages peers and QR codes from a browser. It's handy for a family VPN. For a handful of peers, the config files above are simpler and have fewer moving parts.

Can I change the port?

Yes. Change ListenPort on the server, the port in every client's Endpoint, and the firewall rules. Any free UDP port works.

How many clients can one server handle?

Far more than a homelab needs. WireGuard has very little overhead, so a 1 vCPU VPS handles dozens of peers easily. Bandwidth usually runs out before CPU does.

Does WireGuard work in Docker or LXC?

The client and server tools do, but the WireGuard interface lives in the kernel, so the host kernel has to support it (any kernel from 5.6 on does). The simplest homelab setup is to run the server on a small VM or directly on the host. If you run a userspace implementation such as wireguard-go inside a container and it complains about /dev/net/tun, see fixing "Cannot open TUN/TAP dev /dev/net/tun" in LXC and Docker.

For more self-hosting builds, see the Pi-hole blocklists post. Pi-hole pairs well with this VPN: put the Pi-hole's tunnel IP in the clients' DNS = line and you get ad blocking on your phone everywhere.