Commercial VPN apps are convenient, but you're trusting a company you can't inspect with all of your traffic. Running your own VPN on a VPS gives you a private, encrypted tunnel you control completely, whether you want safer browsing on public Wi-Fi, a fixed IP address for accessing admin panels, or a secure way into servers that shouldn't be exposed to the internet.
WireGuard is the easiest way to do it. It's built into the Linux kernel, uses modern cryptography, and its configuration files are short enough to read in a minute. This guide shows how to set up a WireGuard VPN server on an Ubuntu VPS and connect your devices to it.
Before you begin
You'll need:
- A VPS running Ubuntu 22.04 or 24.04 with a public IPv4 address
- A sudo user and SSH access (see how to set up SSH keys on a Linux VPS)
- The WireGuard app on each device you want to connect (available for Windows, macOS, Linux, iOS and Android)
A VPN server uses very few resources, so even the smallest VPS plan is usually fine for personal use. Bandwidth is the thing to watch, since every byte you browse passes through the server.
Step 1: Install WireGuard
sudo apt update
sudo apt install -y wireguard
This installs the wg and wg-quick tools. The kernel module is already included in modern Ubuntu kernels.
Step 2: Generate the server keys
WireGuard uses public and private key pairs, similar to SSH. Each side keeps its private key secret and shares its public key with the other side.
sudo -i
cd /etc/wireguard
umask 077
wg genkey | tee server_private.key | wg pubkey > server_public.key
umask 077 makes sure the key files are readable only by root. Generate a pair for your first client while you're here:
wg genkey | tee client1_private.key | wg pubkey > client1_public.key
You can print any key with cat filename. You'll need to paste them into config files shortly.
Step 3: Find your network interface name
Traffic from VPN clients has to leave the server through its main network interface. Find its name:
ip route list default
The output will look something like default via 203.0.113.1 dev eth0. The word after dev (often eth0, ens3 or enp1s0) is your interface name. Use it wherever you see eth0 below.
Step 4: Create the server config
Create /etc/wireguard/wg0.conf:
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
# client1
PublicKey = CLIENT1_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32
What each part does:
- Address gives the server an IP inside the private VPN network,
10.8.0.0/24. - ListenPort is the UDP port clients connect to. 51820 is the conventional default.
- PrivateKey is the contents of
server_private.key. - PostUp / PostDown add firewall rules when the tunnel starts and remove them when it stops. The
MASQUERADErule rewrites client traffic so it appears to come from the VPS, which is what lets clients reach the internet through the tunnel. - [Peer] describes one client.
AllowedIPs = 10.8.0.2/32means this client owns exactly that one VPN address.
Lock the file down:
chmod 600 /etc/wireguard/wg0.conf
Step 5: Enable IP forwarding
By default, Linux won't pass traffic from one interface to another. Turn forwarding on permanently:
echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/99-wireguard.conf
sysctl --system
Step 6: Open the firewall and start the tunnel
If you use UFW, allow the WireGuard port:
ufw allow 51820/udp
UFW's default policy drops forwarded packets. If clients connect but can't reach the internet, edit /etc/default/ufw, set DEFAULT_FORWARD_POLICY="ACCEPT", and run ufw reload.
Now bring the interface up and make it start at boot:
systemctl enable --now wg-quick@wg0
wg show
wg show lists the interface, its listening port and any peers. Until a client connects, the peer won't show a handshake.
Step 7: Create the client config
On your computer, create a file called client1.conf:
[Interface]
PrivateKey = CLIENT1_PRIVATE_KEY
Address = 10.8.0.2/24
DNS = 1.1.1.1
[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = YOUR_VPS_IP:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
The important lines:
- AllowedIPs = 0.0.0.0/0, ::/0 sends all traffic through the VPN. If you only want to reach the VPN network itself (for example, private admin access to your servers), use
10.8.0.0/24instead. That's called a split tunnel. - DNS sets which resolver the client uses while connected, so DNS lookups don't leak outside the tunnel.
- PersistentKeepalive = 25 sends a small packet every 25 seconds, which keeps the connection alive behind home routers and mobile networks.
Import this file into the WireGuard app on your desktop and click Activate.
Step 8: Add a phone with a QR code
Typing keys on a phone is miserable. Instead, put the client config on the server and display it as a QR code:
apt install -y qrencode
qrencode -t ansiutf8 < client1.conf
In the WireGuard mobile app, choose to add a tunnel by scanning a QR code and point the camera at your terminal. Delete the config file from the server afterward, since it contains the client's private key.
Step 9: Test the connection
With the tunnel active, visit any "what is my IP" site. It should show your VPS's IP address instead of your home or mobile IP. On the server, run wg show again; you should see a recent handshake and transfer counters for the peer.
Adding more devices
Every device gets its own key pair and its own VPN address. For a second client:
- Generate a new key pair on the server.
- Add another
[Peer]block towg0.confwith the new public key andAllowedIPs = 10.8.0.3/32. - Create a client config using the new private key and
Address = 10.8.0.3/24. - Restart the tunnel with
systemctl restart wg-quick@wg0.
You can also add a peer without restarting using wg set wg0 peer <public-key> allowed-ips 10.8.0.3/32, but remember to add it to the config file too, or it will disappear on the next restart.
To revoke a device, delete its [Peer] block and restart the service.
Troubleshooting
- No handshake at all: the UDP port is probably blocked. Check UFW, and check whether your VPS provider has a separate network firewall in its control panel.
- Handshake works but no internet: IP forwarding is off, the interface name in
PostUpis wrong, or UFW is dropping forwarded traffic. - Websites load slowly or some fail: try lowering the MTU by adding
MTU = 1380to the client's[Interface]section.
Wrapping up
A personal WireGuard VPN server takes two short config files, a forwarding setting and one firewall port. Once it's running, it uses almost no resources, reconnects instantly when you switch networks, and gives you a private tunnel that answers only to you. Keep your VPS updated, give every device its own keys, and remove peers you no longer use.