ACLClouds
ACLClouds

How do I set up my own WireGuard VPN on a VPS?

Install WireGuard on a Debian or Ubuntu VPS: keys, wg0.conf, routing and NAT with nftables, firewall, IPv6, then a client profile and a QR code for Android

With WireGuard, your VPS becomes a personal VPN: traffic from your phone or computer travels through an encrypted tunnel to the server, then leaves for the Internet with the VPS's IP address. That helps on public Wi-Fi, for reaching private services on the VPS without exposing them, or for keeping a fixed exit address. This guide sets up the server on Debian 12 / 13 or Ubuntu 22.04 / 24.04, then a first mobile client with a QR code. Allow about fifteen minutes.

Step-by-step procedure

  1. Install WireGuard

    Install wireguard, which provides the wg and wg-quick tools, along with qrencode for the QR code and nftables for NAT. The WireGuard module ships with the Linux kernel on Debian 12 / 13 and Ubuntu 22.04 / 24.04, so there is nothing to compile.

  2. Generate the keys

    In /etc/wireguard, after umask 077, generate one key pair for the server and one for the phone. Each private key stays secret; only the public keys are exchanged between the server and the clients.

  3. Write the server configuration

    Create /etc/wireguard/wg0.conf from the block below. Replace SERVER_PRIVATE_KEY with the contents of server.key and PHONE_PUBLIC_KEY with the contents of phone.pub. The PostUp and PostDown lines add and remove the nftables NAT together with the tunnel.

  4. Enable routing

    Allow the VPS to forward packets between the tunnel and the Internet with net.ipv4.ip_forward and net.ipv6.conf.all.forwarding, in a file under /etc/sysctl.d/, then apply it with sysctl --system.

  5. Open UDP port 51820

    Allow UDP 51820 in UFW if you use it, and in the ACLClouds panel "Firewall" if it is enabled. With UFW, also add a ufw route rule: without it, UFW blocks traffic that passes through the VPS.

  6. Start the tunnel

    Run systemctl enable --now wg-quick@wg0. The wg0 interface now comes up with the VPS. wg show displays the server's public key, the listening port and the declared clients.

  7. Connect the phone with a QR code

    Write the phone.conf profile with the contents of phone.key, the contents of server.pub and the VPS's IPv4 address, display it as a QR code with qrencode -t ansiutf8, then add a tunnel in the WireGuard app on Android or iPhone by scanning the code. Turn it on: wg show should report a recent latest handshake.

Install WireGuard and generate the keys

apt update
apt install -y wireguard qrencode nftables
cd /etc/wireguard
umask 077
wg genkey | tee server.key | wg pubkey > server.pub
wg genkey | tee phone.key | wg pubkey > phone.pub
cat server.key phone.pub

/etc/wireguard/wg0.conf (server)

[Interface]
Address = 10.8.0.1/24, fd42:42:42::1/64
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
PostUp = nft add table inet wg-nat; nft add chain inet wg-nat postrouting '{ type nat hook postrouting priority 100; }'; nft add rule inet wg-nat postrouting ip saddr 10.8.0.0/24 oifname != "%i" masquerade; nft add rule inet wg-nat postrouting ip6 saddr fd42:42:42::/64 oifname != "%i" masquerade
PostDown = nft delete table inet wg-nat

[Peer]
PublicKey = PHONE_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32, fd42:42:42::2/128

Enable IPv4 and IPv6 routing

cat > /etc/sysctl.d/99-wireguard.conf <<'EOF'
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
EOF
sysctl --system

UFW firewall and startup

ip route show default
ufw allow 51820/udp
ufw route allow in on wg0 out on eth0
systemctl enable --now wg-quick@wg0
wg show

/etc/wireguard/phone.conf (client)

[Interface]
PrivateKey = PHONE_PRIVATE_KEY
Address = 10.8.0.2/32, fd42:42:42::2/128
DNS = 1.1.1.1, 2606:4700:4700::1111

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = VPS_IPV4:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25

Show the QR code, then delete the phone's key

qrencode -t ansiutf8 < /etc/wireguard/phone.conf
rm /etc/wireguard/phone.key /etc/wireguard/phone.conf

Add a second client

cd /etc/wireguard
umask 077
wg genkey | tee laptop.key | wg pubkey > laptop.pub
printf '\n[Peer]\nPublicKey = %s\nAllowedIPs = 10.8.0.3/32, fd42:42:42::3/128\n' "$(cat laptop.pub)" >> wg0.conf
systemctl reload wg-quick@wg0

Before you start

You need a KVM Linux VPS with root access, running Debian 12 / 13 or Ubuntu 22.04 / 24.04, and the WireGuard app on each device: Android, iPhone, Windows, macOS or Linux. The commands are run as root; with another account, put sudo in front of each one.

  • The tunnel network, 10.8.0.0/24, must not already be in use on your home network: if your router uses that range, pick for example 10.66.0.0/24 everywhere in the files.
  • Note the name of the VPS's network interface: ip route show default shows it after dev (eth0, ens18…). You need it for the ufw route rule.
  • In the output of the cat command, the first line is the server's private key and the second the phone's public key; cat server.pub gives the server's public key for the client profile.

