Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

WireGuard: What It Is and How to Set Up Your Own VPN Server on Ubuntu

WireGuard is a small, fast VPN built into Linux. How it works, and a complete setup of your own server on Ubuntu with one client, in about fifteen minutes.

4 min readBy Swati

Reviewed by the AmazingRDP technical team

Setting up WireGuard

Your own VPN needs a server with a public address and root access: a Linux VPS. See Linux VPS from $6.99

WireGuard is a VPN protocol and program that became part of the Linux kernel in 2020. It does one thing, an encrypted tunnel between machines, with a fraction of the code of older VPN software, modern cryptography with no options to get wrong, and speeds close to an unencrypted connection. That combination made it the default choice for new VPN setups within a few years, and most commercial VPN services now offer it.

How it works, in four sentences

  • Every machine has a key pair; machines identify each other by public key, as with SSH.
  • Each side lists its peers and the addresses that may come from each one.
  • Traffic travels over a single UDP port, 51820 by default.
  • There is no connection to "establish": a peer that has nothing to send is silent, and roaming between Wi-Fi and mobile data just works.
WireGuardOpenVPNIPsec / IKEv2
SpeedVery highModerateHigh
ConfigurationA few linesLong; certificatesComplex
Passes strict firewallsUDP only; can be blockedCan use TCP 443Often blocked
Built intoLinux; apps for everything elseNeeds a clientWindows, macOS, iOS, Android

What you need

  • A server with Ubuntu 22.04 or later, a public IP address and root access. A small VPS is plenty.
  • A client device: phone or computer, with the free WireGuard app.

VPN software may run on our VPS and dedicated plans; it is not possible on shared remote desktops.

1. Install and create the server's keys

sudo apt update && sudo apt install wireguard
cd /etc/wireguard
umask 077
wg genkey | sudo tee server.key | wg pubkey | sudo tee server.pub

2. Create keys for the first client

wg genkey | sudo tee phone.key | wg pubkey | sudo tee phone.pub

Generating the client's key on the server is the simple way. The stricter way is to create it on the device and only bring the public key over.

3. Write the server configuration

Find the name of the server's network card with ip -br addr (often eth0 or ens3). Create /etc/wireguard/wg0.conf:

[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = CONTENT-OF-server.key
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
# phone
PublicKey = CONTENT-OF-phone.pub
AllowedIPs = 10.8.0.2/32

Replace eth0 with your card's name. The two iptables lines let VPN clients reach the internet through the server.

4. Allow 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

If ufw is active, also set DEFAULT_FORWARD_POLICY="ACCEPT" in /etc/default/ufw and reload it; otherwise clients connect but reach nothing. See the Ubuntu firewall.

5. Start it

sudo systemctl enable --now wg-quick@wg0
sudo wg show

6. The client configuration

Create a file phone.conf:

[Interface]
PrivateKey = CONTENT-OF-phone.key
Address = 10.8.0.2/24
DNS = 1.1.1.1
[Peer]
PublicKey = CONTENT-OF-server.pub
Endpoint = 203.0.113.10:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
LineMeaning
EndpointYour server's public address and port
AllowedIPs = 0.0.0.0/0Send all traffic through the tunnel. Use 10.8.0.0/24 instead to reach only the VPN's own network
DNSThe resolver to use while connected, so that look-ups do not leak outside the tunnel
PersistentKeepaliveKeeps the path open through home routers

For a phone, turn the file into a QR code and scan it with the WireGuard app:

sudo apt install qrencode
sudo qrencode -t ansiutf8 < phone.conf

On a computer, import the file into the WireGuard app. Then delete phone.key and phone.conf from the server; the private key belongs on the device only.

7. Test

  • Connect on the device, then run sudo wg show on the server: a "latest handshake" time and growing transfer counters mean the tunnel is up.
  • Open a "what is my IP" page on the device: it should show the server's address.

Adding more devices

Each device gets its own key pair, its own address (10.8.0.3, 10.8.0.4 …) and its own [Peer] block on the server. Never share one key between two devices. After editing the file: sudo systemctl restart wg-quick@wg0. To remove a lost device, delete its block.

Problems

SignCause
No handshakeThe UDP port is closed in a firewall, the Endpoint is wrong, or the keys are mixed up (each side needs its own private key and the other's public key)
Handshake, but no internetForwarding is off, the card's name in the PostUp line is wrong, or ufw's forward policy drops traffic
Sites by address work, names do notThe DNS line is missing or unreachable
Works on Wi-Fi, not on a company or hotel networkUDP is blocked there. WireGuard cannot disguise itself as web traffic
Slow or stalling on some connectionsAdd MTU = 1380 under [Interface] on the client

Keep it safe

  • The configuration files contain private keys; they must be readable by root only (chmod 600).
  • Keep the server updated and its SSH hardened: SSH hardening.
  • Websites will see the server's address, which leads to your hosting account. A personal VPN protects you from the network you sit in; it is not anonymity, and the usage policy applies to what passes through it.

Easier ways

Installer scripts and web interfaces such as wg-easy set all this up with a few answers; mesh services built on WireGuard, such as Tailscale, connect your devices without a server of your own. What a VPN does and does not hide is covered in understanding VPNs and how a VPN works; a commercial service is described in a look at CyberGhost. A small server is enough: Linux VPS.

Questions

What is WireGuard?

A modern VPN protocol and program, built into the Linux kernel, known for its small code base, simple configuration and high speed.

Which port does WireGuard use?

UDP port 51820 by default. Any UDP port can be chosen.

Is WireGuard better than OpenVPN?

It is faster and far simpler to set up. OpenVPN can run over TCP port 443, which helps on networks that block UDP.

How do I add a second device to WireGuard?

Give it its own key pair and address, add a [Peer] block with its public key on the server, and restart the interface.