Skip to content
GEEKnodes

Raspberry Pi

WireGuard VPN on Raspberry Pi With PiVPN: Access Home Remotely

Set up a WireGuard VPN on your Raspberry Pi with PiVPN: UDP port 51820, dynamic DNS, CG-NAT, phone clients via QR code and ad blocking with Pi-hole.

You want to reach your NAS, camera or home router while traveling, but you do not feel like opening one port per service. The clean solution is your own VPN: a single encrypted entry point that puts you on your network as if you were on the couch. In this guide you set up a WireGuard VPN on a Raspberry Pi with PiVPN, learn what to do if your IP changes and how to detect the big obstacle: CG-NAT.

In short

  • WireGuard is a modern, fast and simple VPN; on the Raspberry Pi it installs in minutes with PiVPN.
  • You only need to forward one UDP port (51820 by default) and a dynamic DNS name if your public IP changes.
  • If your provider uses CG-NAT, forwarding does not work: ask for a public IP or use another strategy.

What WireGuard is and why choose it

WireGuard is a VPN protocol that has been part of the Linux kernel since version 5.6. Compared with OpenVPN or IPsec it has three clear advantages:

  • Little code: much easier to audit and a smaller attack surface.
  • Fast: modern cryptography (Curve25519, ChaCha20-Poly1305), great even on a modest Raspberry Pi.
  • Silent: it does not answer packets not signed with a valid key. A port scan does not even see anything listening.

It works with key pairs, like SSH. The server knows each client’s public key and each client the server’s. No usernames or passwords to guess.

WireGuardOpenVPN
TransportUDP onlyUDP or TCP
Default port518201194
ConfigurationA short file per deviceCertificates and more options
Performance on Raspberry PiVery highFine, more CPU load
Network change on phoneReconnects instantlyUsually takes a few seconds

PiVPN: the installer and its current status

PiVPN is a script that installs and configures WireGuard or OpenVPN with a text wizard and adds a pivpn command to manage clients. It is worth knowing the project’s state: in 2024 its maintainer released version 4.6.0 announcing the end of active development. Fix releases have followed and the project remains in minimal maintenance mode: issues are fixed so it works on new Debian and Raspberry Pi OS versions, but do not expect new features.

In practice it is still the easiest way to start. And since it installs standard WireGuard, if PiVPN ever stops working, your VPN keeps working: configuration files are in /etc/wireguard/ and you can manage them by hand.

Before you start

  1. A Raspberry Pi with updated Raspberry Pi OS and SSH access.
  2. A static IP on the local network, so port forwarding always points to the same device.
  3. Access to the router settings.
  4. Know whether you have a real public IP (see below).
sudo apt update && sudo apt full-upgrade -y

Install WireGuard with PiVPN

  1. Run the installer. Review the script first if you want to know what it does, a good habit with any curl | bash:
curl -L https://install.pivpn.io | bash
  1. Choose WireGuard when asked for the protocol.
  2. Accept port 51820 or set another. Note it down: you will need it on the router.
  3. Choose the DNS clients will use. If you have Pi-hole on the same Raspberry, the wizard detects it and offers to use it.
  4. Say whether you will connect by public IP or DNS name. If your IP changes, choose DNS name and enter your dynamic domain.
  5. Accept unattended security updates and reboot when done.

Open the UDP port on the router

WireGuard needs whatever reaches your public IP on the chosen port to end up on the Raspberry. Create a forwarding rule on the router:

FieldValue
ProtocolUDP (not TCP)
External port51820
Internal IPThe Raspberry’s static IP, for example 192.168.1.20
Internal port51820
The phone connects over the internet to the router, which forwards UDP port 51820 to the Raspberry Pi; NAS and cameras stay behind the tunnel
A single open UDP port; everything else is reachable only through the tunnel.

It is the only port needed. Everything else (NAS, cameras, router panel) stays closed to the internet and reachable only through the tunnel.

