WireGuard is a modern, lightweight VPN solution that provides secure encrypted connections with minimal configuration. In this tutorial, you’ll learn how to install and configure a WireGuard VPN on Ubuntu 24.04, create secure server and client keys, establish a split-tunnel VPN connection, and verify that everything is working correctly.
This guide is designed for both beginners and experienced Linux users. By the end, you’ll have a functioning WireGuard VPN where clients can securely connect to a private network without routing all internet traffic through the VPN.
Prerequisites
Before starting, make sure you have:
- An Ubuntu 24.04 server with a public IP address or DNS hostname
- Administrative (sudo) access to the server
- A client device (Linux, Windows, macOS, iOS, or Android)
- Access to adjust firewall rules if required
- UDP port 51820 available
This tutorial uses the following network configuration:
- Setting ValueInterface Name 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 Port 51820
Step 1: Verify Ubuntu and Choose a VPN Subnet
Before installing WireGuard, confirm your Ubuntu version and review existing network routes to avoid subnet conflicts.
Run:
cat /etc/os-release
ip -4 addr
ip -4 route


The following command checks whether the planned VPN subnet already exists on the server:
ip -4 route | grep '10\.66\.66\.0/24' || true
Also perform a similar route check on the client device.
Important: If either the server or client already uses 10.66.66.0/24, select another private RFC1918 subnet and use it consistently throughout the configuration.
Ensure that:
- Ubuntu reports version 24.04.
- You know the server’s public endpoint.
- The VPN subnet does not overlap with existing routes.
Step 2: Install WireGuard
WireGuard is available directly from Ubuntu’s official repositories. Update packages and install WireGuard:
sudo apt update
sudo apt install -y wireguard

Verify that the WireGuard tools are available:
wg --version
wg-quick --help 2>&1 | head

As an additional diagnostic check, load the WireGuard kernel module:
sudo modprobe wireguard
lsmod | grep '^wireguard' || true

Confirm that:
- wg –version returns successfully.
- wg-quick displays help information.
- No third-party repositories are required.
Step 3: Generate the Server Key Pair Securely
WireGuard uses public-key cryptography. The private key must remain protected at all times. Create the WireGuard directory and generate the server keys:
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 ownership and permissions:
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 the public key:
sudo cat /etc/wireguard/wg0.pub

Security Note: Never display, share, email, or commit the private key to source control.
Confirm that:
- /etc/wireguard/wg0.key has permission mode 600.
- Only the public key is shared when needed.
Step 4: Create the WireGuard Server Configuration
Create the WireGuard server configuration file:
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

Secure the configuration file:
sudo chown root:root /etc/wireguard/wg0.conf
sudo chmod 600 /etc/wireguard/wg0.conf
![]()
Review the configuration:
sudo cat /etc/wireguard/wg0.conf

This setup does not require:
- IP forwarding
- NAT/MASQUERADE rules
- AllowedIPs = 0.0.0.0/0
The goal is a private VPN connection rather than a full internet tunnel.
Ensure that:
- The address is 10.66.66.1/24.
- UDP port 51820 is configured.
- No NAT or forwarding rules are present.
Step 5: Allow WireGuard Through the Firewall
First, check the current UFW status:
sudo ufw status numbered

If UFW is already enabled, allow WireGuard traffic:
sudo ufw allow 51820/udp comment 'WireGuard VPN'
sudo ufw status numbered


Warning: If UFW is disabled and you’re connected remotely via SSH, do not enable it without first ensuring your SSH access is permitted. Keep a second SSH session available during testing.
If your cloud provider or hosting platform uses additional firewalls, make sure UDP port 51820 is allowed there as well.
Confirm that:
- UDP 51820 is allowed.
- SSH access remains functional.
Step 6: Generate a Client Key Pair
Whenever possible, generate client keys directly on the client device.
Install WireGuard on the client:
sudo apt update
sudo apt install -y wireguard

Create a secure directory and generate keys:
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 the public key:
cat ~/.config/wireguard-raff/client.pub

Verify private key permissions:
stat -c '%a %n' ~/.config/wireguard-raff/client.key

Ensure that:
- The private key remains only on the client.
- Only the public key is copied to the server.
- Private key permissions are restrictive.
Step 7: Add the Client as a Peer
On the server, temporarily store the client’s public key:
CLIENT_PUBLIC_KEY='PASTE_CLIENT_PUBLIC_KEY_HERE'
Add the peer configuration:
sudo tee -a /etc/wireguard/wg0.conf >/dev/null <<EOF
[Peer]
PublicKey = ${CLIENT_PUBLIC_KEY}
AllowedIPs = 10.66.66.2/32
EOF
Clear the temporary variable:
unset CLIENT_PUBLIC_KEY

View the peer configuration:
sudo grep -A2 '^Peer' /etc/wireguard/wg0.conf

