WireGuard’s inclusion in Ubuntu 24.04’s default repositories means you can deploy a modern, high-speed VPN without additional kernel modules or complex dependencies. Its streamlined architecture and use of advanced cryptography make it a preferred choice for both individual users and organizations seeking reliable, low-overhead VPN connectivity. The following steps outline the most robust approach to installing and configuring WireGuard on Ubuntu 24.04, from server setup to client connection and firewall integration.
Install and Configure WireGuard VPN (Recommended Method)
sudo apt update && sudo apt upgrade -y
sudo apt install wireguard wireguard-tools -y
umask first to protect private keys:umask 077
sudo wg genkey | sudo tee /etc/wireguard/server_private.key | wg pubkey | sudo tee /etc/wireguard/server_public.key
This command creates /etc/wireguard/server_private.key and /etc/wireguard/server_public.key. The private key must remain secret. Adjust permissions to restrict access:
sudo chmod 600 /etc/wireguard/server_private.key
ip a
Typically, the main interface is named enp1s0, eth0, or similar. Note the interface name and public IP address for later use.
/etc/wireguard/wg0.conf with your preferred text editor and add the following, replacing placeholders with your actual private key and interface name:[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey =
SaveConfig = true
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o -j MASQUERADE
Explanation: Address assigns a private subnet for VPN clients. ListenPort sets the UDP port (default is 51820). PrivateKey is the server’s private key. PostUp and PostDown add/remove firewall and NAT rules automatically when the interface starts or stops, ensuring VPN traffic is routed through the correct interface and clients can access the internet.
/etc/sysctl.conf:net.ipv4.ip_forward = 1
Apply the change immediately:
sudo sysctl -p
sudo ufw allow 51820/udp
sudo ufw reload
If UFW is not yet enabled, enable it after allowing SSH (port 22) to prevent lockout:
sudo ufw allow 22/tcp
sudo ufw enable
sudo systemctl start wg-quick@wg0
sudo systemctl enable wg-quick@wg0
Check the status to confirm it’s running:
sudo systemctl status wg-quick@wg0
To view the active VPN interface and peers, run:
sudo wg show wg0
Join readers who trust AllThings.How
Add us as a preferred source on Google so our practical guides show up first next time you search.
Add to Google Preferences →Configure WireGuard Clients
wg genkey | tee client_private.key | wg pubkey > client_public.key
Keep client_private.key secure. You will need client_public.key for the server configuration.
/etc/wireguard/wg0.conf (or client1.conf) and add:[Interface]
PrivateKey =
Address = 10.8.0.2/24
DNS = 8.8.8.8
[Peer]
PublicKey =
AllowedIPs = 0.0.0.0/0
Endpoint = :51820
PersistentKeepalive = 25
Explanation: PrivateKey is the client’s private key. Address assigns a unique private IP within the VPN subnet. DNS sets the resolver used when the tunnel is active. PublicKey is the server’s public key. AllowedIPs controls which traffic routes through the VPN (use 0.0.0.0/0 for all traffic). Endpoint is the server’s public IP and port. PersistentKeepalive helps maintain NAT traversal for clients behind firewalls.
[Peer] section to /etc/wireguard/wg0.conf on the server for each client:[Peer]
PublicKey =
AllowedIPs = 10.8.0.2/32
Restart the WireGuard service to apply changes:
sudo systemctl restart wg-quick@wg0
scp or a secure file-sharing method). On the client, bring up the VPN:sudo wg-quick up wg0
To stop the VPN, use:
sudo wg-quick down wg0
ping -c 3 10.8.0.1
If successful, the VPN tunnel is operational. You can also check your public IP address (for example, with curl https://icanhazip.com) to confirm that traffic is routed through the VPN server.
Alternate Configuration: Using UFW for NAT and Routing
Some users prefer managing NAT and forwarding rules with UFW rather than iptables commands in PostUp and PostDown. To set up NAT with UFW:
/etc/ufw/before.rules and add the following lines at the top, replacing enp1s0 with your main network interface and 10.8.0.0/24 with your VPN subnet:*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 10.8.0.0/24 -o enp1s0 -j MASQUERADE
COMMIT
-A ufw-before-forward -s 10.8.0.0/24 -j ACCEPT
-A ufw-before-forward -d 10.8.0.0/24 -j ACCEPT
Save and restart UFW:
sudo systemctl restart ufw
This approach centralizes firewall management but requires careful editing to avoid syntax errors.
WireGuard’s efficient protocol and straightforward configuration make it a practical solution for secure remote access and site-to-site networking on Ubuntu 24.04. Regularly update your system and WireGuard packages to maintain optimal security and performance.






