What You Will Build
The server gets a WireGuard interface, wg0, with the tunnel address 10.8.0.1. Your computer, the peer, gets 10.8.0.2. Through the tunnel the peer reaches services on the server and, with forwarding, a private network behind it, here 10.10.0.0/24. The peer's other traffic keeps using its normal connection. Keep your current SSH session open through every step, and test firewall changes from a second session.
Step 1: Install WireGuard
sudo apt install wireguard # Ubuntu, Debian
sudo dnf install wireguard-tools # AlmaLinux, Rocky LinuxThe kernel module comes with the kernel on current releases; the package adds the wg and wg-quick tools. On AlmaLinux and Rocky Linux 9 and 10, wireguard-tools is in the standard AppStream repository, so EPEL is not needed. Install the tools on the peer too.
Step 2: Create the Key Pairs
sudo -i
cd /etc/wireguard
umask 077
wg genkey | tee server.key | wg pubkey > server.pubThis writes the server's private key to server.key, readable only by root, and its public key to server.pub. Create the peer's own pair on the peer the same way. Only public keys are exchanged; a private key never leaves the machine it was made on. In the files below, SERVER_
Step 3: Write the Server Configuration
Still as root, create /etc/wireguard/wg0.conf with mode 600:
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
[Peer]
PublicKey = PEER_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32On the server, AllowedIPs lists the tunnel addresses a peer may send from: one /32 per peer. 51820 is the usual port; any UDP port works. Type exit to leave the root shell.
Step 4: Open the UDP Port
Run sudo ufw status first: if UFW is inactive, allow your SSH port before you enable it. Then, on Ubuntu and Debian:
sudo ufw allow 51820/udpOn AlmaLinux and Rocky Linux:
sudo firewall-cmd --permanent --add-port=51820/udp
sudo firewall-cmd --reloadIf your provider runs a firewall in front of the server, open the port there too. See UFW allow port and firewalld basics.
Step 5: Forwarding and NAT
Skip this step if the peer only needs the server itself. To reach the private network behind it, turn on IP forwarding:
echo 'net.ipv4.ip_forward = 1' | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl --systemThen masquerade tunnel traffic as it leaves toward that network, so its hosts can answer without a route back to 10.8.0.0/24. With UFW, which brings iptables, add these lines to the [Interface] section, with eth1 replaced by the private network's interface:
PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth1 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth1 -j MASQUERADEUFW drops forwarded traffic by default; allow this one path with sudo ufw route allow in on wg0 out on eth1.
With firewalld on AlmaLinux and Rocky Linux, leave out the PostUp lines and use a policy, which allows and masquerades traffic from one zone to another. Put wg0 in the internal zone, and replace public below with the zone that firewall-cmd --get-zone-of-interface=eth1 prints:
sudo firewall-cmd --permanent --zone=internal --add-interface=wg0
sudo firewall-cmd --permanent --new-policy=wg-to-lan
sudo firewall-cmd --permanent --policy=wg-to-lan --add-ingress-zone=internal
sudo firewall-cmd --permanent --policy=wg-to-lan --add-egress-zone=public
sudo firewall-cmd --permanent --policy=wg-to-lan --set-target=ACCEPT
sudo firewall-cmd --permanent --policy=wg-to-lan --add-masquerade
sudo firewall-cmd --reloadNAT is explained in what is NAT.
Step 6: Start the Tunnel
sudo systemctl enable --now wg-quick@wg0
sudo wg showThe systemd unit is wg-quick followed by the interface name, so wg0.conf belongs to wg-quick@wg0, which now also starts at boot.
Step 7: Configure the Peer
[Interface]
Address = 10.8.0.2/24
PrivateKey = PEER_PRIVATE_KEY
[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = 203.0.113.10:51820
AllowedIPs = 10.8.0.0/24, 10.10.0.0/24
PersistentKeepalive = 25On the peer, AllowedIPs decides which destinations use the tunnel: here only the tunnel subnet and the private network. PersistentKeepalive keeps the tunnel open through NAT on the peer's side. Start it with sudo wg-quick up wg0 and run ping 10.8.0.1; sudo wg show on the server then lists a latest handshake for the peer.
Add Peers and Troubleshoot
Give each new peer its own key pair and tunnel address, add a [Peer] block to the server's file, and load it without dropping existing peers:
sudo bash -c 'wg syncconf wg0 <(wg-quick strip wg0)'- No handshake: the UDP port is closed somewhere, the Endpoint is wrong, or the public keys are swapped.
- Handshake, no traffic: AllowedIPs on one side does not include the address you are trying to reach.
- Server reachable, network not: check
sysctl net.ipv4.ip_and the NAT rule.forward
For a single service, an SSH tunnel may be enough.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations