Running your own VPN gives you an exit IP nobody else shares and no third-party logging policy to trust. Most guides pipe a big install script into bash. That works, but you end up with a config you don't understand. This guide does it by hand in about ten minutes, on Ubuntu 22.04/24.04.
What you need
- A small VPS — 1 vCPU / 1 GB RAM is plenty for personal use. Pick a location close to you for low latency.
- Root SSH access.
- The WireGuard app on your phone or laptop.
1. Install WireGuard
sudo apt update && sudo apt install -y wireguard qrencode
2. Generate keys
One key pair for the server, one per device:
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. Find your public network interface
ip -o -4 route show to default | awk '{print $5}'
It's usually eth0, ens3 or enp1s0. Use it below instead of eth0.
4. Server config
Create /etc/wireguard/wg0.conf:
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <contents of server.key>
PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE; iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE; iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT
[Peer]
# phone
PublicKey = <contents of phone.pub>
AllowedIPs = 10.8.0.2/32
The PostUp lines NAT your VPN traffic out of the server's public IP and allow forwarding while the tunnel is up.
5. Enable IP forwarding
echo 'net.ipv4.ip_forward=1' > /etc/sysctl.d/99-wireguard.conf
sysctl --system
6. Open the firewall
If you use UFW:
ufw allow 51820/udp
ufw route allow in on wg0 out on eth0
ufw allow OpenSSH
ufw enable
Also check your provider's cloud firewall, if you set one up, and allow UDP 51820 there too.
7. Start it
systemctl enable --now wg-quick@wg0
wg show
8. Client config
Create phone.conf:
[Interface]
PrivateKey = <contents of phone.key>
Address = 10.8.0.2/32
DNS = 1.1.1.1
[Peer]
PublicKey = <contents of server.pub>
Endpoint = <SERVER_PUBLIC_IP>:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
AllowedIPs = 0.0.0.0/0, ::/0 sends all traffic through the tunnel. Since this tunnel only carries IPv4, IPv6 traffic is simply blocked while connected instead of leaking around the VPN.
Show it as a QR code and scan it with the WireGuard mobile app:
qrencode -t ansiutf8 < phone.conf
9. Test
Connect, then open any "what is my IP" page or run curl ifconfig.me on a laptop client. You should see the VPS's IP. On the server, wg show should list a recent handshake.
Adding more devices
Generate another key pair, add another [Peer] with the next address (10.8.0.3/32), and reload without dropping existing connections:
wg syncconf wg0 <(wg-quick strip wg0)
A note on privacy
A self-hosted VPN hides your traffic from your local network and ISP and gives you a clean IP, but your VPS provider can see that the server exists. If you'd rather not tie it to your identity or card, pick a host that accepts crypto and doesn't require KYC.
Disclosure: I work on Kvantis, a crypto-paid VPS host. The original, shorter version of this guide is on our blog.
Top comments (0)