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.
| WireGuard | OpenVPN | |
|---|---|---|
| Transport | UDP only | UDP or TCP |
| Default port | 51820 | 1194 |
| Configuration | A short file per device | Certificates and more options |
| Performance on Raspberry Pi | Very high | Fine, more CPU load |
| Network change on phone | Reconnects instantly | Usually 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
- A Raspberry Pi with updated Raspberry Pi OS and SSH access.
- A static IP on the local network, so port forwarding always points to the same device.
- Access to the router settings.
- Know whether you have a real public IP (see below).
sudo apt update && sudo apt full-upgrade -y
Install WireGuard with PiVPN
- 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
- Choose WireGuard when asked for the protocol.
- Accept port 51820 or set another. Note it down: you will need it on the router.
- Choose the DNS clients will use. If you have Pi-hole on the same Raspberry, the wizard detects it and offers to use it.
- Say whether you will connect by public IP or DNS name. If your IP changes, choose DNS name and enter your dynamic domain.
- 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:
| Field | Value |
|---|---|
| Protocol | UDP (not TCP) |
| External port | 51820 |
| Internal IP | The Raspberry’s static IP, for example 192.168.1.20 |
| Internal port | 51820 |

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.

To check:
- Look at the WAN IP in the router panel.
- Compare it with what any “what is my IP” site shows.
- If they differ, or the router’s starts with
100.64to100.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

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
- Install the official WireGuard app on Android or iOS.
- Tap add tunnel and choose scan QR code.
- Point at the QR in the terminal and turn the tunnel on.
- 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
| Symptom | Likely cause |
|---|---|
| Tunnel turns on but no data | Port forwarded as TCP instead of UDP, or to another IP |
| Works at home, not outside | You are testing on your own Wi-Fi; use mobile data |
| Worked and fails days later | Public IP changed and you do not use DDNS |
| Never connects from outside | Provider CG-NAT |
| Connects but names do not resolve | Pi-hole does not accept queries from wg0 |
| You see the network but no internet | IP 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.



