Skip to content

Tutorial

How to install WireGuard VPN on an Ubuntu VPS

Running your own WireGuard VPN on an Ubuntu 24.04 VPS solves two problems at once: you get a fast, encrypted tunnel with a dedicated static IP on untrusted Wi-Fi networks, and you can close sensitive administrative ports (SSH, databases, internal panels and RDP) so only peers on the private VPN subnet can reach them. In this tutorial you configure WireGuard with locally generated keys, a PresharedKey for post-quantum resistance, iptables NAT, UFW rules, a mobile QR code and live peer additions without dropping active connections.

On this page
  1. How the WireGuard tunnel works on a VPS
  2. Prerequisites on an Ubuntu 24.04 VPS
  3. 1. Install WireGuard and detect the public network interface
  4. 2. Enable IPv4 packet forwarding
  5. 3. Generate server and first client keys
  6. 4. Write `/etc/wireguard/wg0.conf` and the client profile
  7. 5. Open UDP 51820 in UFW and start `wg0`
  8. 6. Connect your first device (laptop or phone via QR code)
  9. 7. Add more devices without dropping active tunnels
  10. 8. Lock down admin panels, SSH and databases behind the VPN
  11. Common troubleshooting checks
  12. Next steps

All packages, commands and directives below were verified against the official WireGuard documentation (wireguard.com/quickstart), Ubuntu 24.04 LTS's wireguard-tools (1.0.20210914) package and the preinstalled WireGuard recipe used on Nuvits (wg-quick@wg0 on UDP port 51820 and subnet 10.66.66.0/24).

How the WireGuard tunnel works on a VPS

Unlike OpenVPN or legacy IPsec stacks, WireGuard runs directly inside the Linux kernel (since kernel 5.6; Ubuntu 24.04 ships with kernel 6.8), uses a fixed modern cryptographic suite (ChaCha20-Poly1305, Curve25519 and BLAKE2s), and stays silent on the wire: UDP port 51820 drops any packet not signed by an authorized peer key without replying to port scanners.

Architecture diagram of a WireGuard VPN on an Ubuntu 24.04 VPS showing UDP port 51820, private subnet 10.66.66.0/24, iptables NAT and private access to admin panels and RDP

In practice, you choose between two routing modes by changing a single AllowedIPs line on the client:

  • Full tunnel (AllowedIPs = 0.0.0.0/0, ::/0): all internet traffic from your laptop or phone exits through the VPS public IP (ideal on public Wi-Fi or when you need a static IP for third-party allowlists).
  • Split tunnel (AllowedIPs = 10.66.66.0/24): only traffic addressed to the VPS and private services goes through the VPN; regular browsing stays on your local connection with zero extra latency.

Prerequisites on an Ubuntu 24.04 VPS

  • A Linux VPS running Ubuntu 24.04 LTS (or Debian 12/13) with root or sudo SSH access.
  • Minimum sizing: WireGuard itself uses under 50 MB of RAM at idle and runs smoothly on 2 vCPU / 4 GB RAM plans. If the same VPS also hosts Docker, n8n, Evolution API or Coolify, start at 8 GB of RAM to keep comfortable headroom.
  • UDP port 51820 allowed in your VPS firewall (and in any upstream network firewall).

1. Install WireGuard and detect the public network interface

Update your apt package index and install wireguard, wireguard-tools, qrencode (to render mobile QR codes in your SSH session) and iptables:

bash
sudo apt-get update
sudo apt-get install -y --no-install-recommends wireguard wireguard-tools qrencode iptables

Next, detect the server's default public network interface (commonly eth0, ens3 or enp1s0) and public IPv4 address. Both values go into the NAT rule and client profile:

bash
PUB_IFACE=$(ip -o -4 route show to default | awk '{print $5; exit}')
PUB_IP=$(ip -o -4 addr show dev "$PUB_IFACE" scope global | awk '{split($4, a, "/"); print a[1]; exit}')
echo "Interface: $PUB_IFACE | IP: $PUB_IP"

Verify that the output prints the real external interface (not lo or docker0) and your VPS's public IP before continuing.

2. Enable IPv4 packet forwarding

For the VPS to forward traffic arriving on wg0 out to the internet (NAT/MASQUERADE), persist net.ipv4.ip_forward = 1 in a dedicated file under /etc/sysctl.d/:

