Setting up a WireGuard VPN on a VPS

A personal VPN on your own VPS gives you an encrypted tunnel from your phone or laptop to a server you control. It protects your traffic on public Wi-Fi, gives you a stable IP address for logging in to your own services, and lets you reach private tools on the server without opening them to the internet.

This guide sets up WireGuard, a modern VPN that is fast, light and built into Linux. It runs on Ubuntu or Debian and takes about half an hour. It is honest about the limits too: a VPN on your own server is private from your Wi-Fi provider, but it is not anonymous, and it is not a replacement for every use of a commercial VPN service.

1 GB VPS plans in stock, plenty for a personal VPN

In stock when this page loaded. See every plan

What is in this guide

A few words you will see

TermWhat it means
VPNA virtual private network: an encrypted tunnel between your device and another network.
WireGuardA modern VPN that is part of the Linux kernel. Simple to set up and light on memory.
PeerAny device in a WireGuard network, including the server and each phone or laptop.
Key pairA private key, kept secret, and a public key, shared with the other side.
InterfaceA network connection on the server. The VPN creates one called wg0.
ForwardingLetting the server pass traffic between the VPN and the internet.
NATMaking traffic from the VPN appear to come from the server's own IP address.
HandshakeThe first exchange between two peers. If it happens, the keys and network path are working.

What a personal VPN is good for

UseGood fit?
Safer browsing on public Wi-FiYes
Reaching private tools on your server without opening portsYes, and one of the best reasons to run one
A stable IP address to allow in your own firewallsYes
Ad-blocking DNS on your phone everywhereYes, together with a DNS blocker on the server
Hiding who you are onlineNo. The server's IP address is linked to your account
Changing country oftenNot really. A commercial VPN service with many locations suits that better

VPS vs VPN explains the difference between running your own and paying for a service. Best RackNerd VPS for a VPN covers choosing a plan, and choosing a VPS location covers picking a data centre near you.

Before you start

  • A VPS running Ubuntu or Debian, secured with SSH keys and a firewall. Your first hour on a new RackNerd VPS walks through it.
  • The WireGuard app on your phone or computer. It is available for Windows, macOS, iOS, Android and Linux from the official app stores or the project's documentation.
  • Your server's public IP address.

This guide uses 10.8.0.0/24 as the VPN's private network: the server is 10.8.0.1 and your first device is 10.8.0.2. You can use these numbers as they are.

1. Install WireGuard

sudo apt update
sudo apt install wireguard qrencode

qrencode is optional. It shows a QR code so your phone can scan its config in step 7. The commands in step 5 use iptables. If iptables --version reports that it is not found, which can happen on Debian, install it with sudo apt install iptables.

2. Create the server's keys

sudo -i
cd /etc/wireguard
umask 077
wg genkey | tee server.key | wg pubkey > server.pub
cat server.key server.pub

sudo -i gives you a root shell, because this folder belongs to root. umask 077 makes sure the new key files can only be read by root. The last command shows both keys: the first line is the server's private key and the second is its public key. You will paste them into the configs. Never share the private key.

Stay in the root shell for steps 3 to 6. Type exit when you are done.

3. Find your network interface

The server needs to know which network connection leads to the internet:

ip route show default

The word after dev is your interface, often eth0 or a name like ens3. This guide uses eth0. Replace it with yours wherever it appears.

4. Write the server config

nano /etc/wireguard/wg0.conf

Put this in it, pasting the server's private key from step 2:

