Skip to contentcloudvix
cloudvix
GuideServers & hosting

Set up a WireGuard VPN on Ubuntu 24.04

Denys DunaievskyiFounder, Cloudvix
  • 16 min read
  • Published
  • LevelIntermediate
Guides
On this page

Key takeaways

  • Each device gets its own address: this one is 10.8.0.2, the next would be 10.8.0.3.
  • It is a tool for a safer connection on a network you do not trust, not for hiding.
  • AllowedIPs = 0.0.0.0/0, ::/0 sends all traffic through the tunnel.

WireGuard is a small, fast VPN that is part of the Linux kernel. Running your own on a server in Frankfurt gives you one tunnel that you control: all your traffic, or only the traffic to your server, leaves through it. This guide installs WireGuard on Ubuntu 24.04, adds one device as a client and ends with checks that nothing leaks around the tunnel.

Before you start

You need an Ubuntu 24.04 server and a user with sudo, as in the first-hour guide, and a device to connect: a laptop with WireGuard installed, or a phone with the WireGuard app. The server listens on one UDP port, 51820, and the tunnel uses the private range 10.8.0.0/24 (the server is 10.8.0.1).

  • an Ubuntu 24.04 server and a user with sudo
  • the public IP address of the server, written here as SERVER_IP
  • a laptop or phone for the first client
  • five minutes of quiet: a mistake in the firewall step can lock you out of SSH, so keep your current session open until the end

The steps

Install WireGuard

The package brings the wg tool and wg-quick, which turns a config file into a running network interface.

On the serverbash
sudo apt update
sudo apt install -y wireguard

Create the server’s key pair

A key pair is two files: the private key stays on the server and the public key is what you give to the devices. The umask 077 makes the files readable by root only. The second command prints the public key; copy it for later, it is not secret.

On the serverbash
sudo sh -c 'umask 077 && wg genkey | tee /etc/wireguard/server.key | wg pubkey > /etc/wireguard/server.pub'
sudo cat /etc/wireguard/server.pub

Allow the server to forward traffic

Without forwarding, the server would receive your traffic and drop it. This setting is read at every boot and the second command applies it now. The answer must be net.ipv4.ip_forward = 1.

On the serverbash
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl -p /etc/sysctl.d/99-wireguard.conf

Write the server configuration

The first line finds the name of the network interface that has the default route (often eth0 or ens3) and the second prints it; if it is empty, stop and look at ip route. The rest writes /etc/wireguard/wg0.conf: the private key is read from its file, so it is never shown on screen, and two rules let the tunnel traffic through and translate it to the server’s own address (NAT). PostDown removes the same rules when the tunnel stops.

On the serverbash
IFACE=$(ip -o -4 route show to default | awk '{print $5; exit}')
echo "$IFACE"
sudo tee /etc/wireguard/wg0.conf > /dev/null <<EOF
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = $(sudo cat /etc/wireguard/server.key)
PostUp = iptables -I FORWARD 1 -i wg0 -j ACCEPT; iptables -I FORWARD 1 -o wg0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT; iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o $IFACE -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT; iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o $IFACE -j MASQUERADE
EOF
sudo chmod 600 /etc/wireguard/wg0.conf
Note

The iptables command on Ubuntu 24.04 is the nftables version and works with ufw. If you later flush or rewrite the firewall by hand, restart the tunnel with sudo systemctl restart wg-quick@wg0 so that these rules are added again.

Open the port in the firewall

If ufw is active, it must let the UDP port through. The status must show 51820/udp and the rule for SSH that you set up earlier. If it says inactive, there is nothing to open.

On the serverbash
sudo ufw allow 51820/udp
sudo ufw status
Warning

Never enable ufw in this step if you have not allowed SSH first: sudo ufw allow OpenSSH. Check the status in a second window before you close the first one.

Start the tunnel

enable --now starts the interface and also starts it at every boot. wg show lists the interface and its public key; it has no peers yet.

On the serverbash
sudo systemctl enable --now wg-quick@wg0
sudo wg show

Create a key pair on your device

Do this on the laptop, not on the server, so the private key is made where it will stay. It needs the wireguard-tools package; on a phone, the WireGuard app makes the pair when you add an empty tunnel and shows the public key. Copy the public key line.

