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
- 1 GB KVM VPS Special1 CPU · 1 GB RAM · 20 GB disk · 3 TB bandwidth · New York, Los Angeles, Dallas$21.99/yrabout $1.83/moView at RackNerd
- Storage VPS Special - 100GB Storage1 CPU · 1 GB RAM · 100 GB disk · 10 TB bandwidth · Los Angeles, Dallas, New York$35.00/yrabout $2.92/moView at RackNerd
- Ryzen-1GB (Linux)1 CPU · 1 GB RAM · 15 GB disk · 1 TB bandwidth · New York, Chicago, Seattle$194.29/yrabout $16.19/moView at RackNerd
- KVM-1GB2 CPU · 1 GB RAM · 50 GB disk · 1 TB bandwidth · Los Angeles, Dallas, New York$215.88/yrabout $17.99/moView at RackNerd
In stock when this page loaded. See every plan
What is in this guide
- A few words you will see
- What a personal VPN is good for
- Before you start
- 1. Install WireGuard
- 2. Create the server's keys
- 3. Find your network interface
- 4. Write the server config
- 5. Turn on forwarding and the firewall
- 6. Create a config for your device
- 7. Start the VPN and connect
- 8. Add more devices
- Full tunnel or private only
- Troubleshooting
- Limits
- When to step up
- What next
A few words you will see
| Term | What it means |
|---|---|
| VPN | A virtual private network: an encrypted tunnel between your device and another network. |
| WireGuard | A modern VPN that is part of the Linux kernel. Simple to set up and light on memory. |
| Peer | Any device in a WireGuard network, including the server and each phone or laptop. |
| Key pair | A private key, kept secret, and a public key, shared with the other side. |
| Interface | A network connection on the server. The VPN creates one called wg0. |
| Forwarding | Letting the server pass traffic between the VPN and the internet. |
| NAT | Making traffic from the VPN appear to come from the server's own IP address. |
| Handshake | The first exchange between two peers. If it happens, the keys and network path are working. |
What a personal VPN is good for
| Use | Good fit? |
|---|---|
| Safer browsing on public Wi-Fi | Yes |
| Reaching private tools on your server without opening ports | Yes, and one of the best reasons to run one |
| A stable IP address to allow in your own firewalls | Yes |
| Ad-blocking DNS on your phone everywhere | Yes, together with a DNS blocker on the server |
| Hiding who you are online | No. The server's IP address is linked to your account |
| Changing country often | Not 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
DNSis the DNS server your device uses while connected.1.1.1.1is one well-known public resolver, used here as an example. Use any you trust, or10.8.0.1if you run your own DNS on the server.AllowedIPs = 0.0.0.0/0, ::/0sends all your traffic through the VPN. See full tunnel or private only for the alternative.PersistentKeepalivekeeps 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
- 1 GB KVM VPS Special1 CPU · 1 GB RAM · 20 GB disk · 3 TB bandwidth · New York, Los Angeles, Dallas$21.99/yrabout $1.83/moView at RackNerd
- Storage VPS Special - 100GB Storage1 CPU · 1 GB RAM · 100 GB disk · 10 TB bandwidth · Los Angeles, Dallas, New York$35.00/yrabout $2.92/moView at RackNerd
- 2 GB KVM VPS Special2 CPU · 2 GB RAM · 35 GB disk · 5 TB bandwidth · New York, Los Angeles, Dallas$35.99/yrabout $3.00/moView at RackNerd
- Ryzen-1GB (Linux)1 CPU · 1 GB RAM · 15 GB disk · 1 TB bandwidth · New York, Chicago, Seattle$194.29/yrabout $16.19/moView at RackNerd
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 device | What goes through the VPN | Good for |
|---|---|---|
0.0.0.0/0, ::/0 | Everything | Public Wi-Fi, a stable IP address everywhere |
10.8.0.0/24 | Only traffic to the VPN network, including the server itself | Reaching 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
| Symptom | Likely cause | Fix |
|---|---|---|
No handshake in wg show | Port blocked, wrong server IP, or keys swapped | Check ufw status shows 51820/udp, the Endpoint line, and that each side has the other's public key |
| Handshake, but no internet | Forwarding off, NAT missing, or wrong interface name | Check sysctl net.ipv4.ip_forward shows 1, the interface in wg0.conf, and the ufw route rule |
| Handshake, websites do not load by name | DNS setting not reachable | Try another DNS address in the device config |
| VPN does not start | A typo in wg0.conf | Run sudo systemctl status wg-quick@wg0 and read the error |
| Works on Wi-Fi, not on mobile data, or the reverse | Some networks block unusual UDP ports | Test on another network. Some people use port 443 for UDP instead, which you set in both configs and the firewall |
| Two devices keep disconnecting | They share one key or address | Give each device its own keys and address |
| Slow speeds | Distance to the server, or a busy network route | Test 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
- 2 GB KVM VPS Special2 CPU · 2 GB RAM · 35 GB disk · 5 TB bandwidth · New York, Los Angeles, Dallas$35.99/yrabout $3.00/moView at RackNerd
- 4 GB KVM VPS Special3 CPU · 4 GB RAM · 60 GB disk · 7 TB bandwidth · New York, Los Angeles, Dallas$59.99/yrabout $5.00/moView at RackNerd
- Ryzen-2GB (Linux)2 CPU · 2 GB RAM · 20 GB disk · 2 TB bandwidth · New York, Chicago, Seattle$222.37/yrabout $18.53/moView at RackNerd
- KVM-2GB3 CPU · 2 GB RAM · 75 GB disk · 2 TB bandwidth · Los Angeles, Dallas, New York$247.08/yrabout $20.59/moView at RackNerd
In stock when this page loaded. See all 6 matching plans
What next
- Add ad-blocking DNS for your devices: see idea 4 in what you can do with a 512 MB VPS.
- Reach a home server from outside: see home server or VPS.
- Keep private apps behind the VPN: self-hosted apps you can run on a VPS.
To compare what is available now, start with the cheapest RackNerd VPS guide and check the sales page.