bash
printf 'net.ipv4.ip_forward = 1\n' | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl --system

Verify that the kernel applied the setting:

bash
sysctl net.ipv4.ip_forward

The command should return net.ipv4.ip_forward = 1.

3. Generate server and first client keys

Create both directories with mode 700 and generate the server keypair, the first client keypair and a preshared key (PresharedKey) inside a subshell with umask 077 so private keys are never world-readable:

bash
sudo install -d -m 700 /etc/wireguard /root/wireguard
sudo bash -c '
  umask 077
  wg genkey | tee /etc/wireguard/server.key | wg pubkey > /etc/wireguard/server.pub
  wg genkey | tee /root/wireguard/laptop.key | wg pubkey > /root/wireguard/laptop.pub
  wg genpsk > /root/wireguard/laptop.psk
'

4. Write /etc/wireguard/wg0.conf and the client profile

Generate both /etc/wireguard/wg0.conf and /root/wireguard/laptop.conf using the detected interface and public IP:

bash
sudo bash -c '
  umask 077
  PUB_IFACE=$(ip -o -4 route show to default | awk "{print \$5; exit}")
  PUB_IP=$(ip -o -4 addr show dev "$PUB_IFACE" scope global | awk "{split(\$4, a, \"/\"); print a[1]; exit}")
  SERVER_KEY=$(cat /etc/wireguard/server.key)
  SERVER_PUB=$(cat /etc/wireguard/server.pub)
  CLIENT_KEY=$(cat /root/wireguard/laptop.key)
  CLIENT_PUB=$(cat /root/wireguard/laptop.pub)
  CLIENT_PSK=$(cat /root/wireguard/laptop.psk)

  cat > /etc/wireguard/wg0.conf <<EOF
[Interface]
Address = 10.66.66.1/24
ListenPort = 51820
PrivateKey = ${SERVER_KEY}
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -s 10.66.66.0/24 -o ${PUB_IFACE} -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -s 10.66.66.0/24 -o ${PUB_IFACE} -j MASQUERADE

[Peer]
# laptop
PublicKey = ${CLIENT_PUB}
PresharedKey = ${CLIENT_PSK}
AllowedIPs = 10.66.66.2/32
EOF

  cat > /root/wireguard/laptop.conf <<EOF
[Interface]
PrivateKey = ${CLIENT_KEY}
Address = 10.66.66.2/32
DNS = 1.1.1.1, 9.9.9.9

[Peer]
PublicKey = ${SERVER_PUB}
PresharedKey = ${CLIENT_PSK}
Endpoint = ${PUB_IP}:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
EOF
'

Three lines here prevent the most common WireGuard pitfalls:

  • AllowedIPs = 0.0.0.0/0, ::/0 on the client: including ::/0 keeps IPv6 traffic from leaking around a full tunnel. For a split tunnel that only reaches the VPS private subnet, change that line to AllowedIPs = 10.66.66.0/24 (and omit DNS if you want to keep your local resolver).
  • PersistentKeepalive = 25: sends a tiny keepalive packet every 25 seconds so home routers and carrier-grade NAT (CGNAT) on mobile networks do not drop the UDP mapping.
  • %i in PostUp and PostDown: wg-quick expands %i to wg0, cleanly adding and removing the FORWARD and MASQUERADE rules whenever the interface starts or stops.

5. Open UDP 51820 in UFW and start wg0

Allow UDP port 51820 (not TCP) in UFW and permit forwarded traffic from wg0 out to your public interface. Because UFW's default FORWARD policy is DROP (while return traffic is already permitted by RELATED,ESTABLISHED), allowing in on wg0 out on "$PUB_IFACE" is all you need:

bash
PUB_IFACE=$(ip -o -4 route show to default | awk '{print $5; exit}')
sudo ufw allow 51820/udp comment "WireGuard VPN"
sudo ufw route allow in on wg0 out on "$PUB_IFACE"

Now enable and start wg0 via systemd:

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

The output should list the server public key, listening port: 51820 and your first peer (allowed ips: 10.66.66.2/32).

6. Connect your first device (laptop or phone via QR code)