On the server:
- AllowedIPs = 10.66.66.2/32
This does two important things:
- Associates traffic destined for 10.66.66.2 with this client.
- Accepts packets only from that assigned VPN address.
Important: Never assign the same VPN address to multiple peers.
Confirm that:
- The correct public key is configured.
- Only one peer uses 10.66.66.2/32.
Step 8: Start WireGuard and Enable Auto-Start
Enable the WireGuard interface and start it immediately:
sudo systemctl enable --now wg-quick@wg0
![]()
Check the service status:
systemctl is-active wg-quick@wg0
systemctl is-enabled wg-quick@wg0

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

Ensure that:
- The service is active and enabled.
- Interface wg0 owns 10.66.66.1/24.
- WireGuard is listening on UDP 51820.
Step 9: Create the Client Configuration
Retrieve the server public key:
sudo cat /etc/wireguard/wg0.pub

Load the client private key:
CLIENT_PRIVATE_KEY="$(cat ~/.config/wireguard-raff/client.key)"
Create the client configuration, replacing the placeholders with your actual values:
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
Remove the variable and secure the file:
unset CLIENT_PRIVATE_KEY
sudo chmod 600 /etc/wireguard/raffvpn.conf

Understanding the Settings
- AllowedIPs = 10.66.66.0/24 creates a split-tunnel VPN.
- Only VPN subnet traffic uses WireGuard.
- Internet traffic continues using the normal network connection.
- PersistentKeepalive = 25 helps clients behind NAT maintain connectivity.
Ensure that:
- The client uses 10.66.66.2/32.
- The server endpoint is correct.
- Only VPN traffic is routed through WireGuard.
Step 10: Bring Up the Client Tunnel
Start the VPN connection on the client:
sudo wg-quick up raffvpn

Check the interface:
ip -brief addr show raffvpn
sudo wg show raffvpn
Verify VPN routing:
ip route get 10.66.66.1

Verify normal internet routing:
ip route get 1.1.1.1

Confirm that:
- Traffic to 10.66.66.1 uses raffvpn.
- Internet traffic still uses the existing default route.
Step 11: Verify Connectivity and Handshake
Generate traffic from the client:
ping -c 4 10.66.66.1

Check the VPN status on the client:
sudo wg show raffvpn
Check the VPN status on the server:
sudo wg show wg0

Monitor activity in real time:
watch -n 1 sudo wg show wg0

A healthy tunnel should show:
- A recent handshake
- Increasing transmit counters
- Increasing receive counters
Ensure that:
- The ping succeeds.
- Both peers report successful handshakes.
- Traffic counters increase during testing.
Step 12: Move SSH Access to the VPN Safely
Allow SSH access through WireGuard:
sudo ufw allow in on wg0 to any port 22 proto tcp \
comment 'SSH over WireGuard'
If SSH uses a custom port, replace 22 with the actual port number.
Test VPN-based SSH access:
ssh your-user@10.66.66.1

Important: Keep your existing public SSH access enabled until VPN-based access has been fully tested from a new session.
Confirm that:
- SSH access works through the VPN.
- Firewall rules only allow intended services.
Step 13: Manage Additional Peers
Assign a unique VPN IP address to each new peer:
10.66.66.2/32 laptop
10.66.66.3/32 workstation
10.66.66.4/32 phone
After adding a new peer to the configuration, reload WireGuard:
sudo systemctl reload wg-quick@wg0
Managing Peers
- Use a unique public key for every device.
- Use a unique /32 address for every peer.
- Remove a peer block and reload the service to revoke access.
- Generate a new key pair if credentials are compromised.
Optional preshared keys can be generated with:
wg genpsk
Ensure that:
- Each peer has a unique key pair.
- Revoked peers disappear from wg show.
- Existing peers remain connected after reload.
Step 14: Test Reboot Recovery and Cleanup
During a maintenance window, reboot the server:
sudo reboot
After reconnecting, verify the VPN service:
systemctl is-active wg-quick@wg0
ip -brief addr show wg0
sudo wg show wg0
sudo ss -lunp | grep ':51820' || true

Retest connectivity:
ping -c 4 10.66.66.1
sudo wg show raffvpn
To disconnect the client:
sudo wg-quick down raffvpn
To disable the server tunnel:
sudo systemctl disable --now wg-quick@wg0
Note: Only remove firewall rules after confirming that no clients rely on the VPN.
Confirm that:
- The WireGuard service starts automatically after a reboot.
- Handshakes and connectivity still work.
- The interface and listener are removed when intentionally disabled.
Conclusion
You have successfully deployed a WireGuard VPN on Ubuntu 24.04 using a secure split-tunnel configuration. The setup includes protected key management, firewall configuration, client and server peer setup, automatic startup with systemd, and verification procedures to confirm connectivity.
By using a dedicated VPN subnet, unique peer addresses, and restrictive key permissions, you maintain a secure and manageable WireGuard deployment that can easily scale as additional clients are added.



