Network technology

Self-hosted VPN in practice: WireGuard from network planning to connectivity checks

A common self-hosted VPN mistake is copying a full-tunnel script and treating interface startup as deployment completion. First connect two authorized machines with WireGuard, then understand when routing, forwarding and NAT become necessary. This identifies the failing layer while preserving existing remote management access.

Contents

Plan a minimal lab that does not take over all traffic

Assume a supported Ubuntu server and a Linux client capable of running WireGuard. The server has a reachable public address or explicitly configured UDP forwarding. This example uses the virtual subnet 10.77.0.0/24, server address 10.77.0.1 and client address 10.77.0.2, with both interfaces named wg0。

Before deployment, run ip route to confirm the subnet does not overlap home, office or cloud private networks. If it does, replace the entire example subnet with a nonconflicting private range. The example's 203.0.113.10 is reserved for documentation and cannot be used directly. Replace it with the server's actual public address or resolvable hostname.

  1. ClientTunnel address 10.77.0.2, initiating contact with the server.
  2. Public UDPPort 51820 on the server's actual address.
  3. ServerTunnel address 10.77.0.1, initially offering local access only.
Stage one verifies the two tunnel endpoints only, without changing the default gateway or opening third-party private networks.

Keep an existing SSH session and cloud-console recovery access. Do not switch the default route on your only remote management connection. Record existing firewall rules, then make minimal changes.

Install the tools and generate separate keys on each device

WireGuard's official installation page lists platform-specific installation methods. Run the Ubuntu commands once on the server and once on the client. Generate keys in a directory accessible only to the current administrator, never a public web or synchronized shared directory.

sudo apt update
sudo apt install wireguard
umask 077
wg genkey > private.key
wg pubkey < private.key > public.key

Each machine retains its own private.key and exchanges only public.key. Public keys identify peers; private keys establish local identity. Pasting a public key into a private-key field will not create the expected connection, even if their lengths look similar. Phones and laptops should not share configurations; independent keys permit individual revocation.

Maintain a separate key-free device inventory with owner, purpose, tunnel address and expiry. A server's Peer list is technical configuration, not evidence that a laptop was returned or a member still requires access. An inventory makes revocation independent of administrator memory.

The private key in a configuration file is a sensitive credential. Mask it in screenshots, tickets and repositories. If lost or exposed, generate a new device key and update its peer, rather than merely renaming the device in an interface.

Server configuration: initially accept only this client's address

Create /etc/wireguard/wg0.conf and replace the two uppercase placeholders with single-line values from the corresponding key files. Placeholders are not working keys. This configuration has no NAT or forwarding scripts; it only allows access to the server's own tunnel address.

[Interface]
Address = 10.77.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY

[Peer]
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.77.0.2/32

On the server, AllowedIPs restricts the peer's permitted tunnel source address and selects the peer for traffic sent there. It is not a firewall rule granting access to all services. Give a second device another public key and independent address rather than copying the same /32。

Use sudo chmod 600 /etc/wireguard/wg0.conf to restrict file permissions. In cloud security groups and host firewalls, permit only the selected UDP listener and intended tunnel test traffic, retaining existing SSH and website rules. If behind a router, forwarding must use UDP and point to the correct internal address.

Client configuration: route only the lab subnet through the tunnel

On the Linux client, create a configuration with the same filename and replace keys and the Endpoint address. The client private key differs from the server's; the public-key field contains the server's public key.

[Interface]
Address = 10.77.0.2/32
PrivateKey = CLIENT_PRIVATE_KEY

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = 203.0.113.10:51820
AllowedIPs = 10.77.0.0/24

The official wg-quick manual describes route configuration from AllowedIPs. Since this example includes neither 0.0.0.0/0 nor ::/0, it does not route all ordinary internet traffic into the VPN or take over IPv6. It is suitable for initially testing access to the management host.

