How to Set Up WireGuard VPN on Ubuntu 24.04 for Private Access

Aybars Altınyay
Aybars Altınyay
Platform Engineer & Technical Writer
IntermediateUpdated 24 min read~30 minutes total
WireGuardUbuntu 24.04VPNSplit TunnelAllowedIPsPrivate AccessSecurity
Last reviewed:
Tutorial
How to Set Up WireGuard VPN on Ubuntu 24.04 for Private Access
rafftechnologies.com/learn

Prerequisites

  • An Ubuntu 24.04 VM with SSH sudo access and a reachable public IPv4 address or DNS endpoint, a WireGuard-capable client, UDP 51820 permitted through the protecting firewall, and a non-overlapping private VPN subnet

Reach your private services over your own WireGuard VPN.

Tested on 1 vCPU and 2 GB RAM: $8.99 a month

  1. 1Pick a Linux imageUbuntu, Debian and more, with your SSH key added.
  2. 2Choose a sizeFrom $4.99 a month. Resize when your app grows.
  3. 3Connect over SSHYour server is up in about 55 seconds.

Ubuntu, Debian and other Linux distributions with full root and SSH access, up and running in about 55 seconds.

WireGuard can give a laptop or workstation a private route to an Ubuntu 24.04 server without turning that server into a full-tunnel internet gateway. In this tutorial, you will install WireGuard, create a 10.66.66.0/24 tunnel, add one client peer, keep normal client internet traffic outside the VPN, protect the UDP listener, verify the route and handshake, and test private SSH access before changing any public administration path.

The saved test environment used a Raff Linux VM as the Ubuntu 24.04 host. The existing test note remains Ubuntu 24.04 LTS on Raff 1 vCPU / 2 GB RAM VM; Linux client for tunnel verification. This revision updates the operational guidance against current Ubuntu Server and WireGuard documentation without claiming a new end-to-end test.

This is a split-tunnel design. The client uses AllowedIPs = 10.66.66.0/24, so only traffic for the WireGuard subnet enters the tunnel. There is no IPv4 forwarding, NAT, or masquerading because the goal is private access to services on the WireGuard server itself. If you want all internet traffic to exit through the VM, use the separate full-tunnel tutorial.

WireGuard's AllowedIPs setting is especially important: on transmit it selects which peer should receive traffic for a destination; on receive it also restricts which source addresses that peer is allowed to use. Ubuntu's current WireGuard documentation describes those routing and access-control roles explicitly.

Prerequisites:

  • Ubuntu 24.04 with SSH and sudo access
  • A public IPv4 address or reachable DNS name for the WireGuard endpoint
  • A client with WireGuard support
  • UDP port 51820 reachable through the firewall protecting the VM
  • A VPN subnet that does not overlap the client or server networks

Step 1 — Verify Ubuntu and choose a non-overlapping VPN subnet

Confirm the server release and current routes:

Bash
cat /etc/os-release ip -4 addr ip -4 route

This tutorial uses:

Text
WireGuard interface: wg0 VPN subnet: 10.66.66.0/24 Server VPN address: 10.66.66.1/24 Client VPN address: 10.66.66.2/32 UDP listener: 51820

Check whether the proposed subnet already appears in the server routing table:

Bash
ip -4 route | grep '10\.66\.66\.0/24' || true

Run an equivalent route check on the client. If either side already uses 10.66.66.0/24, choose a different RFC1918 subnet and use it consistently throughout the configuration.

Verify: Ubuntu should report 24.04, the public endpoint should be known, and 10.66.66.0/24 should not conflict with an existing route on either peer.

Step 2 — Install WireGuard from Ubuntu's package repository

Install WireGuard:

Bash
sudo apt update sudo apt install -y wireguard

The official WireGuard installation page uses sudo apt install wireguard for Ubuntu, and the standard Ubuntu kernel includes WireGuard support.

Verify the userspace tools:

Bash
wg --version wg-quick --help 2>&1 | head

Load the kernel module explicitly as a diagnostic check:

Bash
sudo modprobe wireguard lsmod | grep '^wireguard' || true

A missing lsmod line after normal use is not by itself proof of failure on every kernel configuration; the functional checks later in the tutorial are what matter.