[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

The PostUp and PostDown lines switch NAT on when the VPN starts and off when it stops, so traffic from your devices leaves the internet from the server's address. You will add a [Peer] section for each device in step 6.

5. Turn on forwarding and the firewall

Linux does not pass traffic between networks unless you allow it. Turn forwarding on, now and after reboots:

echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/99-wireguard.conf
sysctl --system

Open WireGuard's port, and allow traffic from the VPN to go out through the server. UFW blocks forwarded traffic by default, so the second rule is needed:

ufw allow 51820/udp
ufw route allow in on wg0 out on eth0
ufw status

Keep your SSH rule. Only the VPN port needs to be added.

6. Create a config for your device

Make keys for your first device, here called phone:

wg genkey | tee phone.key | wg pubkey > phone.pub
cat phone.key phone.pub

Add the device to the server by putting this at the end of wg0.conf, pasting the phone's public key:

[Peer]
PublicKey = PHONE_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32

Now write the config the phone itself will use:

nano /etc/wireguard/phone.conf

Put this in it, pasting the phone's private key and the server's public key:

[Interface]
PrivateKey = PHONE_PRIVATE_KEY
Address = 10.8.0.2/32
DNS = 1.1.1.1

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = SERVER_IP:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
  • DNS is the DNS server your device uses while connected. 1.1.1.1 is one well-known public resolver, used here as an example. Use any you trust, or 10.8.0.1 if you run your own DNS on the server.
  • AllowedIPs = 0.0.0.0/0, ::/0 sends all your traffic through the VPN. See full tunnel or private only for the alternative.
  • PersistentKeepalive keeps the connection alive behind home and mobile routers.

The ::/0 part sends IPv6 traffic into the tunnel too. This guide does not set up IPv6 on the VPN, so IPv6 traffic is dropped rather than leaking past it, and websites fall back to IPv4.

7. Start the VPN and connect

systemctl enable --now wg-quick@wg0
wg show

The VPN now starts at every boot. To load the phone's config, show it as a QR code and scan it in the WireGuard app:

qrencode -t ansiutf8 < /etc/wireguard/phone.conf

On a computer, copy the contents of phone.conf into a new tunnel in the WireGuard app instead, over your SSH session rather than by email or chat.

Turn the tunnel on in the app. On the server, run wg show again. A "latest handshake" line under the peer means it is working. Search for "what is my IP" in your browser: it should show the server's address.

Once the device works, delete its private key from the server, as the device has its own copy: rm /etc/wireguard/phone.key /etc/wireguard/phone.conf. Then type exit to leave the root shell.

8. Add more devices

Each device needs its own keys and its own address. For a laptop, repeat step 6 with new names and the next address, 10.8.0.3, in both the server's [Peer] section and the laptop's Address line. Then reload WireGuard:

sudo systemctl restart wg-quick@wg0

Restarting drops connected devices for a moment, and they reconnect by themselves. Never share one config between devices: two devices with the same key will knock each other off.

To remove a device, for example a lost phone, delete its [Peer] section from wg0.conf and restart.

1 GB and 2 GB VPS plans in stock, for a VPN shared by a household

In stock when this page loaded. See all 7 matching plans

Full tunnel or private only

The AllowedIPs line on your device decides what goes through the VPN.

Setting on the deviceWhat goes through the VPNGood for
0.0.0.0/0, ::/0EverythingPublic Wi-Fi, a stable IP address everywhere
10.8.0.0/24Only traffic to the VPN network, including the server itselfReaching private tools on the server while browsing normally

The second option uses almost no bandwidth and does not change your normal browsing. It is a good way to reach tools you have kept private by binding them to the server's VPN address. Many people keep two tunnels in the app, one of each, and switch as needed.

Troubleshooting

SymptomLikely causeFix
No handshake in wg showPort blocked, wrong server IP, or keys swappedCheck ufw status shows 51820/udp, the Endpoint line, and that each side has the other's public key
Handshake, but no internetForwarding off, NAT missing, or wrong interface nameCheck sysctl net.ipv4.ip_forward shows 1, the interface in wg0.conf, and the ufw route rule
Handshake, websites do not load by nameDNS setting not reachableTry another DNS address in the device config
VPN does not startA typo in wg0.confRun sudo systemctl status wg-quick@wg0 and read the error
Works on Wi-Fi, not on mobile data, or the reverseSome networks block unusual UDP portsTest on another network. Some people use port 443 for UDP instead, which you set in both configs and the firewall
Two devices keep disconnectingThey share one key or addressGive each device its own keys and address
Slow speedsDistance to the server, or a busy network routeTest with a server location nearer to you

Limits

  • Not anonymous. Your server's IP address is tied to your RackNerd account. Websites see the server's address, but it is still yours.
  • Bandwidth counts. Everything you do through the VPN uses the server's monthly allowance. Streaming video uses a lot. Unmetered does not mean unlimited, and fair use applies.
  • Rules still apply. You are responsible for traffic leaving your server. RackNerd's acceptable use policy covers it, and so do the terms of the sites you visit, including streaming services' rules about location.
  • One location. Your VPN appears in one city. Changing location means another server.
  • Keys are the security. Anyone with a device's config can use your VPN. Remove lost devices straight away.
  • IPv6 is left out to keep this guide simple. The full-tunnel setting drops IPv6 traffic rather than letting it bypass the tunnel.

When to step up

WireGuard itself uses very little memory, so a small plan is enough for a household. Step up when you add other jobs alongside it, such as ad-blocking DNS, private dashboards or self-hosted apps reached over the VPN. If the server is shared by many people, bandwidth matters more than memory, so compare the monthly allowance on each plan.

2 GB and 4 GB VPS plans in stock, for a VPN plus private apps

In stock when this page loaded. See all 6 matching plans

What next

To compare what is available now, start with the cheapest RackNerd VPS guide and check the sales page.

More RackNerd guides