On your computerbash
umask 077
wg genkey | tee client.key | wg pubkey

Add the device to the server

Replace CLIENT_PUBLIC_KEY with the line you copied. Each device gets its own address: this one is 10.8.0.2, the next would be 10.8.0.3. The restart briefly drops connected devices.

On the serverbash
sudo tee -a /etc/wireguard/wg0.conf > /dev/null <<'EOF'

[Peer]
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32
EOF
sudo systemctl restart wg-quick@wg0
sudo wg show

Write the client configuration

On the laptop, save this as the file named below; the name of the file becomes the name of the interface. Replace the three words in capitals: the private key from client.key, the public key from /etc/wireguard/server.pub on the server and the server’s address. AllowedIPs = 0.0.0.0/0, ::/0 sends all traffic through the tunnel. To send only the traffic to the server’s private range, use 10.8.0.0/24 instead. DNS is the name server used while the tunnel is up; any resolver you trust will do. The path in the box is the Linux one; on macOS, Windows or a phone, import the same text in the WireGuard app (add an empty tunnel, paste, name it wg-cloudvix) instead of saving the file.

On your computer · /etc/wireguard/wg-cloudvix.confini
[Interface]
PrivateKey = CLIENT_PRIVATE_KEY
Address = 10.8.0.2/32
DNS = 9.9.9.9

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = SERVER_IP:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
Warning

The private key lives only in this file. Do not paste it into a chat or a ticket, and do not copy the file to the server.

Connect and look for leaks

After up, wg show must list a latest handshake of a few seconds ago. The first curl must print SERVER_IP. The second asks for an IPv6 address: it must fail or time out, not print your home address. Switch off with sudo wg-quick down wg-cloudvix. In the apps, the switch of the tunnel does the same as up and down.

On your computerbash
sudo wg-quick up wg-cloudvix
sudo wg show
curl -4 https://ifconfig.me
curl -6 --max-time 5 https://ifconfig.me

Remove a device

Run this when a laptop is lost or sold. It takes effect at once. Also delete the matching [Peer] block from /etc/wireguard/wg0.conf, or the device returns at the next restart.

On the serverbash
sudo wg set wg0 peer CLIENT_PUBLIC_KEY remove

If something goes wrong

wg show shows no handshake

Either the packets do not arrive or the keys do not match. On the server, check that the firewall allows 51820/udp and that wg show lists your device with the allowed address you expect. On the device, check that Endpoint has the right address and that you pasted the server’s public key, not your own.

The handshake works but no page loads

The server is not forwarding. Check sysctl net.ipv4.ip_forward (it must be 1) and that the interface name in the PostUp line is the one the default route uses: ip -o -4 route show to default. Restart the tunnel after you fix the file.

wg-quick@wg0 does not start

Read the reason with sudo journalctl -u wg-quick@wg0 -n 20 --no-pager. The usual causes are a typo in wg0.conf, a private key that was cut when pasted, or a file that is readable by other users, which wg-quick warns about.

resolvconf: command not found on the laptop

wg-quick needs a helper to set the name server when the config has a DNS line. Install openresolv, or delete the DNS line and set the name server in your network settings.

Questions people ask

Does a VPN on my own server make me anonymous?

No. Websites see the server’s address instead of yours, and nobody on your local network sees which sites you visit. But the server’s provider and the server itself still see where the traffic goes, and the address belongs to your account. It is a tool for a safer connection on a network you do not trust, not for hiding.

Do I need a new key pair for every device?

Yes. One pair and one tunnel address per device, so you can remove a single device without touching the others. Never copy a private key from one device to another.

Can I use the tunnel only to reach the server, not for all traffic?

Yes. Put 10.8.0.0/24 instead of 0.0.0.0/0, ::/0 in AllowedIPs on the device. Then only traffic to the tunnel’s private range goes through it and everything else uses your normal connection.

Where to go next

Keep a copy of /etc/wireguard off the server (it holds the server’s private key, so store it as carefully as a password) and read the security checklist for the rest of the server.

About the author

Denys Dunaievskyi

Founder, Cloudvix

Runs Cloudvix and is responsible for the guides published here.

Related guides

Your server is a few clicks away.

Choose the plan and the system in one order form; the location too, when there is more than one.

Create server