Setting up a WireGuard VPN server on a VPS
Updated: 18 November 2026 · Guide · VPS · Linux
A WireGuard server on a VPS needs four things: a key pair per device, a configuration file /etc/wireguard/wg0.conf, IP forwarding switched on with NAT so clients can reach the internet through the server, and an open UDP port, usually 51820. Then you bring up the interface with wg-quick and check with wg show whether a handshake takes place.
This guide follows the Debian wiki and the manual pages of wg and wg-quick. It assumes a VPS with a sudo user and ufw. On our test bench we ran through the same setup with Ubuntu 24.04, including a real tunnel with handshake and ping through the server.
Installing
sudo apt update sudo apt install wireguard
The wireguard package pulls in the tools wg and wg-quick (package wireguard-tools). The kernel module itself has been part of the kernel since Linux 5.5; on current Debian and Ubuntu versions nothing needs to be built.
Generating keys
Every device has a private key and a public key derived from it. The private key stays on the device; the public key goes into the other side's configuration. umask 077 ensures that only root can read the files:
sudo -i cd /etc/wireguard ( umask 0077 && wg genkey | tee server.key | wg pubkey > server.pub ) ( umask 0077 && wg genkey | tee client1.key | wg pubkey > client1.pub )
You can also generate the client key directly on the client, so the private key never leaves the device. To start with it's simpler this way, but delete client1.key from the server afterwards.
Looking up the network interface name
For NAT the configuration needs the name of the network interface through which the server reaches the internet. The Debian wiki writes eth0, but on many VPS it's called something else. The name appears after dev:
ip route show default
Server configuration: /etc/wireguard/wg0.conf
The tunnel network is a private network that only exists between server and clients. Pick a range your home network doesn't use; here 10.66.66.0/24. Replace eth0 with your interface name and insert the keys from the files above:
[Interface] Address = 10.66.66.1/24 ListenPort = 51820 PrivateKey = <contents of server.key> PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE [Peer] # client1 PublicKey = <contents of client1.pub> AllowedIPs = 10.66.66.2/32
PostUp and PostDown are commands that wg-quick runs after bringing the interface up and down; it replaces %i with the interface name, here wg0. MASQUERADE rewrites the clients' source address to the server's public address. If the iptables command is missing on your system, install it with sudo apt install iptables.
The Debian wiki also sets SaveConfig = true. We deliberately leave that out: according to the wg-quick manual, the option overwrites the file with the running state on shutdown, and any changes you wrote into the file by hand in the meantime are then lost.
Switching on IP forwarding
Without forwarding the client reaches the server but goes no further into the internet. Write the switch into its own file and load it:
echo 'net.ipv4.ip_forward = 1' | sudo tee /etc/sysctl.d/99-wireguard.conf sudo sysctl --system
Firewall: open the port and allow forwarding
WireGuard only speaks UDP. With ufw you open the port. Because according to its manual ufw drops forwarded traffic by default, you also need a route rule from the tunnel to the internet interface:
sudo ufw allow 51820/udp sudo ufw route allow in on wg0 out on eth0
On a VPS with an additional firewall at the provider, 51820/udp has to be open there too.
With us that's the firewall in the VPS panel.
Starting and enabling at boot
sudo systemctl enable --now wg-quick@wg0 sudo wg show
wg show lists the interface with port and public key and, below that, each peer. As long as no client has connected yet, the peer has no latest handshake line. That's not an error.
Client configuration
On the client a file with this content goes into /etc/wireguard/wg0.conf (Linux) or is imported into the device's WireGuard app:
[Interface] PrivateKey = <contents of client1.key> Address = 10.66.66.2/24 # DNS = 9.9.9.9 [Peer] PublicKey = <contents of server.pub> Endpoint = YOUR-SERVER-IP:51820 AllowedIPs = 0.0.0.0/0 PersistentKeepalive = 25
AllowedIPs = 0.0.0.0/0 sends all of the client's IPv4 traffic through the tunnel. If you only want to reach services on the server, such as a database or an admin panel that shouldn't be public, enter only 10.66.66.0/24 instead. PersistentKeepalive keeps the connection open behind NAT routers; the WireGuard site gives 25 seconds as a value that works with a wide range of firewalls. Without the entry the feature is off.
More clients
Each additional client gets its own key pair, its own tunnel address (10.66.66.3, 10.66.66.4 and so on) and its own [Peer] block on the server. After changing the server file, restart the interface:
sudo systemctl restart wg-quick@wg0
When no handshake happens
If wg show shows no latest handshake, the client isn't reaching the server or the keys don't match. Check the most common causes in this order: is 51820/udp open in ufw and in any provider firewall? Does Endpoint contain the correct public address? Is the client's public key entered in the server's [Peer] block and vice versa, and not accidentally a private one?
If there is a handshake but no connection to the internet, one of three parts is almost always missing: IP forwarding (sysctl net.ipv4.ip_forward must show 1), the NAT rule with the correct interface name, or the route rule in ufw.
sysctl net.ipv4.ip_forward sudo iptables -t nat -S POSTROUTING sudo ufw status verbose
- First: set up SSH and firewallsudo user, SSH key, ufw
- Docker on the same serverReach internal containers only through the tunnel
- nginx as a reverse proxyFor services that should be public
- See VPS plansAll tiers with real figures
- Configure your own VPSA small tier is enough for a VPN