How to Set Up a VPN on Your VPS
By Harborvm6 min read
Key takeaways
- You can set up a VPN on a VPS in under 30 minutes using WireGuard, which ships in the Linux kernel since version 5.6 (March 2020).
- WireGuard listens on UDP port 51820 by default and uses key pairs, so each device gets its own private and public key.
- Running your own VPN gives you control over who connects and where your traffic exits, something a shared commercial VPN cannot offer.
- Harborvm includes a built-in WireGuard VPN and a private network per customer, so you can skip the manual install if you prefer.
- Always keep your own copy of the server config and client keys, because Harborvm offers no backups or snapshots.
To set up a VPN on a VPS, install WireGuard, generate a key pair for the server and each client, write a short config file, open one UDP port, and turn on IP forwarding. The whole job takes well under 30 minutes on a fresh Linux VPS. This guide walks through every step with exact commands, then covers client setup, testing, and a few hardening and performance tips.
WireGuard is the tool most people reach for in 2026. It has been part of the mainline Linux kernel since version 5.6, released in March 2020, which made it the first VPN protocol built directly into the kernel. That means less code to trust and good performance on even a small server.
Why run your own VPN instead of a commercial service
A self-hosted VPN gives you control. You decide who connects, where your traffic exits, and how long logs live. A commercial service decides all of that for you. Demand for VPNs is real and global: an Iranian Parliament Research Center survey found that 81% of Iranian internet users rely on a VPN to reach blocked sites (Iran International, 2025).
Here is how the two approaches differ in practice:
- Exit IP. Your own VPN gives you a clean, dedicated exit through your server. A commercial VPN shares one IP among many users, which can trip rate limits and CAPTCHAs.
- Trust. You run the box, so you know the logging policy. With a paid service you take their word for it.
- Cost. One small VPS can serve a household or a small team for a flat monthly fee, with no per-device pricing.
The trade-off is honest: you become the administrator. You patch the server, guard the keys, and fix problems yourself. If that suits you, read on.

Set up a VPN on a VPS: install WireGuard step by step
You can install WireGuard on any supported Linux VPS in a few commands. The package is in the default repositories for Ubuntu, Debian, AlmaLinux, Rocky Linux, and Fedora, all of which Harborvm installs in about a minute. Log in over SSH as root and follow these steps.
1. Install the package.
On Ubuntu or Debian:
apt update && apt install -y wireguard
On AlmaLinux, Rocky, or Fedora:
dnf install -y wireguard-tools
2. Generate the server keys.
cd /etc/wireguard
umask 077
wg genkey | tee server_private.key | wg pubkey > server_public.key
The first command creates a private key; the second derives the matching public key. Each client needs its own pair too.
3. Write the server config. Create /etc/wireguard/wg0.conf:
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <server_private.key contents>
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
WireGuard listens on UDP port 51820 by default, as shown in the official quickstart (WireGuard). The PostUp rule lets client traffic reach the internet through the server. Replace eth0 if your main interface has a different name.
4. Turn on IP forwarding so the server routes packets:
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
5. Start the tunnel and enable it at boot:
wg-quick up wg0
systemctl enable wg-quick@wg0
Run wg to confirm the interface is up. You now have a working VPN server.
Configuring clients for desktop and mobile
Each device that connects needs its own key pair and a small config that points at your server. The setup is the same idea on every platform: install the official WireGuard app, add a peer on the server, and import the matching config on the client.
On the server, add a [Peer] block to wg0.conf for each device:
[Peer]
PublicKey = <client_public.key contents>
AllowedIPs = 10.8.0.2/32
Give each client a unique address in the 10.8.0.0/24 range, then reload with wg-quick down wg0 && wg-quick up wg0.
On a desktop (Windows, macOS, or Linux), install the WireGuard app and create a tunnel with this config:
[Interface]
PrivateKey = <client_private.key contents>
Address = 10.8.0.2/24
DNS = 1.1.1.1
[Peer]
PublicKey = <server_public.key contents>
Endpoint = YOUR_SERVER_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
Setting AllowedIPs = 0.0.0.0/0 routes all traffic through the VPN. The PersistentKeepalive line keeps the link alive behind NAT.
On a phone, install the WireGuard app from the App Store or Google Play. The app reads a QR code, so you do not type keys by hand. Generate the code on the server with:
apt install -y qrencode
qrencode -t ansiutf8 < client.conf
Scan it in the app, toggle the tunnel on, and your phone is connected.
Securing and testing your VPN connection
Test the tunnel first, then lock the server down. A VPN that leaks your real IP or exposes extra services is worse than none, so spend a few minutes here.
Test it. With the tunnel on, visit any "what is my IP" page in a browser. The address shown should be your server's IP, not your home connection. Then run a DNS leak test to confirm your queries go through the VPN, not your local provider. On the server, wg show lists each peer and the time of its last handshake, which tells you the connection is live.
Harden the server. Open only the ports you use:
ufw allow 51820/udp
ufw allow 22/tcp
ufw enable
A few more rules keep things tight:
- Use SSH keys and disable password login. Harborvm sets up key-based SSH for you.
- Keep the system patched:
apt upgradeordnf upgradeon a schedule. - Store copies of
wg0.confand every key off the server. Harborvm offers no backups or snapshots, so if the VM is gone, so are your keys unless you saved them.
For a full rundown of firewall, user, and login settings, see our guide on how to secure your VPS.
Tips for performance and multi-region access
WireGuard is light, so even a 1 vCPU, 2 GB VPS handles a personal VPN with room to spare. Its small code base keeps overhead low, which is a big part of why it runs well on modest hardware. A few settings help you get the most from it.
- Pick a close region. Latency is mostly distance. Put the server near where you use it. Harborvm runs servers in America, Europe, and Asia-Pacific, so you can choose the region that sits closest to you.
- Match the MTU if speeds drop. On some networks, add
MTU = 1420to the client[Interface]block to avoid fragmentation. - Run more than one region. For access in several places, spin up a small VPS in each region and add a tunnel for each on your devices. Switch between them in the app.
- Use the private network for teams. If several of your own servers need to talk securely, Harborvm gives each customer a private network plus a built-in WireGuard VPN, so you can connect machines without exposing them to the internet.
One honest limit: do not plan to send email straight from the VPN. Outbound port 25 is blocked on Harborvm, so use a mail relay on port 587 or 465 for any sending.
Your next step is simple. Spin up a small Linux VPS, run the five install steps above, and connect one device to confirm it works. Once that first tunnel is live, adding more clients is a two-minute job.
Frequently asked questions
Do I need root access to set up a VPN on a VPS?
Yes. Installing WireGuard, editing sysctl settings and adding firewall rules all require root or sudo. On a KVM VPS like Harborvm you get full root access, so this is not a problem.
Which port does WireGuard use?
WireGuard listens on UDP port 51820 by default. You can change it in wg0.conf with the ListenPort setting. Make sure that port is forwarded and open in your firewall.
Can I run a VPN and a website on the same VPS?
Yes. WireGuard uses one UDP port, so it does not clash with a web server on ports 80 and 443. A single small VPS can comfortably handle both for personal use.
Is a self-hosted VPN safer than a commercial VPN?
It depends on your goal. A self-hosted VPN gives you control and no shared exit IP, but you are responsible for patching and keys. A commercial VPN hides you among many users on one IP.
Can I send email through my VPN on Harborvm?
Outbound port 25 is blocked on Harborvm, so direct SMTP will fail even over the VPN. Use a mail relay on port 587 or 465 instead.