Set up a WireGuard VPN: your own VPN server in 15 minutes
Set up a WireGuard server on Debian or Ubuntu, generate keys, connect clients on Windows, macOS, Android and iOS and tunnel all traffic or just your network.
WireGuard is a lean, fast VPN that is built right into the Linux kernel. With your own WireGuard server you can reach your services securely on the go, connect sites with each other or browse encrypted through your server on public Wi-Fi.
How it works
Every device has a key pair. The server knows the public keys of its clients and vice versa. Inside the tunnel every device gets its own address, here from the network 10.8.0.0/24:
| Device | Tunnel address |
|---|---|
| Server | 10.8.0.1 |
| Laptop | 10.8.0.2 |
| Phone | 10.8.0.3 |
Install the server
sudo apt update
sudo apt install wireguard
Generate keys (the folder belongs to root, so sudo tee writes straight into it):
wg genkey | sudo tee /etc/wireguard/server.key | wg pubkey | sudo tee /etc/wireguard/server.pub
sudo chmod 600 /etc/wireguard/server.key
Configure the server
First find the name of your network interface; you need it for forwarding to the internet:
ip route show default
The output shows the name after dev, for example eth0 or ens18.
sudo nano /etc/wireguard/wg0.conf
sudo cat /etc/wireguard/server.key shows the private key.
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = CONTENT_OF_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]
# Laptop
PublicKey = PUBLIC_KEY_OF_THE_LAPTOP
AllowedIPs = 10.8.0.2/32
Replace eth0 with your interface name. You only need the two iptables lines if clients should reach the internet through the server.
Enable IP forwarding and open the port:
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl --system
sudo ufw allow 51820/udp
WireGuard uses UDP. Open UDP port 51820 in the panel firewall as well.
Set up a client
Install the WireGuard app on the device (Windows, macOS, Android, iOS), or on Linux sudo apt install wireguard. In the app, create an empty tunnel; it generates the key pair itself. The client configuration:
[Interface]
PrivateKey = PRIVATE_KEY_OF_THE_CLIENT
Address = 10.8.0.2/32
DNS = 1.1.1.1
[Peer]
PublicKey = CONTENT_OF_server.pub
Endpoint = YOUR-SERVER-IP:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
Enter the client's public key on the server in the [Peer] block.
All traffic or just your network?
AllowedIPs on the client decides what goes through the tunnel:
| Setting | Effect |
|---|---|
0.0.0.0/0, ::/0 |
Everything goes through the server (safe browsing on foreign Wi-Fi). |
10.8.0.0/24 |
Only the tunnel itself, for example to reach services on the server. Everything else goes directly to the internet. |
Start and check
sudo systemctl enable --now wg-quick@wg0
sudo wg show
wg show lists for every client when the last handshake happened and how much data has flowed. If there is no handshake, a key is usually wrong or the UDP port is closed.
More clients
One [Peer] block per device with its own address (10.8.0.3/32, 10.8.0.4/32 …), then:
sudo systemctl restart wg-quick@wg0
Never use the same key pair on two devices. If you lose a phone, simply delete its peer block.
Make services reachable only via the VPN
A nice side effect: you can restrict admin interfaces or SSH to the tunnel:
sudo ufw allow in on wg0 to any port 22 proto tcp
Only delete the general SSH rule once you are sure you can get in through the VPN, otherwise you lock yourself out.
For the community
Know your stuff? Write a guide.
Share what you know: we review every submitted guide, publish it under your name and say thank you with credit for your services.