nordelo
Menu

Guides

Your own VPN with WireGuard on a VPS

Set up a fast, private VPN with WireGuard on Ubuntu or Debian and connect from your computer or phone. Step by step.

With your own VPN, your traffic travels encrypted to your own server and leaves for the internet from there. Pick the city you want your traffic to come out in. A Cloud 2 is plenty. Run the commands as root after you have connected with SSH.

1. Install WireGuard

apt update && apt install -y wireguard iptables qrencode

2. Create keys

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 the network interface name

ip route show default

The name comes after dev, for example eth0. Replace eth0 below if yours is different.

4. Write the server settings

cat > /etc/wireguard/wg0.conf <<EOF
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = $(cat server.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

[Peer]
PublicKey = $(cat phone.pub)
AllowedIPs = 10.8.0.2/32
EOF

5. Turn on forwarding and start it

echo 'net.ipv4.ip_forward=1' > /etc/sysctl.d/99-wireguard.conf
sysctl --system
systemctl enable --now wg-quick@wg0

If you use the ufw firewall, it has to let WireGuard in and pass the traffic on:

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

6. Create the settings for your phone or computer

Replace 203.0.113.10 with the server's IP address.

cat > /etc/wireguard/phone.conf <<EOF
[Interface]
PrivateKey = $(cat phone.key)
Address = 10.8.0.2/32
DNS = 1.1.1.1

[Peer]
PublicKey = $(cat server.pub)
Endpoint = 203.0.113.10:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF
qrencode -t ansiutf8 < /etc/wireguard/phone.conf

Install the WireGuard app on your phone, choose to add a tunnel from a QR code and scan the code in the terminal. On a computer, import the file phone.conf into the WireGuard app.

Check that it works

wg show

Once the phone is connected, the command shows a latest handshake. Visit a site that shows your IP address: it should now be the server's.

More devices

Each device needs its own key pair and its own address, such as 10.8.0.3. Add another [Peer] block to wg0.conf and restart with systemctl restart wg-quick@wg0.

See the VPS sizes