Verify: wg --version and wg-quick should run successfully without installing a third-party VPN package or repository.

Step 3 — Generate the server key pair without exposing the private key

Create the WireGuard directory and a root-only server key:

Bash
sudo install -d -m 700 /etc/wireguard sudo sh -c 'umask 077; wg genkey > /etc/wireguard/wg0.key' sudo sh -c 'wg pubkey < /etc/wireguard/wg0.key > /etc/wireguard/wg0.pub'

Set explicit permissions:

Bash
sudo chown root:root /etc/wireguard/wg0.key /etc/wireguard/wg0.pub sudo chmod 600 /etc/wireguard/wg0.key sudo chmod 644 /etc/wireguard/wg0.pub

Display only the public key:

Bash
sudo cat /etc/wireguard/wg0.pub

WireGuard's official quick start recommends creating private keys under a restrictive umask. Ubuntu's security guidance also recommends keeping private keys out of configuration text that might later be shared for troubleshooting.

Verify: /etc/wireguard/wg0.key should be mode 600; the public key may be displayed or copied, but the private key should never be printed, pasted into tickets, or committed to source control.

Step 4 — Create the server configuration for private-only access

Create /etc/wireguard/wg0.conf without embedding the server private key:

Bash
sudo tee /etc/wireguard/wg0.conf >/dev/null <<'EOF' [Interface] Address = 10.66.66.1/24 ListenPort = 51820 PostUp = wg set %i private-key /etc/wireguard/%i.key SaveConfig = false EOF sudo chown root:root /etc/wireguard/wg0.conf sudo chmod 600 /etc/wireguard/wg0.conf

This server does not need:

Text
net.ipv4.ip_forward = 1 MASQUERADE / NAT rules AllowedIPs = 0.0.0.0/0

Those belong to routed or full-tunnel designs. Here, the client only needs to reach 10.66.66.1 and any service intentionally listening on that server address.

Inspect only the non-secret configuration:

Bash
sudo cat /etc/wireguard/wg0.conf

Because the private key is loaded from /etc/wireguard/wg0.key, this output does not expose it.

Verify: The interface should use 10.66.66.1/24, listen on UDP 51820, load its private key from the protected key file, and contain no forwarding or NAT rule.

Step 5 — Allow the WireGuard UDP listener without risking SSH lockout

Check UFW state first:

Bash
sudo ufw status numbered

If UFW is already active, allow WireGuard:

Bash
sudo ufw allow 51820/udp comment 'WireGuard VPN' sudo ufw status numbered

If UFW is inactive, do not blindly enable it from a single remote SSH session. First follow the lockout-safe process in Set Up UFW Firewall on Ubuntu 24.04, verify your real SSH port is allowed, and keep a second SSH session open while testing.

If another firewall protects the VM outside Ubuntu, allow UDP 51820 there as well. WireGuard does not use TCP 51820 in this configuration.

Verify: UDP 51820 should be reachable through every firewall layer that protects the VM, while the existing SSH administration path should remain working.

Step 6 — Generate the client key pair on the client device

Generate client private keys on the client whenever possible so the private key never has to travel through the server.

On an Ubuntu or Debian client:

Bash
sudo apt update sudo apt install -y wireguard

Create a protected directory and client keys:

Bash
mkdir -p ~/.config/wireguard-raff chmod 700 ~/.config/wireguard-raff umask 077 wg genkey > ~/.config/wireguard-raff/client.key wg pubkey < ~/.config/wireguard-raff/client.key \ > ~/.config/wireguard-raff/client.pub

Display only the client public key:

Bash
cat ~/.config/wireguard-raff/client.pub

Check the private-key permissions:

Bash
stat -c '%a %n' ~/.config/wireguard-raff/client.key

Windows, macOS, iOS, and Android users can generate the peer keys in the official WireGuard application and use the same tunnel addresses shown in this guide.

Verify: The client private key should remain on the client with restrictive permissions; only the client public key should be copied to the server.

Step 7 — Add the client peer and understand AllowedIPs

On the server, paste the client public key into a temporary shell variable:

Bash
CLIENT_PUBLIC_KEY='PASTE_CLIENT_PUBLIC_KEY_HERE'