Dynamic IP and dynamic DNS

Most home connections have a dynamic public IP: it can change when you reboot the router or when the provider decides. If the client points to an IP that is no longer yours, the VPN stops connecting.

The solution is a dynamic DNS (DDNS) service: it gives you a fixed name, like myhome.ddns-service.net, updated automatically when your IP changes. Two ways to keep it current:

  • From the router: many have a DDNS section with several providers. The most convenient.
  • From the Raspberry: with the provider’s client or ddclient, which checks the IP every few minutes.
sudo apt install ddclient

In the PiVPN wizard enter that name instead of the IP. If you already installed with the IP, edit the Endpoint line of new clients or run the setup again.

CG-NAT: the obstacle that blocks it all

Some providers, especially budget fiber and 4G/5G, share one public IP among many customers using CG-NAT. Your router gets an IP in 100.64.0.0/10 or a private one, and however many ports you open, outside connections never arrive.

With CG-NAT the provider shares one public IP among many routers with 100.64.x.x addresses and outside connections do not arrive
With CG-NAT your router has no public IP and outside connections do not arrive.

To check:

  1. Look at the WAN IP in the router panel.
  2. Compare it with what any “what is my IP” site shows.
  3. If they differ, or the router’s starts with 100.64 to 100.127, you are behind CG-NAT.

Options: ask the provider for a public IP (some give it free, others charge), use IPv6 if your provider offers it and the router lets you open the port, or use a solution that connects outward, such as an intermediate cloud server. Details are in the NAT guide linked at the end.

Add clients: phone with QR code

Each device is a client with its own keys. Create it like this:

pivpn add
pivpn add creates the client movil-ana, pivpn -qr shows a QR in the terminal and the phone app scans it to create the tunnel
One client per device: created on the Pi and imported on the phone with a QR.

It asks for a name (for example, movil-ana) and generates the file /home/user/configs/movil-ana.conf. For the phone, the QR is quickest:

pivpn -qr movil-ana
  1. Install the official WireGuard app on Android or iOS.
  2. Tap add tunnel and choose scan QR code.
  3. Point at the QR in the terminal and turn the tunnel on.
  4. Test on mobile data, not home Wi-Fi, or you will not know if it works from outside.

For a laptop, copy the .conf with scp and import it in the WireGuard app for Windows, macOS or Linux. Useful commands:

pivpn -l          # list clients
pivpn -c          # connected clients and last handshake
pivpn -r movil-ana # revoke a client (lost phone)

One client per device: if you lose the phone, you revoke only that one and the rest keep working.

Combine it with Pi-hole

If clients use the Raspberry’s tunnel IP as DNS, Pi-hole ad blocking follows you away from home, even on mobile data. If you installed Pi-hole after PiVPN, check the client’s DNS line and, in Pi-hole, the interface option: it must allow queries from the wg0 interface or all local network interfaces.

By default PiVPN sends all traffic through the VPN (AllowedIPs = 0.0.0.0/0, ::/0). If you only want to reach your network and let the rest go direct, change that line on the client to your local range and the VPN’s, for example 192.168.1.0/24, 10.6.0.0/24.

Common problems

SymptomLikely cause
Tunnel turns on but no dataPort forwarded as TCP instead of UDP, or to another IP
Works at home, not outsideYou are testing on your own Wi-Fi; use mobile data
Worked and fails days laterPublic IP changed and you do not use DDNS
Never connects from outsideProvider CG-NAT
Connects but names do not resolvePi-hole does not accept queries from wg0
You see the network but no internetIP forwarding disabled after changing config; check net.ipv4.ip_forward

And a classic: your home range matching the range of the network you are on (for example, both 192.168.1.0/24). The device does not know where to send traffic. If you travel a lot, use a less common range at home.

Accessibility preferences

Quick profiles

Text and reading

Text size
Default
Line height
Default
Letter spacing
Default
Word spacing
Default

Colour

Motion and orientation

Cursor