How to Set Up a WireGuard VPN Server on a VPS in 2026

A wireguard vpn server on a vps gives you a private tunnel to a machine you control, so traffic travels through your own server instead of an unknown network. That helps on public Wi-Fi, on a client network you do not manage, and when you need to reach a service that should not sit open to the internet. This guide walks through the setup in plain steps, needing only basic Linux commands.

You will install WireGuard, create keys, write a server config, open the firewall, add a client, and test the tunnel. Private keys are the whole security of this setup: anyone with one can pretend to be that peer, so never paste them into chat, tickets, or public repos.

WireGuard VPN server on a VPS: what you are building

WireGuard is a VPN protocol built into the Linux kernel on most modern distributions. The server listens on one UDP port for clients holding matching keys. Each device gets a key pair: a private key kept secret and a public key shared. The server config lists each client by public key and tunnel address; the client config points back by public key, endpoint, and port.

Before you start, you need a VPS running recent Ubuntu or Debian, SSH access with sudo, and a client device with the WireGuard app installed. A small server handles a personal VPN fine, but a shared team tunnel needs more headroom. If you have not picked a provider yet, a comparison of budget VPS providers can help you weigh the trade-offs.

Step 1: Update the server and install WireGuard

Log in over SSH and update the package list first, so stale package data does not cause odd failures.

  1. Run sudo apt update && sudo apt upgrade -y.
  2. Install WireGuard with sudo apt install -y wireguard.
  3. Check it with wg --version and wg-quick --help.
  4. Turn on packet forwarding: in /etc/sysctl.conf set net.ipv4.ip_forward=1, then run sudo sysctl -p.

If this server already runs other services, snapshot or back it up before changing forwarding and firewall rules. A guide to hardening a fresh Ubuntu server is a good companion read if this VPS is new.

Step 2: WireGuard key generation for server and client

WireGuard key generation uses two small commands: one makes a private key, the other turns it into a public key. Do this once for the server and once for each client, keeping each private key on its own device only.

  1. On the server, run sudo mkdir -p /etc/wireguard and sudo chmod 700 /etc/wireguard.
  2. Generate the server private key with wg genkey | sudo tee /etc/wireguard/server_private.key, then set sudo chmod 600 /etc/wireguard/server_private.key.
  3. Make the server public key with sudo cat /etc/wireguard/server_private.key | wg pubkey | sudo tee /etc/wireguard/server_public.key.
  4. On the client device, or in a temporary server folder you delete after copying, run wg genkey for the client private key and pipe it through wg pubkey for the public key.
  5. Note the two public keys and the client private key somewhere safe: each public key goes in the other side’s config.

Never send a private key over email or chat to move it to a phone. Use a config QR code in the WireGuard app, or type the key by hand. If a private key ever appears in a screenshot, a log, or a shared file, generate a fresh pair and replace it everywhere.

Step 3: WireGuard server configuration

The wireguard server configuration lives in /etc/wireguard/wg0.conf, with an Interface section for the server and a Peer section per client. Use a private tunnel range that does not clash with your home or office network, such as 10.6.0.0/24.

  1. Create /etc/wireguard/wg0.conf with sudo and set permissions with sudo chmod 600 /etc/wireguard/wg0.conf.
  2. In the Interface section, set PrivateKey to the server private key, Address to 10.6.0.1/24, ListenPort to 51820.
  3. Add a Peer section for your first client with PublicKey set to the client public key and AllowedIPs to 10.6.0.2/32.
  4. Start the tunnel with sudo wg-quick up wg0 and check it with sudo wg show.
  5. Make it start on boot with sudo systemctl enable wg-quick@wg0.

Keep the server config readable only by root, since it holds the server private key. Add one Peer section per client, and never reuse a client key pair across two devices.

Step 4: VPS firewall rules, including your provider panel

VPS firewall rules come in two layers, and beginners often fix one and forget the other: the firewall on the server itself, such as UFW, and the firewall or security group in your provider panel. WireGuard needs UDP port 51820 open in both layers, or outside clients cannot reach it at all.

  1. With UFW, run sudo ufw allow 51820/udp and sudo ufw allow OpenSSH before enabling it, to avoid locking out your own SSH session.
  2. Enable UFW with sudo ufw enable and confirm with sudo ufw status verbose.
  3. In your provider panel, add an inbound UDP port 51820 rule to this VPS firewall or security group, from your client location or anywhere if your client IP changes often.
  4. With iptables instead of UFW, add a rule accepting UDP port 51820 and save the rule set so it survives a reboot.
  5. Add forwarding rules so client traffic can leave the server; with UFW, this usually means a NAT rule masquerading tunnel-range traffic out through the public interface.

Test SSH in a second terminal window after any firewall change, before closing your first session.

Step 5: WireGuard client setup

The wireguard client setup mirrors the server config: the client needs its own private key, the server public key, the server endpoint, and its tunnel address. Most clients import a plain config file, and phone apps can scan a QR code generated from it.

  1. Create a client config with an Interface section holding the client private key and Address = 10.6.0.2/24.
  2. Add a Peer section with the server public key, Endpoint = YOUR_SERVER_IP:51820, and AllowedIPs = 0.0.0.0/0 for all traffic through the tunnel, or 10.6.0.0/24 for tunnel traffic only.
  3. Set PersistentKeepalive = 25 if the client sits behind a home router that drops idle UDP sessions.
  4. Import the file into the WireGuard app, or on a Linux client copy it to /etc/wireguard/wg0.conf and run sudo wg-quick up wg0.
  5. Replace YOUR_SERVER_IP with your VPS public IP or domain before connecting.

If you only need tunnel services, use the narrower AllowedIPs value. Full-tunnel routing is simpler but sends every request through your VPS. Developers who need port forwarding or static addresses should compare options first, since a guide to the best VPNs for developers covers split tunneling and port forwarding in more depth.

Step 6: VPN connection testing and troubleshooting

VPN connection testing starts on the server, then moves to the client.

  1. On the server, run sudo wg show and look for your client peer.
  2. Connect from the client, run wg show, and look for a recent handshake: one within a minute or two means keys and endpoint match.
  3. From the client, ping 10.6.0.1; a reply proves the tunnel passes traffic.
  4. From the client, check your public IP on a what-is-my-ip page; it should show the VPS address when full-tunnel routing is on.
  5. If no handshake appears, recheck endpoint, port, and both firewall rules, in that order.
  6. If the handshake works but nothing loads, check packet forwarding and the NAT rule.

No handshake almost always means a blocked port or a wrong endpoint; a handshake with no traffic usually means a forwarding or NAT problem. Work through that split before rebuilding keys, which will not fix a firewall rule.

When a mesh VPN fits better than a self-hosted server

A self-hosted server fits when you want one exit point you control, less so when many devices must reach each other directly or you do not want to manage firewall rules and keys by hand; then a mesh VPN handles peer discovery and NAT traversal, at the cost of depending on a coordination service. A comparison of mesh VPN options lays out those trade-offs.

Readers who know their way around a VPS can also read the existing guide to setting up WireGuard on your own VPS; this beginner version separates the two firewall layers and treats testing as its own step.

Keeping the server safe after setup

A VPN server is public-facing, so treat it like one: keep the system updated, keep SSH key-only if you can, and remove a Peer section as soon as a device is lost or a team member leaves.

After updates and reboots, run sudo wg show and confirm a client can still handshake.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.