Append the peer:

Bash
sudo tee -a /etc/wireguard/wg0.conf >/dev/null <<EOF [Peer] PublicKey = ${CLIENT_PUBLIC_KEY} AllowedIPs = 10.66.66.2/32 EOF unset CLIENT_PUBLIC_KEY

On the server, AllowedIPs = 10.66.66.2/32 means two things:

  1. traffic destined for 10.66.66.2 is associated with this peer;
  2. packets received from this peer are accepted as coming from 10.66.66.2, not arbitrary tunnel addresses.

Do not assign the same tunnel address to multiple peers.

Inspect the peer block without exposing the server private key:

Bash
sudo grep -A2 '^\[Peer\]' /etc/wireguard/wg0.conf

Verify: Exactly one peer should own 10.66.66.2/32, and the placeholder public key must be replaced before starting the interface.

Step 8 — Start WireGuard and enable reboot persistence

Start the interface through systemd:

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

Check service state:

Bash
systemctl is-active wg-quick@wg0 systemctl is-enabled wg-quick@wg0

Inspect the tunnel address and runtime WireGuard state:

Bash
ip -brief addr show wg0 sudo wg show wg0 sudo ss -lunp | grep ':51820' || true

Before the client sends traffic, latest handshake may be absent. WireGuard intentionally stays quiet when there is nothing to send.

Verify: wg0 should own 10.66.66.1/24, systemd should report the service active/enabled, and WireGuard should listen on UDP 51820 with the client peer loaded.

Step 9 — Create the split-tunnel client configuration

Get the server public key:

Bash
sudo cat /etc/wireguard/wg0.pub

On the client, load its private key into a temporary variable:

Bash
CLIENT_PRIVATE_KEY="$(cat ~/.config/wireguard-raff/client.key)"

Create /etc/wireguard/raffvpn.conf, replacing SERVER_PUBLIC_KEY and SERVER_PUBLIC_IP_OR_DNS:

Bash
sudo tee /etc/wireguard/raffvpn.conf >/dev/null <<EOF [Interface] PrivateKey = ${CLIENT_PRIVATE_KEY} Address = 10.66.66.2/32 [Peer] PublicKey = SERVER_PUBLIC_KEY Endpoint = SERVER_PUBLIC_IP_OR_DNS:51820 AllowedIPs = 10.66.66.0/24 PersistentKeepalive = 25 EOF unset CLIENT_PRIVATE_KEY sudo chmod 600 /etc/wireguard/raffvpn.conf

AllowedIPs = 10.66.66.0/24 is the split-tunnel rule: only that private subnet is routed through WireGuard. It does not replace the client's normal default route.

PersistentKeepalive = 25 is useful when the client is behind NAT or a stateful firewall and needs the mapping to stay usable while idle. WireGuard documents 25 seconds as a sensible interval for that case. If the client does not need it, omit the setting rather than enabling unnecessary keepalives.

Verify: The client should use 10.66.66.2/32, point to the server's real public endpoint, route only 10.66.66.0/24 through the peer, and have a mode-600 configuration file.

Step 10 — Bring up the client and verify the split route

Start the client interface:

Bash
sudo wg-quick up raffvpn

Inspect its address and WireGuard state:

Bash
ip -brief addr show raffvpn sudo wg show raffvpn

Verify the route to the private server address:

Bash
ip route get 10.66.66.1

It should use the WireGuard interface.

Then verify that an ordinary internet destination still follows the normal client route:

Bash
ip route get 1.1.1.1

That route should not be replaced by this WireGuard tunnel.

Verify: 10.66.66.1 should route through raffvpn, while a normal internet destination should continue to use the client's pre-existing default route.

Step 11 — Verify a real handshake and bidirectional tunnel traffic

Trigger the tunnel from the client:

Bash
ping -c 4 10.66.66.1

Inspect the client:

Bash
sudo wg show raffvpn

Inspect the server:

Bash
sudo wg show wg0

A working tunnel should show a recent handshake and increasing transfer counters on both sides.

You can also watch the session live on the server while sending client traffic:

Bash
watch -n 1 sudo wg show wg0

