Self-host a WireGuard VPN on a VPS in 10 minutes (Ubuntu 24.04 / Debian 12)

2026-11-27来源 dev.toEnglish

WireGuard is small, fast and built into the Linux kernel. A server for yourself, your family or a small team needs one config file, one firewall rule set and a few minutes. This guide sets up a full-tunnel VPN (all client traffic goes through the server) on a fresh Ubuntu 24.04 or Debian 12 VPS.

What it gives you: an encrypted link from your devices to a server you control, protection on public Wi-Fi, and a fixed exit IP address. What it does not give you: anonymity. You move trust from your local network to your server and whoever hosts it, and websites see your server's address.

What you need

  • A VPS with a public IPv4 address and root access. One vCPU and 1 GB of RAM handle a household easily; the bandwidth allowance is the number that matters.
  • SSH access, with key login and a basic firewall already in place (see the checklist in the previous article of this series if not).

Everything below runs as root (sudo -i).

1. Install the packages

apt update apt install -y wireguard iptables qrencode 
  • wireguard pulls in wireguard-tools (wg, wg-quick); the kernel module ships with both distributions.
  • iptables is used for NAT in the config. Minimal Debian 12 images may not include it.
  • qrencode turns a client config into a QR code for the phone apps.

2. Generate keys

cd /etc/wireguard umask 077 wg genkey | tee server.key | wg pubkey > server.pub wg genkey | tee phone.key | wg pubkey > phone.pub wg genkey | tee laptop.key | wg pubkey > laptop.pub 

umask 077 makes sure the private keys are readable only by root. Each side keeps its private key and gives its public key to the other side.

3. Find your public interface

ip route get 1.1.1.1 | awk '{for(i=1;i<=NF;i++) if($i=="dev") print $(i+1)}' 

Usually eth0, ens3 or enp1s0. The examples use eth0; replace it everywhere if yours differs.

4. Server config

cat > /etc/wireguard/wg0.conf <<EOF [Interface] Address = 10.8.0.1/24 ListenPort = 51820 PrivateKey = $(cat /etc/wireguard/server.key) PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE [Peer] # phone PublicKey = $(cat /etc/wireguard/phone.pub) AllowedIPs = 10.8.0.2/32 [Peer] # laptop PublicKey = $(cat /etc/wireguard/laptop.pub) AllowedIPs = 10.8.0.3/32 EOF chmod 600 /etc/wireguard/wg0.conf 

On the server, AllowedIPs for each peer is the single VPN address that peer may use. It works like a routing table and an access list at the same time.

5. Enable IP forwarding

echo "net.ipv4.ip_forward = 1" > /etc/sysctl.d/99-wireguard.conf sysctl --system | grep ip_forward 

6. Firewall: open the port and allow routing

If you use ufw, its default policy drops forwarded traffic. Instead of switching the whole forward policy to accept, allow exactly the path you need:

ufw allow 51820/udp ufw route allow in on wg0 out on eth0 ufw status verbose 

Make sure SSH is still allowed (ufw status should list your SSH port) before you do anything else with the firewall.

If you do not use ufw and your FORWARD policy is already ACCEPT, the NAT rule from PostUp is all you need.

7. Start the tunnel

systemctl enable --now wg-quick@wg0 wg show 

wg show lists the interface and both peers. Peers show no handshake yet; that comes when a client connects.

8. Client configs

Create one file per device. The client's AllowedIPs = 0.0.0.0/0, ::/0 sends all traffic through the tunnel. Including ::/0 also captures IPv6, so if your server has no IPv6 routing set up, IPv6 traffic simply does not leave the device instead of bypassing the VPN.

SERVER_IP=$(curl -4 -s https://ifconfig.me) cat > /root/phone.conf <<EOF [Interface] PrivateKey = $(cat /etc/wireguard/phone.key) Address = 10.8.0.2/32 DNS = 1.1.1.1, 9.9.9.9 [Peer] PublicKey = $(cat /etc/wireguard/server.pub) Endpoint = ${SERVER_IP}:51820 AllowedIPs = 0.0.0.0/0, ::/0 PersistentKeepalive = 25 EOF qrencode -t ansiutf8 < /root/phone.conf 

Scan the QR code with the official WireGuard app (Android or iOS). For the laptop, build laptop.conf the same way with 10.8.0.3/32 and the laptop key, copy it over with scp, and import it into the desktop app or run wg-quick up ./laptop.conf on Linux.

PersistentKeepalive = 25 sends a tiny packet every 25 seconds so that home routers and mobile networks do not drop the idle UDP mapping.

Pick DNS resolvers you trust, or run your own resolver on the server and point clients to 10.8.0.1.

9. Test

On the client, connect, then:

curl -4 https://ifconfig.me # should print the server IP 

On the server:

wg show 

A recent latest handshake and growing transfer counters mean it works.

Add a peer without dropping anyone

cd /etc/wireguard && umask 077 wg genkey | tee tablet.key | wg pubkey > tablet.pub cat >> wg0.conf <<EOF [Peer] # tablet PublicKey = $(cat tablet.pub) AllowedIPs = 10.8.0.4/32 EOF wg syncconf wg0 <(wg-quick strip wg0) 

wg syncconf applies the new peer list to the running interface; existing sessions stay up. systemctl restart wg-quick@wg0 also works but briefly disconnects everyone.

To remove a device, delete its [Peer] block and run the same syncconf line. Then delete its key files.

Troubleshooting

Symptom Likely cause
No handshake at all UDP 51820 blocked (ufw, the network you are on), wrong Endpoint, or swapped public keys
Handshake but no internet ip_forward off, missing ufw route allow, or wrong interface name in PostUp
Works on Wi-Fi, not on mobile Missing PersistentKeepalive, or the mobile network filters UDP
Some sites hang MTU; try MTU = 1380 in the client [Interface]

Useful commands:

journalctl -u wg-quick@wg0 -n 50 iptables -t nat -S POSTROUTING sysctl net.ipv4.ip_forward tcpdump -ni eth0 udp port 51820 # do packets arrive at all? 

Hardening notes

  • Keep /etc/wireguard root-only; anyone with a client private key can use your server.
  • One key pair per device, never shared, so you can revoke one without touching the others.
  • Keep the system updated (unattended-upgrades) and SSH key-only.
  • If plain WireGuard is filtered on a network you need, look at obfuscated variants such as AmneziaWG or at Xray with REALITY.

Where to run it

I work with PhantomVPS; our VPN server hosting page covers plan sizing and links step-by-step guides for WireGuard, AmneziaWG and Xray. Fast VPS plans start at $7.99 a month with root access, sign-up needs an email address and a password (no ID documents, no phone number), and you can pay with Monero, Bitcoin, Litecoin, USDT, USDC, SOL or TRX. You can ping the test IPs linked there before you order to compare latency from where you are. The setup above works on any Ubuntu or Debian VPS.

原文:https://www.a8q9.cn/en/article/self-host-a-wireguard-vpn-on-a-vps-in-10-minutes-ubuntu-24-04-debian-12-e580b2.html

相关文章