VPS

How to Create a VPN on Your VPS with WireGuard

11 min read

How to Create a VPN on Your VPS with WireGuard

The Short Answer

A commercial VPN rents you someone else’s server and asks you to trust their no-logs promise. A WireGuard VPN on your own VPS gives you the same encrypted tunnel with no third party in the middle — and it costs whatever your server already costs. WireGuard is the modern choice: it is faster and far simpler to configure than OpenVPN, with roughly 4,000 lines of code instead of hundreds of thousands, and a working setup takes about 20 minutes. This guide installs WireGuard on Ubuntu 24.04, configures the server, and connects your first phone and laptop.

One honest caveat before you start: a self-hosted VPN hides your traffic from your local network and ISP, but it does not make you anonymous — your VPS provider can see the traffic leaving your server, and the VPN’s IP address is tied to your account. It is a privacy and security tool, not an invisibility cloak. Also check your provider’s terms: most VPS providers allow a personal VPN, but a few prohibit VPN services — running your own private VPN for personal use is different from selling VPN access, and this guide is about the former. See Free VPS for a VPN: What Works and What Gets You Banned for the free-tier picture.

What You Need

  • A VPS running Ubuntu 24.04 LTS — even the smallest plan works; WireGuard uses very little CPU and RAM. A 1 GB server handles several simultaneous users easily.
  • A non-root user with sudo access, and the server secured per How to Secure Your VPS.
  • The WireGuard app on your devices (free, available for Windows, macOS, Linux, Android and iOS).

Step 1: Install WireGuard

WireGuard is in Ubuntu’s repositories, and the kernel module ships with Ubuntu 24.04’s kernel:

sudo apt update
sudo apt install wireguard

Verify the installation:

wg --version

Step 2: Generate the Server Keys

WireGuard identifies peers by cryptographic keys, not usernames or passwords. Generate a private key for the server and derive its public key:

umask 077
wg genkey | sudo tee /etc/wireguard/server_private.key
sudo cat /etc/wireguard/server_private.key | wg pubkey | sudo tee /etc/wireguard/server_public.key

umask 077 ensures the private key file is readable only by its owner. Keep the private key on the server; you will need the public key when configuring clients. Display it for later:

sudo cat /etc/wireguard/server_public.key

Step 3: Enable IP Forwarding

For the VPN to route your traffic to the internet, the server must forward packets between the WireGuard interface and its public network interface. Enable forwarding now and permanently:

echo 'net.ipv4.ip_forward=1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

If you also want IPv6 through the tunnel, add net.ipv6.conf.all.forwarding=1 the same way. This guide uses IPv4 throughout for simplicity; IPv6 works with analogous configuration.

Step 4: Create the Server Configuration

Find your server’s public network interface name — WireGuard needs it for the NAT rule:

ip route get 1.1.1.1 | awk '{print $5}'

On most VPS images this is eth0 or ens3. Note it, then create the server configuration:

sudo nano /etc/wireguard/wg0.conf
[Interface]
Address = 10.6.0.1/24
ListenPort = 51820
PrivateKey = <SERVER_PRIVATE_KEY>
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

# Client 1 (laptop) — peer section added in Step 6

Replace <SERVER_PRIVATE_KEY> with the contents of /etc/wireguard/server_private.key and eth0 with your actual interface name from the command above. What each line does:

  • Address — the server’s address inside the VPN subnet. Clients get 10.6.0.2, 10.6.0.3, and so on.
  • ListenPort — UDP port 51820, WireGuard’s default. You can change it, but there is rarely a reason to.
  • PostUp / PostDown — firewall rules that let VPN traffic out to the internet (NAT masquerading) when the interface starts, and remove them when it stops.

Open the WireGuard port in the firewall:

sudo ufw allow 51820/udp

Note this is UDP, not TCP — WireGuard is a UDP-only protocol. Allowing the TCP port does nothing, and this is the single most common setup mistake.

Step 5: Start WireGuard

Start the interface and enable it at boot:

sudo systemctl enable --now wg-quick@wg0
sudo systemctl status wg-quick@wg0 --no-pager

Check the interface exists:

sudo wg show

At this point the server is running but has no peers — it will accept no connections until you add a client.

Step 6: Add Your First Client

Each device gets its own key pair and its own IP in the VPN subnet. Generate the client’s keys on the server (simplest for a first setup):