Ubuntu's troubleshooting guidance specifically recommends checking keys, AllowedIPs, routes, interface addresses, and wg state rather than treating an interface that is merely “up” as proof of connectivity.

Verify: ping 10.66.66.1 should succeed, both peers should show a recent handshake, and receive/transmit counters should increase after the test.

Step 12 — Move administrative access onto the VPN carefully

If SSH listens on the server, allow it through the WireGuard interface before changing any public SSH rule:

Bash
sudo ufw allow in on wg0 to any port 22 proto tcp \ comment 'SSH over WireGuard'

If your SSH daemon uses a non-default port, replace 22 with the real port.

Test from the client:

Bash

Keep the existing public SSH path until this private path has been tested from a fresh session and from the client/network you will actually use operationally.

For other administrative services, prefer binding them to 127.0.0.1, the WireGuard address, or a private interface instead of 0.0.0.0 when the application supports that design. WireGuard traffic is bidirectional, so use normal host firewall rules to restrict which services VPN peers may reach.

Verify: A new SSH session to 10.66.66.1 should succeed before you consider tightening any public SSH exposure, and VPN peers should have only the service access you intentionally grant.

Step 13 — Add, reload, rotate, or revoke peers without losing control

Give every peer its own key pair and /32 tunnel address, for example:

Text
10.66.66.2/32 laptop 10.66.66.3/32 workstation 10.66.66.4/32 phone

After adding another [Peer] block to /etc/wireguard/wg0.conf, Ubuntu documents that a systemd reload can add a peer without disrupting existing connections:

Bash
sudo systemctl reload wg-quick@wg0

Use reload for peer-only changes. If you change interface addresses or routes, perform a controlled restart because reload does not recreate every wg-quick networking action.

To revoke a peer, remove its [Peer] block from the saved configuration and reload the service. To rotate a compromised peer, generate a new key pair on that peer and replace the old public key on the server.

Optional preshared keys can add another symmetric layer between a specific pair of peers, but they are not a substitute for protecting private keys. If used, generate them with wg genpsk, distribute them through a secure secret channel, and protect them like other VPN credentials.

Verify: Every peer should have a unique public key and unique tunnel IP; revoked keys should disappear from wg show after reload, and existing unrelated peers should remain connected.

Step 14 — Verify reboot recovery and clean up safely

During a safe maintenance window, reboot the server:

Bash
sudo reboot

After it returns, verify:

Bash
systemctl is-active wg-quick@wg0 ip -brief addr show wg0 sudo wg show wg0 sudo ss -lunp | grep ':51820' || true

Reconnect the client if needed and repeat:

Bash
ping -c 4 10.66.66.1 sudo wg show raffvpn

To disconnect only the client:

Bash
sudo wg-quick down raffvpn

To decommission the server tunnel:

Bash
sudo systemctl disable --now wg-quick@wg0

Remove the UDP firewall rule only after confirming no peer still depends on it. Keep or securely destroy /etc/wireguard/wg0.key according to your recovery/decommissioning policy.

Verify: After a reboot the server tunnel should recover automatically and pass the handshake/ping test; after intentional decommissioning, wg0 and its UDP listener should no longer exist.

Troubleshooting

No handshake appears

Check the server listener and firewall:

Bash
sudo systemctl status wg-quick@wg0 --no-pager sudo wg show wg0 sudo ss -lunp | grep ':51820' sudo ufw status numbered

Then verify that the client Endpoint uses the current public IP/DNS name and UDP port, and that the client/server public keys are not swapped.

A handshake appears but ping 10.66.66.1 fails

Check addresses and AllowedIPs on both peers:

Bash
ip -brief addr show wg0 sudo wg show wg0

The server peer should own 10.66.66.2/32; the client should own 10.66.66.2/32 and route 10.66.66.0/24 through the server peer.

The client loses normal internet access

Inspect the client route configuration:

Bash
ip route get 1.1.1.1

For this split-tunnel tutorial, the client peer should use:

Text
AllowedIPs = 10.66.66.0/24

Do not use 0.0.0.0/0 here. That turns the design into an IPv4 full tunnel and requires forwarding/NAT on the server.

The client is behind NAT and becomes unreachable after being idle

