
Running your own VPN server on an Ubuntu VPS gives you a private, encrypted tunnel for public Wi-Fi, a fixed IP address for whitelisting, and secure access to servers on a private network. WireGuard is the best choice for most people today: it is built into the Linux kernel, fast, and configured in a few short files. This guide sets up a WireGuard VPN server on Ubuntu 22.04, 24.04 or 26.04, adds a client for Windows, macOS, Linux or a phone, and covers firewall, routing and troubleshooting. OpenVPN is covered briefly at the end for setups that need it.
Quick answerInstall wireguard, generate a key pair for the server and each client, create /etc/wireguard/wg0.conf with the server's private key and each client's public key, enable IP forwarding and NAT, open UDP port 51820, and start it with sudo systemctl enable --now wg-quick@wg0.
Use your VPN in line with the law and your provider's terms of service.
Step 1: Update the server and install WireGuard
sudo apt update && sudo apt upgrade -y
sudo apt install wireguard qrencode -y
qrencode is optional; it creates QR codes to import client settings on phones.
Step 2: Generate server keys
sudo -i
cd /etc/wireguard
umask 077
wg genkey | tee server.key | wg pubkey > server.pub
umask 077 ensures the key files are readable only by root.
Step 3: Find your network interface
ip route list default
Note the interface after dev, for example eth0 or ens3. You need it for the NAT rule below.
Step 4: Create the server configuration
Create /etc/wireguard/wg0.conf (replace SERVER_PRIVATE_KEY with the contents of server.key and eth0 with your interface):
[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
PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
The VPN uses the private subnet 10.8.0.0/24. The PostUp and PostDown rules share the server's internet connection with VPN clients (NAT).
Step 5: Enable IP forwarding
echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl --system
Step 6: Open the firewall
With UFW:
sudo ufw allow OpenSSH
sudo ufw allow 51820/udp
sudo ufw route allow in on wg0 out on eth0
sudo ufw enable
The route allow rule lets traffic from VPN clients pass through to the internet. If your provider has a cloud firewall, allow UDP 51820 there too.
Step 7: Add a client
Generate a key pair for the client (on the server, or on the client device itself):
cd /etc/wireguard
wg genkey | tee client1.key | wg pubkey > client1.pub
Add the client to the end of wg0.conf:
[Peer]
PublicKey = CLIENT1_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32
Create the client's configuration file, client1.conf:
[Interface]
PrivateKey = CLIENT1_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 sends all the client's traffic through the VPN. To use the VPN only for your private network, set it to 10.8.0.0/24 instead. Give each additional client its own key pair and address (10.8.0.3, 10.8.0.4 and so on).
Step 8: Start the VPN
sudo systemctl enable --now wg-quick@wg0
sudo wg show
After adding peers later, apply changes with sudo systemctl restart wg-quick@wg0.
Step 9: Connect your devices
- Windows and macOS: install the official WireGuard app, choose Import tunnel(s) from file and select
client1.conf, then click Activate. Copy the file from the server securely withscp. - Android and iOS: install the WireGuard app and scan a QR code generated on the server:
qrencode -t ansiutf8 < client1.conf. - Linux: copy the file to
/etc/wireguard/wg0.confon the client and runsudo wg-quick up wg0.
Check it works by visiting a "what is my IP" page; it should show the server's IP. On the server, sudo wg show lists each peer's latest handshake and data transferred.
Troubleshooting
- No handshake: UDP 51820 is blocked, the Endpoint IP is wrong, or the public keys are swapped. Each side needs the other side's public key.
- Connected but no internet: check IP forwarding (
sysctl net.ipv4.ip_forward), the NAT rule's interface name, and the UFW route rule. - Websites do not load by name: set a
DNSline in the client config.
What about OpenVPN?
OpenVPN is older and more complex to set up (it needs a certificate authority, built with Easy-RSA), but it can run over TCP port 443, which helps on networks that block UDP or VPN traffic. It is available with sudo apt install openvpn easy-rsa. For most new setups, WireGuard is faster and simpler.
Frequently asked questions
Which VPN is best for an Ubuntu server?
WireGuard, for its speed, simplicity and kernel integration. Use OpenVPN if you need TCP mode or compatibility with existing OpenVPN clients.
How many clients can a WireGuard server handle?
Dozens on a small VPS. Each client needs its own key pair and VPN IP address.
What port does WireGuard use?
Whatever you set in ListenPort; 51820/UDP is the conventional default.
Do I need a VPS with a dedicated IP?
Yes, a public IPv4 address that clients can reach on UDP is required.