umask 077
wg genkey | tee /tmp/client1_private.key | wg pubkey > /tmp/client1_public.key
cat /tmp/client1_private.key
cat /tmp/client1_public.key

Add the client as a peer in the server configuration:

sudo nano /etc/wireguard/wg0.conf

Append this section at the end:

[Peer]
# Laptop
PublicKey = <CLIENT1_PUBLIC_KEY>
AllowedIPs = 10.6.0.2/32

AllowedIPs on the server side means “traffic from this peer is allowed if it comes from this address” — it is both a routing rule and an access control. Each client gets a unique /32 address: 10.6.0.2 for the first, 10.6.0.3 for the next, and so on.

Apply the change without restarting the whole interface:

sudo wg syncconf wg0 <(sudo wg-quick strip wg0)

Or simply restart the service: sudo systemctl restart wg-quick@wg0. The restart drops existing connections briefly; syncconf does not. For a first setup, either is fine.

Now create the client configuration file. On your own computer (or on the server, then transfer it), create client1.conf:

[Interface]
PrivateKey = <CLIENT1_PRIVATE_KEY>
Address = 10.6.0.2/24
DNS = 1.1.1.1, 8.8.8.8

[Peer]
PublicKey = <SERVER_PUBLIC_KEY>
Endpoint = YOUR_SERVER_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

Key points:

  • AllowedIPs = 0.0.0.0/0 on the client means “send all traffic through the VPN”. This is what you want for a privacy VPN. If you only want to reach the server’s private network (a split tunnel), use AllowedIPs = 10.6.0.0/24 instead.
  • DNS sets the resolver used while connected. 1.1.1.1 (Cloudflare) and 8.8.8.8 (Google) are common choices; without this line, your device may keep using your ISP’s DNS, leaking your browsing destinations.
  • PersistentKeepalive = 25 sends a packet every 25 seconds so NAT routers and firewalls keep the connection mapping alive — essential for mobile devices behind carrier NAT.

Transfer the file to your device securely (USB, encrypted message, or scp — never paste private keys into an unencrypted channel). On a phone, the easiest path is a QR code: install qrencode on the server and run qrencode -t ansiutf8 < client1.conf, then scan it with the WireGuard app.

Step 7: Connect and Verify

Import the configuration into the WireGuard app on your device and activate the tunnel. Then verify:

  1. Check your public IP changed. Visit ifconfig.me or a similar site — it should show your VPS’s IP address, not your home IP.
  2. Check for DNS leaks. A DNS leak test site (dnsleaktest.com, browserleak.com) should show your VPN’s DNS servers, not your ISP’s.
  3. Check the server side. On the VPS, sudo wg show displays each peer, its latest handshake time, and bytes transferred. A recent handshake (within the last couple of minutes) means the tunnel is live.

If the handshake timestamp is missing or stale, the client is not reaching the server — see Troubleshooting below.

Adding More Clients

Repeat Step 6 for each device: new key pair, new peer section with the next IP (10.6.0.3/32, 10.6.0.4/32…), new client config. Give each device its own keys — never share one client configuration across devices, because you cannot revoke one device without revoking all of them.

To revoke a device, delete its [Peer] section from wg0.conf and restart or sync the configuration. The device’s keys become useless immediately.

Hardening and Maintenance

  • Keep the server updated. WireGuard itself rarely needs attention, but the host OS does — automatic security updates are covered in How to Secure Your VPS.
  • Restrict SSH. Your VPN server is a public-facing machine; the SSH hardening steps (keys only, no password login, fail2ban) apply here as much as anywhere.
  • Watch bandwidth. A VPN routes all your traffic through the VPS, so it counts against the plan’s transfer allowance. Video streaming over the VPN consumes the allowance fast — check your provider’s included transfer (for example, Hetzner’s CX plans include 20 TB/month outbound; smaller budget plans may include 1–3 TB). See How to Monitor Your VPS for tracking usage.
  • Back up the configuration. /etc/wireguard/ is small — include it in your backup routine (see How to Back Up Your VPS). Without it, rebuilding the VPN means regenerating every client’s configuration.

Troubleshooting

