HosterQ

How to Set Up a WireGuard VPN Server on a VPS

16 Sep, 2026 HosterQ Editör Ekibi
A laptop and a smartphone connect to a central server with a security shield icon, which in turn connects to a globe symbolizing the internet.

A WireGuard VPN server on a VPS can carry a client’s IPv4 internet traffic through the server. A working setup needs keys, routes, forwarding and firewall rules to agree; a successful handshake alone does not prove internet access works. This guide builds a dedicated personal VPN on Ubuntu 24.04 LTS or Debian 12, with one client and an IPv4 endpoint.

Check endpoint and recovery access

The server needs an internet-reachable UDP endpoint. That can be a public IPv4 address on the VPS or a correctly forwarded UDP port on an upstream NAT device; a directly assigned public address is not an absolute WireGuard requirement. You also need operating-system administration and recovery-console access. Ordinary shared web hosting generally does not provide control of kernel interfaces and routing.

Define the network and the firewall scope first

The example uses server address 10.66.66.1/24, client address 10.66.66.2/32 and UDP port 51820. Choose a VPN subnet that does not overlap the client LAN, VPS private network or other VPNs. The endpoint 203.0.113.10 is a documentation address, not a working server; replace it with your actual endpoint.

A server and three devices connected via shield icons with locks, representing a secure VPN connection conceptually.
Conceptual illustration: Plan the VPN network first.

The firewall commands below are for a dedicated VPS where you deliberately manage these IPv4 rules with iptables. If UFW, firewalld, container software or a provider agent manages the host rules, integrate the same permissions into that manager instead of layering competing rules. Do not disable or flush an existing firewall. Keep working SSH access and the provider console available.

This is an IPv4 full-tunnel example, not a dual-stack privacy guarantee. IPv6 can continue over the client’s normal connection unless separately routed through a correctly configured IPv6 tunnel or blocked by an explicit client policy. Decide which behavior you require before relying on the VPN. Untrusted clients also need additional access controls for the VPS itself and private networks reachable from it.

Install WireGuard and generate private keys locally

sudo apt update
sudo apt install wireguard iptables
wg --version
sudo install -d -m 700 /etc/wireguard
sudo sh -c 'umask 077; wg genkey > /etc/wireguard/server.key; wg pubkey < /etc/wireguard/server.key > /etc/wireguard/server.pub'

Do not rerun key generation over an existing installation: that replaces its identity. Standard supported Ubuntu/Debian kernels provide WireGuard support, but restricted containers or custom kernels may require action from the host administrator. wg --version checks the userspace tool; starting the interface later checks that the kernel can actually create it.

Keep each peer’s identity separate

Each device needs its own key pair. Generate the client key in its WireGuard application or locally on the client with umask 077; wg genkey | tee client.key | wg pubkey > client.pub. Exchange only public keys. View the server public key with sudo cat /etc/wireguard/server.pub; keep private keys and full configuration files out of chat, tickets and public repositories. The official WireGuard quick start explains this key-generation workflow.

Create the server interface and peer configuration

Run ip -4 route show default and identify the actual outbound interface. The example uses eth0; replace every occurrence if your route uses ens3 or another name. Multiple default routes or policy routing require selecting the intended egress explicitly.

Create an empty /etc/wireguard/wg0.conf owned by root with mode 600 before inserting a private key. The conditional command below stops if a file or symbolic link already exists. Do not overwrite an existing configuration or run the install part on its own after a failed check.

sudo test ! -e /etc/wireguard/wg0.conf && sudo test ! -L /etc/wireguard/wg0.conf && sudo install -o root -g root -m 600 /dev/null /etc/wireguard/wg0.conf

Only after the empty file is successfully created, open it with a trusted local root editor. Insert the server private key from its protected local file and the client’s public key; placeholders below are not usable keys.