Understanding the configuration

WireGuard ties each public key to a set of allowed addresses. On the server, the AllowedIPs of a [Peer] lists the client's addresses inside the tunnel: a packet coming from 10.8.0.2 is only accepted if it is signed with the phone's key. On the client, AllowedIPs = 0.0.0.0/0, ::/0 sends all traffic into the tunnel.

  • Address: the server's address inside the tunnel, in IPv4 (10.8.0.1) and in private IPv6 (fd42:42:42::1).
  • PostUp creates an nftables table, wg-nat, that masquerades the tunnel addresses behind the VPS's address; PostDown removes it on shutdown. %i is replaced with the interface name, wg0.
  • Endpoint gives the client the VPS's public IPv4 address, shown in "My services", and the UDP port.
  • PersistentKeepalive = 25 sends a packet every 25 seconds to keep the tunnel open behind a home router or a mobile network.
  • DNS sets the resolvers the phone uses while the tunnel is up.

IPv6: what you need to know

The configuration gives the tunnel private IPv6 addresses (fd42:42:42::/64) and masquerades them behind the VPS's IPv6. If the VPS has a public IPv6 address, which you can check with ip -6 addr show scope global, clients also browse over IPv6 through the tunnel. If not, they simply use IPv4, and no IPv6 request leaves the tunnel by another path.

Enabling net.ipv6.conf.all.forwarding makes Linux ignore router advertisements. If the VPS gets its IPv6 automatically rather than from a static configuration, add net.ipv6.conf.eth0.accept_ra = 2 (with your interface name) to the same file, otherwise the VPS may lose its IPv6 route.

Full VPN or private access to the VPS

With AllowedIPs = 0.0.0.0/0, ::/0, all of the client's traffic goes through the VPS. To reach only private services on the server, such as a database or an admin interface bound to 10.8.0.1, set AllowedIPs = 10.8.0.0/24 in the client profile: the rest of the traffic keeps its usual path.

To add a computer, generate a new key pair, add a [Peer] block with the next address (10.8.0.3), then reload with systemctl reload wg-quick@wg0, without disconnecting the clients already connected.

Which VPS for a WireGuard VPN?

WireGuard is very light: encryption happens in the kernel and a few MB of RAM are enough. The ACLClouds VPS Mini (2 vCPU, 8 GB of RAM, 40 GB of storage), at €4.99 per month with no commitment as of 16 November 2026, runs the VPN alongside other services. A KVM VPS is required: WireGuard needs kernel access, which shared hosting cannot provide.

Quick check

  • wg show reports a latest handshake less than two minutes old for the client.
  • From the phone, the IP address seen on the Internet is the VPS's address.
  • nft list table inet wg-nat shows both masquerade rules.
  • sysctl net.ipv4.ip_forward returns net.ipv4.ip_forward = 1.
  • The *.key files in /etc/wireguard are mode 600 and the phone's private key is no longer on the server.

Frequently asked questions

wg show shows no latest handshake. What should I check?

The client's packets are not arriving or are being rejected. Check that UDP port 51820 is open in UFW and in the panel firewall, that Endpoint holds the right IPv4 address, then that the keys match: the client must have server.pub and the server phone.pub. tcpdump -ni any udp port 51820 shows whether packets reach the VPS.

The tunnel is up but the Internet no longer responds. Why?

The VPS receives the traffic but does not forward it. Check sysctl net.ipv4.ip_forward, the wg-nat table with nft list table inet wg-nat and the ufw route rule. If Docker is installed, it blocks forwarding by default: add iptables -I DOCKER-USER -i wg0 -j ACCEPT and iptables -I DOCKER-USER -o wg0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT to the PostUp line.

On a Linux client, wg-quick up prints resolvconf: command not found. What do I do?

wg-quick needs resolvconf to apply the DNS line. Install the openresolv package or, if the machine uses systemd-resolved, create the link ln -s /usr/bin/resolvectl /usr/local/bin/resolvconf. Removing the DNS line also gets rid of the error, but DNS queries then go outside the tunnel.

Some sites hang while loading through the VPN. Why?

It is often the packet size (MTU) on a mobile network or a PPPoE connection. Add MTU = 1380 to the [Interface] section of the client profile, import it again, then try again. WireGuard uses 1420 by default.

My VPS has no public IPv6. Should I remove the fd42 addresses?

You don't have to. Without public IPv6, the client's IPv6 traffic stops at the VPS and devices use IPv4. You can also remove the IPv6 addresses from the Address and AllowedIPs lines and the ip6 rule from PostUp for an IPv4-only setup.

Does a VPN on my own VPS make me anonymous?

No. It encrypts traffic on the network you are on, such as public Wi-Fi, and websites see the VPS's address instead of yours. But that address is linked to your hosting account, and you are its only user. It is not the right tool for anonymity.