If the client needs to remain reachable through a NAT/stateful firewall, keep:

Text
PersistentKeepalive = 25

If the tunnel is always initiated by the client and does not need inbound reachability while idle, you can omit the keepalive.

SSH works publicly but not through WireGuard

Check the SSH listener and interface-specific firewall rule:

Bash
sudo ss -lntp | grep -E ':(22)\b' sudo ufw status numbered

Do not remove the working public SSH path until a new SSH connection to 10.66.66.1 has succeeded.

A new peer was added to the file but does not appear at runtime

For peer-only changes:

Bash
sudo systemctl reload wg-quick@wg0 sudo wg show wg0

If the change also modifies addresses or routes, schedule a controlled restart instead of assuming reload will recreate them.

Conclusion

You now have a split-tunnel WireGuard VPN on Ubuntu 24.04 that gives a client private access to the server at 10.66.66.1 while leaving normal client internet traffic on its existing route. The configuration uses per-peer public keys, unique tunnel addresses, explicit AllowedIPs, an optional NAT keepalive, UDP-only firewall exposure, reboot persistence, and a verified private SSH path.

Use Install WireGuard VPN on Ubuntu 24.04 for Full-Tunnel Access when you want the VM to become the client's IPv4 internet gateway. Use Raff VPC when the goal is private networking between cloud resources rather than a roaming client-to-server VPN.

For the architectural decision between private VPN access, a bastion, and restricted public SSH, see Bastion Host vs VPN vs Public SSH.

Deploy a Raff Ubuntu VM for a WireGuard split-tunnel endpoint. Verify private SSH before changing public administration access, and use Raff VPC for cloud-resource networking.

Tested on 1 vCPU and 2 GB RAM: $8.99 a month · 14-day money-back guarantee

Deploy Your Server
Was this article helpful?
Aybars Altınyay
Aybars Altınyay
Platform Engineer & Technical Writer

Aybars is a Platform Engineer & Technical Writer at Raff Technologies. He works on Linux VM templates, self-hosted application workflows, and security-focused deployment validation, turning tested infrastructure procedures into clear, practical tutorials for developers and teams.

Reach your private services over your own WireGuard VPN.

Tested on 1 vCPU and 2 GB RAM: $8.99 a month

Ubuntu, Debian and other Linux distributions with full root and SSH access, up and running in about 55 seconds.

14-day money-back guarantee

Frequently asked questions

How do I install WireGuard on Ubuntu 24.04?
Install the `wireguard` package with APT, generate separate server and client key pairs, create a `wg0` server configuration, add the client peer, start `wg-quick@wg0`, and verify a real handshake plus tunnel traffic.
What does `AllowedIPs` do in WireGuard?
It has routing and access-control roles. On transmit, it selects which peer receives traffic for matching destination addresses. On receive, it restricts which source tunnel addresses that peer is allowed to use.
How do I make WireGuard a split tunnel instead of a full tunnel?
Put only the private networks you want to reach in the client peer's `AllowedIPs`. This guide uses `10.66.66.0/24`, so normal internet traffic keeps using the client's existing default route.
Do I need IP forwarding or NAT for this private-access setup?
No when the client only needs services on the WireGuard server itself. Forwarding and NAT are required when the server must route traffic onward to other networks or act as the client's internet gateway.
Why is `PersistentKeepalive = 25` used?
It helps a peer behind NAT or a stateful firewall keep its mapping usable during idle periods. WireGuard documents 25 seconds as a sensible interval for that case; omit it when you do not need persistent inbound reachability.
Should WireGuard port 51820 use TCP or UDP?
UDP. This tutorial listens on `51820/udp`; there is no reason to expose TCP port 51820 for this configuration.
Can I use WireGuard to access SSH privately?
Yes. Allow SSH on the `wg0` interface and test a fresh SSH connection to `10.66.66.1` before changing any working public SSH rule. Keep a recovery path until private access is proven.
How do I add or revoke WireGuard clients without restarting everyone?
Give each peer a unique key and `/32` tunnel address. For peer-only changes, update `wg0.conf` and reload `wg-quick@wg0`; use a controlled restart when changing interface addresses or routes.

Related Articles

View all →