Insights

Run a WireGuard server on a Raspberry Pi

Set up a Raspberry Pi as a WireGuard VPN server for one Linux laptop, then verify a fresh handshake and service access from outside your network.

Illustration of Raspberry Pi board with seated Ethernet.
On this page

Yes, you can run a WireGuard virtual private network (VPN) server on a Raspberry Pi. In this setup, the Pi listens for incoming connections and your laptop connects to it.

In short:

  • Host on the Pi. Give the Pi and laptop each other's public keys.
  • Check the public endpoint. Use a public address with a reachable User Datagram Protocol (UDP) port.
  • Test away from home. Look for a fresh handshake and a response from one service on the Pi.

What the Pi does in a WireGuard setup

WireGuard calls both devices peers, even though the Pi listens and the laptop starts the connection. That's the reverse of using the Pi to join an existing WireGuard server. You can use the tunnel to reach a service running on the Pi without making the service itself public.

What to check before you set it up

1. Check WireGuard support on both devices. Use Raspberry Pi OS on the Pi and Linux on the laptop. Both need working WireGuard kernel support and the WireGuard tools from their package repositories. Both also need systemd, which starts the tunnel in this setup.

2. Check the public address and incoming path. Use a reachable public Internet Protocol version 4 (IPv4) address. If it changes, use a hostname kept current by dynamic Domain Name System (DNS), which updates the address the hostname points to. If an upstream network blocks incoming UDP, a rule on your home router won't fix it.

3. Forward only the WireGuard port if the Pi is behind your router. Before changing a shared router, confirm you have permission and keep a way to undo the change. Forward UDP port 47111 to the Pi's stable local address. The laptop uses the public address or hostname, not that local address. Don't forward the service or administration ports for this setup.

How to set up the Pi and one other device

1. Use these two matching profiles. The example uses 10.100.0.0/24 for the tunnel. If that range overlaps with a network either device uses, choose an unused range and update both profiles. Replace the key placeholders with the keys you generate in the next step, and replace YOUR_PUBLIC_IPV4_OR_HOSTNAME with your reachable public address or hostname.

On the Raspberry Pi:

[Interface]
Address = 10.100.0.1/24
ListenPort = 47111
PrivateKey = PI_PRIVATE_KEY

[Peer]
PublicKey = LAPTOP_PUBLIC_KEY
AllowedIPs = 10.100.0.2/32

On the Linux laptop:

[Interface]
Address = 10.100.0.2/24
PrivateKey = LAPTOP_PRIVATE_KEY

[Peer]
PublicKey = PI_PUBLIC_KEY
AllowedIPs = 10.100.0.1/32
Endpoint = YOUR_PUBLIC_IPV4_OR_HOSTNAME:47111

AllowedIPs tells WireGuard which destinations to send to a peer and which source addresses to accept from it. It doesn't grant access to a service. The laptop sends only traffic for the Pi's tunnel address through WireGuard, not traffic for other home devices or the rest of the internet.

2. Generate a separate key pair on each device. On Raspberry Pi OS and on the Linux laptop, check that wg0.conf, privatekey and publickey don't already exist in /etc/wireguard before using these filenames. Run the first line to open an administrator shell, then run the remaining lines in that shell. umask 077 keeps the new files private to that account.

sudo -i
cd /etc/wireguard
umask 077
wg genkey | tee privatekey | wg pubkey > publickey

Keep each private key on the device that generated it. Copy only the public key to the other device.

3. Save each device's profile. On each device, stay in the same administrator shell and open the new configuration file. Paste that device's profile with all placeholders replaced, then save it.

nano /etc/wireguard/wg0.conf

4. Enable and start WireGuard. Run these commands from your normal shell on Raspberry Pi OS now. Use the same commands on the Linux laptop once it's on an outside connection, as described below.

sudo systemctl enable wg-quick@wg0.service
sudo systemctl daemon-reload
sudo systemctl start wg-quick@wg0

How to check the tunnel away from home

1. Try one known service from another network. Check that the service is running on the Pi and accepts connections at its tunnel address. Connect the laptop to a phone hotspot or another network outside your home, then start its tunnel. Open the service using its usual client and port, with 10.100.0.1 as the address.

2. Check WireGuard on the Pi right after the attempt. Run this on Raspberry Pi OS:

sudo wg

Look for wg0 listening on port 47111 and find the peer that matches the laptop's public key. The listening port shows that WireGuard is up locally. A fresh latest handshake for that peer shows that the two devices authenticated during your test. The test passes when you also get a response from the intended service.

3. Use the handshake to pick your next check. If there's no fresh handshake, check that wg0 is up on both devices. Then check the laptop's public endpoint, the UDP forwarding rule and the public key in each peer entry. If there's a fresh handshake but the service doesn't respond, check the tunnel addresses, AllowedIPs, the Pi's local firewall and the address and port the service listens on before changing the router rule.

Where doxx.net fits. doxx.net is a private network for people and agents that you can join using WireGuard instead of hosting your own public endpoint. If that's what you want, get started with doxx.net and create an account in the web portal with a Pro trial or eligible invite.

Ask an AI assistant

Share this article

Private Everywhere

Stop giving the internet everything

Keep your browsing, messages, files, and agents private.