Security
How to set up your own WireGuard VPN on a VPS
Build a private VPN on a small VPS: server and client keys, routing, firewall rules and a phone profile.

A personal VPN is one of the best uses of a small VPS: your own exit point, no shared IP reputation, no provider reading your traffic. WireGuard is fast, modern and small enough to configure by hand. This guide uses Ubuntu or Debian on the server, the network 10.8.0.0/24 inside the tunnel, and 203.0.113.10 as the server’s public address.
1. Install WireGuard
sudo apt update
sudo apt install -y wireguard iptables qrencode
2. Create the keys
Each side has a private key, which never leaves it, and a public key, which the other side needs. Create both pairs on the server for now:
sudo -i
cd /etc/wireguard
umask 077
wg genkey | tee server.key | wg pubkey > server.pub
wg genkey | tee phone.key | wg pubkey > phone.pub
3. Let the server route traffic
echo "net.ipv4.ip_forward = 1" > /etc/sysctl.d/99-wireguard.conf
sysctl --system
Find the name of the public network interface. It is the word after dev:
ip route show default
The examples below assume eth0. Replace it if yours is different, for instance ens3.
4. Write the server configuration
Create /etc/wireguard/wg0.conf. Paste the contents of server.key and phone.pub where indicated:
[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
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostUp = iptables -A FORWARD -o wg0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -o wg0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
[Peer]
# phone
PublicKey = PHONE_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32
If you use ufw, open the port:
ufw allow 51820/udp
5. Start the tunnel
systemctl enable --now wg-quick@wg0
wg show
wg show lists the interface and its peer. The tunnel now starts at every boot.
6. Configure the first device
Still on the server, create /etc/wireguard/phone.conf with the contents of phone.key and server.pub:
[Interface]
PrivateKey = PHONE_PRIVATE_KEY
Address = 10.8.0.2/32
DNS = 1.1.1.1
[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = 203.0.113.10:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
AllowedIPs = 0.0.0.0/0, ::/0 sends all traffic through the tunnel. For a phone, show the file as a QR code and scan it with the WireGuard app:
qrencode -t ansiutf8 < phone.conf
On a computer, copy the file over a secure channel and import it in the WireGuard application, or place it in /etc/wireguard/ on Linux and run wg-quick up phone.
7. Check that it works
- Connect from the device, then run
wg showon the server: a “latest handshake” line a few seconds old means the tunnel is up. - Open a “what is my IP” page on the device. It should show the server’s address.
Adding more devices
Each device gets its own key pair and its own address: 10.8.0.3, 10.8.0.4 and so on. Add a [Peer] block for it in wg0.conf, then apply the change without dropping existing connections:
wg syncconf wg0 <(wg-quick strip wg0)
When it does not work
| Symptom | Look at |
|---|---|
| No handshake at all | UDP port 51820 closed by a firewall, a wrong Endpoint, or keys pasted in the wrong place |
| Handshake, but no internet | IP forwarding not enabled, or the wrong interface name in the PostUp lines |
| Websites by IP work, names do not | The DNS line in the client configuration |
| It worked until the server rebooted | systemctl enable wg-quick@wg0 was skipped |
Tidy up
Once the device is configured, delete its private key from the server: rm /etc/wireguard/phone.key /etc/wireguard/phone.conf. A private key belongs on one device only. And keep the server itself in shape: Secure a new Linux server in 15 minutes.
Sources and further reading
Read nextTwo guides to follow this one.
All security guides
SecurityHow to secure a new Linux server in 15 minutesThe first steps on any fresh server: updates, a non-root user, SSH keys, a firewall and automatic security patches.
Privacy and policyDMCA takedowns explained, and what DMCA-ignored hosting meansHow a DMCA notice works, why hosts suspend first and ask later, and what “DMCA-ignored” hosting does and does not cover.
Your own server, in a few minutes.
Cloud VPS from $3.99 a month: no email, no ID, crypto payment, root access in minutes.
- No KYCNo email, no ID
- No DMCANotices not acted on
- Crypto friendlyBTC, ETH, XMR, USDT, SOL
- UnmeteredOn every plan