English
简体中文繁體中文EnglishРусскийفارسی

Home › Guides › The first tunnel

The first tunnel

Build a first WireGuard tunnel with a VPS and a PC: keys, two config files, the UDP port, starting it, and checking with wg show and ping.

The first tunnel: between two machines
The first tunnel: between two machines Matija Podhraški, CC BY-SA 3.0

You need

  • A Linux server with a public IP and SSH.
  • Your own computer with WireGuard.
  • Fifteen minutes.

A pair of keys on each side

Server
sudo apt install wireguard
cd /etc/wireguard && umask 077
wg genkey | tee server.key | wg pubkey > server.pub
cat server.key server.pub

On the PC choose Add empty tunnel; the window shows a pair. One rule: your private key goes into your [Interface], your public key into the other side's [Peer].

Server config

/etc/wireguard/wg0.conf
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY

[Peer]
PublicKey = PC_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32

PC config

Paste into the new tunnel
[Interface]
PrivateKey = PC_PRIVATE_KEY
Address = 10.8.0.2/32

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = SERVER_IP:51820
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 25

Open the port and start

Server
sudo ufw allow 51820/udp
sudo systemctl enable --now wg-quick@wg0

Cloud servers also need UDP 51820 in the security group; many people open TCP by mistake. Then activate the tunnel on the PC.

Check

  1. Handshakesudo wg show on the server prints latest handshake
  2. Pingping 10.8.0.1 from the PC answers
  3. Traffictransfer is above 0 both ways
All three mean it works
Must it be 51820?

No. Any UDP port, the same on both sides.

Too much typing?

The generator writes all files at once, with keys made in your browser.

Next letterPhone by QR

On the same round

Postcodes: WireGuard server setup, WireGuard on a VPS, WireGuard config example