Import /root/wireguard/laptop.conf on one device only — to connect a second device at the same time, create a separate peer in step 7.

  • On a laptop (Linux, macOS or Windows), copy the profile over scp (if direct root SSH login is disabled, first copy it with sudo cp into your user's directory with chmod 600) and import it into the official WireGuard client:
bash
scp root@YOUR_VPS_IP:/root/wireguard/laptop.conf ./wg-nuvits.conf
  • Or, if your first device is a phone (official WireGuard app on iOS or Android), print a terminal QR code over SSH and scan it directly:
bash
sudo qrencode -t ansiutf8 < /root/wireguard/laptop.conf

Once the tunnel is active on your client, run two quick checks:

  1. On the server, run sudo wg show wg0 and confirm latest handshake: X seconds ago plus non-zero received and sent transfer counters.
  2. On the client, run ping 10.66.66.1 to test the private subnet and, if using a full tunnel, check curl -4 ifconfig.me to confirm your outbound IP now matches the VPS.

7. Add more devices without dropping active tunnels

Never reuse the same keypair on two devices at the same time: two clients sharing one PublicKey and 10.66.66.2/32 will constantly kick each other off. Create a dedicated peer with the next free IP (10.66.66.3/32, .4/32, and so on):

bash
sudo bash -c '
  umask 077
  NAME=phone
  CLIENT_IP=10.66.66.3
  PUB_IFACE=$(ip -o -4 route show to default | awk "{print \$5; exit}")
  PUB_IP=$(ip -o -4 addr show dev "$PUB_IFACE" scope global | awk "{split(\$4, a, \"/\"); print a[1]; exit}")
  SERVER_PUB=$(cat /etc/wireguard/server.pub)

  wg genkey | tee /root/wireguard/${NAME}.key | wg pubkey > /root/wireguard/${NAME}.pub
  wg genpsk > /root/wireguard/${NAME}.psk

  cat >> /etc/wireguard/wg0.conf <<EOF

[Peer]
# ${NAME}
PublicKey = $(cat /root/wireguard/${NAME}.pub)
PresharedKey = $(cat /root/wireguard/${NAME}.psk)
AllowedIPs = ${CLIENT_IP}/32
EOF

  cat > /root/wireguard/${NAME}.conf <<EOF
[Interface]
PrivateKey = $(cat /root/wireguard/${NAME}.key)
Address = ${CLIENT_IP}/32
DNS = 1.1.1.1, 9.9.9.9

[Peer]
PublicKey = ${SERVER_PUB}
PresharedKey = $(cat /root/wireguard/${NAME}.psk)
Endpoint = ${PUB_IP}:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
EOF
'

Apply the new peer live without restarting wg-quick@wg0 using wg syncconf inside a root subshell (so sudo does not close the process substitution file descriptor <(...)):

bash
sudo bash -c 'wg syncconf wg0 <(wg-quick strip wg0)'

Then render its QR code with sudo qrencode -t ansiutf8 < /root/wireguard/phone.conf.

8. Lock down admin panels, SSH and databases behind the VPN

One of the best reasons to run WireGuard on a VPS is removing administrative interfaces from the public internet. Once you have verified the tunnel from two separate devices so you cannot lock yourself out:

  • Bind internal services to 10.66.66.1: bind Grafana, Prometheus, PostgreSQL, Redis or staging panels to the wg0 IP instead of 0.0.0.0.
  • Restrict SSH to wg0 in UFW: add the VPN-only rule and verify a fresh SSH login to 10.66.66.1 in a second terminal before deleting your public port 22 rule:
bash
sudo ufw allow in on wg0 to any port 22 proto tcp comment "SSH over WireGuard only"

Always keep emergency console (VNC/serial) access available in your VPS panel as a fallback.

Common troubleshooting checks

Symptom Most likely cause How to fix
latest handshake never appears Port 51820/udp blocked or opened as TCP Run sudo ufw allow 51820/udp and verify any external firewall allows UDP
Handshake succeeds, no internet access net.ipv4.ip_forward is 0, wrong PUB_IFACE, or UFW dropping FORWARD Confirm sysctl net.ipv4.ip_forward, check wg0.conf interface name and add ufw route allow rules
Pages stall or uploads hang over TLS Client MTU too high for PPPoE or cellular links Add MTU = 1380 under [Interface] in the client config
Tunnel drops every few seconds Same .conf imported on two active devices Create a separate peer (10.66.66.3/32, .4/32) and apply with wg syncconf

Next steps