“No handshake / latest handshake: never.” The client cannot reach the server. Check in order: the firewall allows UDP 51820 (sudo ufw status), the provider’s cloud firewall (if any) allows UDP 51820, the Endpoint in the client config has the correct IP and port, and the server is actually running (sudo systemctl status wg-quick@wg0). Also confirm you opened UDP, not TCP — this mistake is remarkably common.

“Handshake works but no internet access.” The tunnel is up; routing or NAT is broken. Check IP forwarding is enabled (sysctl net.ipv4.ip_forward should print 1), check the NAT rule exists (sudo iptables -t nat -L POSTROUTING -n -v should show a MASQUERADE rule for your public interface), and confirm the interface name in PostUp matches your real interface (Step 4’s ip route command). A wrong interface name silently breaks NAT.

“Some sites load, others time out.” Usually an MTU problem: the VPN adds overhead to each packet, and some networks drop the resulting oversized packets. Add MTU = 1280 to the client’s [Interface] section and reconnect. If that fixes it, you can experiment with higher values (1380, 1420) for slightly better throughput.

“DNS leaks — the leak test shows my ISP.” The client’s DNS setting is not being applied. On Linux, install resolvconf or openresolv so WireGuard can update DNS settings; on some distributions, systemd-resolved needs DNSStubListener handling. On mobile apps, the DNS field in the imported config usually just works — confirm the config actually contains the DNS = line.

“Connection drops when the phone sleeps.” Mobile operating systems aggressively suspend background network activity. In the WireGuard app, enable the “always-on” or persistent tunnel option if your OS offers it, and keep PersistentKeepalive = 25 in the client config. Some battery-optimisation settings must be disabled for the WireGuard app specifically.

“Only one client can connect at a time.” Two clients share the same AllowedIPs address on the server, or the same key pair. Each device needs its own keys and its own IP — see “Adding More Clients” above.

Frequently Asked Questions

Is a self-hosted VPN better than a commercial VPN?

It depends on the goal. For privacy from your ISP and local network, and for secure access on public Wi-Fi, a self-hosted VPN is excellent and you control the logging policy (WireGuard logs nothing by design). For bypassing geo-blocks or hiding from state-level surveillance, commercial VPNs offer many server locations and shared IPs that a single VPS cannot match. Many people use both: self-hosted for daily use, commercial for streaming regions.

How many users can a small VPS handle as a VPN?

WireGuard is extremely efficient — a 1 vCPU server can push several hundred Mbps of encrypted traffic, far more than a typical home connection. The practical limit is your plan’s bandwidth allowance and the server’s network port speed, not WireGuard itself. A family of five on a 1 GB VPS is comfortable.

Does a VPN slow down my internet?

Slightly. Encryption adds a few percent of overhead, and routing through a distant server adds latency — a server in the same country typically adds 5–20 ms. Choose a VPS region near you for the best experience; if your audience is in Asia, a Singapore server beats a German one. See Cheap VPS in Asia for regional options.

Can I use the same VPS as a VPN and a web server?

Yes. WireGuard uses UDP port 51820 and negligible resources, so it coexists happily with Nginx, WordPress, or Docker on the same server. Just keep the services separated in your firewall rules and be aware that heavy VPN traffic shares the server’s bandwidth allowance with the website.

What is the difference between WireGuard and OpenVPN?

WireGuard is newer, simpler (about 4,000 lines of code versus OpenVPN’s ~100,000+ with dependencies), faster in most benchmarks, and easier to configure — its entire configuration is the short file in this guide. OpenVPN is older, more widely supported by legacy systems, and offers TCP mode (useful on networks that block UDP). For a new personal VPN in 2026, WireGuard is the default choice.

Will my VPS provider know I am running a VPN?

They can see encrypted UDP traffic on port 51820, so yes, in a technical sense. Most providers allow personal VPN use — it is a normal server workload. What some providers prohibit is operating a commercial VPN service or using the VPN for abuse (spam, attacks, copyright infringement). Keep it personal, keep it legal, and check the provider’s acceptable-use policy if unsure.

The Bottom Line

WireGuard on your own VPS is the best privacy-per-dollar deal in self-hosting: twenty minutes of setup, no monthly VPN subscription, no third party holding your logs, and performance limited only by your server’s network. Generate the keys, write the two short config files, open UDP 51820, and verify the handshake — then enjoy an encrypted tunnel you actually control.

Leave a Reply

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