[Interface]
Address = 10.66.66.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
PostUp = iptables -I INPUT 1 -p udp --dport 51820 -j ACCEPT; iptables -I FORWARD 1 -i %i -o eth0 -s 10.66.66.0/24 -j ACCEPT; iptables -I FORWARD 1 -i eth0 -o %i -d 10.66.66.0/24 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT; iptables -t nat -A POSTROUTING -s 10.66.66.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -D INPUT -p udp --dport 51820 -j ACCEPT; iptables -D FORWARD -i %i -o eth0 -s 10.66.66.0/24 -j ACCEPT; iptables -D FORWARD -i eth0 -o %i -d 10.66.66.0/24 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT; iptables -t nat -D POSTROUTING -s 10.66.66.0/24 -o eth0 -j MASQUERADE

[Peer]
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.66.66.2/32

The input rule permits the encrypted UDP endpoint. Forwarding rules permit traffic from the VPN subnet through the chosen egress and established return traffic. NAT is restricted to 10.66.66.0/24, rather than masquerading all host traffic. These rules do not change the entire FORWARD policy to ACCEPT, and they do not automatically grant access to services on the VPS. Provider-side firewalls must separately permit the same UDP port. The iptables manual explains chain selection, source matching and rule insertion/deletion.

The iptables extensions manual documents how MASQUERADE uses the outbound interface address and how conntrack matches the RELATED,ESTABLISHED connection states.

This example uses MASQUERADE to derive the source address from the egress interface. For a fixed egress IPv4 address, the manual recommends SNAT --to-source INTERFACE_IP_ADDRESS. If adapting the rule, change the NAT target consistently in both PostUp and PostDown. Replace the placeholder with the actual local egress interface address; with upstream NAT, it may differ from the public address seen on the internet.

After saving, verify ownership and permissions; the expected output is root root 600.

sudo stat -c '%U %G %a' /etc/wireguard/wg0.conf

%i expands to the WireGuard interface name. The wg-quick manual documents these hooks. On the server, AllowedIPs = 10.66.66.2/32 associates that destination with this peer and limits accepted inner source addresses from it. It is not a DHCP allocation. Give each additional device a unique address and key, as described in the wg configuration manual.

Enable IPv4 forwarding deliberately

sysctl net.ipv4.ip_forward
echo 'net.ipv4.ip_forward = 1' | sudo tee /etc/sysctl.d/70-wireguard-forwarding.conf
sudo sysctl -p /etc/sysctl.d/70-wireguard-forwarding.conf
sysctl net.ipv4.ip_forward

Record the original value before changing it. The final value must be 1 for this gateway design. Check for other configuration files that could override the setting at boot. The Linux kernel IP sysctl documentation defines this as forwarding packets between interfaces. Enabling it does not automatically create NAT rules or open the firewall. Changing this special kernel setting can reset related IPv4 parameters to host/router defaults; record and recheck custom IPv4 settings when applying it to an existing system.

Configure the client and choose DNS integration

For a WireGuard application that handles the DNS field, create this client tunnel:

[Interface]
PrivateKey = CLIENT_PRIVATE_KEY
Address = 10.66.66.2/32
DNS = 1.1.1.1

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

AllowedIPs = 0.0.0.0/0 makes all IPv4 destinations eligible for this peer; wg-quick or the application installs the corresponding routes. For a split tunnel, use only the intended VPN/private subnets instead. A hostname endpoint must resolve before the tunnel starts; understand the DNS dependencies before replacing the numeric endpoint.

PersistentKeepalive = 25 can preserve a NAT/firewall mapping when an otherwise idle client needs to receive traffic. It is optional, not an acceleration setting. The WireGuard quick start recommends 25 seconds as a broadly useful interval and explains that most peers do not need it.

On a Linux client, wg-quick’s DNS= integration calls resolvconf; it is not automatically interchangeable with every resolver setup. Before starting, check which resolver is in use. On an Ubuntu client with active systemd-resolved and applications using its resolver, remove the DNS= line and use these client-only interface hooks instead:

PostUp = resolvectl dns %i 1.1.1.1; resolvectl domain %i '~.'
PreDown = resolvectl revert %i

Verify this resolver arrangement with resolvectl status; the resolvectl manual documents per-link DNS servers, routing domains and reverting settings. These shell hooks are for Linux wg-quick, not a mobile application’s configuration importer. Do not blindly install a second resolver manager to silence an error. DNS configuration and an IPv4 route still do not prevent IPv6 traffic from taking another path.

Start the interface and verify each layer

sudo systemctl start wg-quick@wg0
sudo systemctl status wg-quick@wg0 --no-pager
ip address show wg0
sudo wg show wg0

Start the client tunnel and generate traffic, then check for a recent handshake and increasing transfer counters. An idle peer may have no handshake until traffic is sent. These are observed state indicators, not proof that a requested website is reachable.

  1. Check the peer keys and current handshake. No handshake points first to the endpoint, UDP access or key pairing.
  2. Try the VPN server address. A failed ping alone is inconclusive if the host blocks ICMP.
  3. Test a known reachable public IPv4 destination, then resolve and access a domain. Separate IP reachability from name resolution.
  4. Use a trusted external address-check service to verify that the client’s visible IPv4 equals the VPS egress address. Check IPv6 separately against the policy chosen earlier.
  5. After success, enable boot startup with sudo systemctl enable wg-quick@wg0. Test a planned reboot while recovery-console access is available.

Diagnose failure without changing everything at once

Observation Next checks
No handshake Confirm the endpoint and public keys, host/provider UDP rules, and sudo ss -lun. Read sudo journalctl -u wg-quick@wg0 -n 50 --no-pager for startup failures.
Handshake, no IPv4 internet Check net.ipv4.ip_forward, client routes, the real egress interface, sudo iptables -S FORWARD and sudo iptables -t nat -S POSTROUTING.
IP works, names fail Inspect client resolver configuration and reachability of the selected DNS server.
Small requests work, larger ones stall Investigate path MTU and filtering. A temporary lower MTU is a diagnostic experiment; compare the same request and revert if it does not help.

If a hook fails partway through startup, inspect the actual rules before retrying. Some earlier commands may already have succeeded. Remove only the exact rules created by this setup; never flush the whole firewall as a repair shortcut.

Plan revocation and rollback

To revoke one client, remove its peer block from the server configuration and remove the active peer with sudo wg set wg0 peer CLIENT_PUBLIC_KEY remove. Verify it is absent. Editing only runtime state lets the old peer return on restart; editing only the file leaves it active until the running configuration is updated.

For rollback, stop the interface with sudo systemctl stop wg-quick@wg0 and disable startup if no longer wanted. Confirm the specific hook rules were removed. Delete only the forwarding file introduced here and restore the recorded previous forwarding value only if no other router/container service depends on it. Protect or retire key-bearing backups according to your retention policy.

Keep one account per administrator and one key pair per client. The Ubuntu administrator-account guide and Proxmox VM setup guide cover useful preparation for an isolated test deployment. A VPN changes traffic routing; it does not make browser accounts, cookies or all application activity anonymous.

Frequently asked questions

Is a full tunnel also a kill switch?

No. When a tunnel disconnects, normal routing may resume. If traffic must stop on VPN failure, configure and test an appropriate client firewall or application kill-switch policy, including IPv6, rather than assuming AllowedIPs provides it.

Can several devices share one peer configuration?

Use separate keys and addresses. Sharing one identity complicates endpoint roaming, simultaneous use and individual revocation.

Does a recent handshake prove DNS works?

No. Test routing and resolver behavior separately; the cryptographic handshake can succeed while either is misconfigured.

Sources

Put this guide to work

Compare the number of websites, resource requirements and who will manage the server.

Cloud server and VPS plans

Compare CPU, memory and storage options for your applications.

Dedicated server hosting

Compare physical servers by processor, memory, storage and network requirements.

Talk to sales

Leave a Reply

Your email address will not be published. Required fields are marked *