If a client behind NAT needs to receive peer-initiated traffic after long idle periods, add PersistentKeepalive = 25 to its Peer section. This maintains a NAT mapping; it does not increase speed or repair wrong keys or blocked UDP. Leave it disabled by default when unnecessary.

After startup, verify handshake, routing and application connectivity

After correcting file permissions, run the first two commands at both ends, generate client test traffic and inspect both endpoints. WireGuard stays quiet without traffic; interface startup does not guarantee an immediate handshake.

sudo wg-quick up wg0
ip addr show dev wg0
ping -c 3 10.77.0.1
sudo wg show wg0
ip route get 10.77.0.1

On the client, verify the route selects wg0 and check both endpoints' wg show for recent handshakes and changing send/receive counters. If the server rejects ICMP, ping failure alone does not establish tunnel failure. Test an authorized listening application port and correlate firewall counters.

Then access an actual internal service and check TLS certificates and account permissions. If the interface IP works but a webpage does not, the service may listen only on loopback or its port may be blocked. This is separate from key exchange. After the lab, run sudo wg-quick down wg0 to undo this interface and associated configuration.

Additional responsibilities for private-network or full-tunnel access

Access to a service on the VPN server itself does not require forwarding. Reaching hosts behind it requires planned IP forwarding, forwarding-chain rules and return routes: those hosts must know the path back to the client's tunnel subnet. Consider source NAT only if return routes cannot be configured, accepting its effect on source identification.

A full tunnel additionally requires default routes, egress NAT, DNS and IPv6 policies. Merely changing the client's AllowedIPs to all addresses does not complete deployment. The actual egress interface may be called ens3 or something else, and cloud security groups, nftables and UFW can apply simultaneously. Do not copy iptables scripts with hardcoded interface names.

Ubuntu's default-gateway example demonstrates the extra steps. Verify them individually in testing, especially DNS resolution and IPv6 egress, before opening access to production users.

Two deployment scenarios: status is not proof of a correct path

Scenario one: the interface is up but never handshakes

The administrator opened TCP 51820 in the cloud console, but WireGuard uses UDP. The client keeps trying and the server sees no expected traffic. Check the public endpoint, UDP security group and host rules, then both public keys. Do not disable the entire firewall to prove connectivity; that may expose database and management ports too.

Scenario two: the VPN server is reachable but the database host is not

The database is on another private host, while only the two-endpoint tunnel was configured. A handshake proves endpoint communication, but database replies may still use their original gateway. Add approved forwarding and return routes with port-level authorization. If only one backend is needed, narrow access is easier to maintain than opening a whole subnet.

Pre-handover checks and ongoing maintenance

  • Addresses and routes: no subnet conflicts, with an independent address and key per device.
  • Management access: retain out-of-band recovery and verify the original authorized login before and after changes.
  • Permissions: expose only required UDP and internal services; VPN access does not replace application authentication.
  • Lifecycle: test device revocation, configuration backup, restart recovery and update windows.
  • Evidence: retain test times, interface addresses, route results and redacted handshake details.

For automatic startup, finish the trial before enabling wg-quick@wg0 through the local service manager, then test a real reboot. Unverified persistent routes or firewall rules often disappear after maintenance. Have another administrator reproduce connection and revocation from the handover notes, proving the result does not rely on undocumented knowledge. 稳付pay provides this tutorial for site-owner learning; free VPN and CDN services remain in preparation and are not delivered by this configuration.

References and technical documentation

The official sources below support protocol and implementation checks. Code and scenarios illustrate technical concepts; validate your environment and production integration protocol separately.

  1. WireGuard official installation documentation
  2. WireGuard Quick Start
  3. WireGuard official wg-quick manual
  4. WireGuard official wg manual
  5. Ubuntu:Using the VPN as the default gateway

Keep improving your site

Check each step of networking, orders and funds notifications against your business scenario. Discuss payment integration and USDT monitoring through the contact page.

Contact 稳付pay ↗