Home Securing Online Presence with PiVPN and WireGuard on Raspberry Pi

Securing Online Presence with PiVPN and WireGuard on Raspberry Pi

In today’s digital landscape, concerns over online privacy and security are growing. With the rise of data breaches and constant surveillance, protecting our online activities has become crucial. A Virtual Private Network (VPN) is a key tool in this scenario. It creates a private network from a public internet connection, hiding your IP address and keeping your online actions untraceable. Besides, it offers secure and encrypted connections, which is a level up from a typical secured Wi-Fi hotspot.

Today, we’ll look into PiVPN, a simple and effective VPN solution that can be installed on a Raspberry Pi, transforming it into a personal VPN server. This setup picks up from our previous post, where we installed Pi-hole on a Raspberry Pi to enhance DNS privacy. By having Pi-hole and PiVPN on the same Raspberry Pi, we are setting up a solid base for a private and secure browsing environment.

PiVPN provides two VPN protocol options: OpenVPN and WireGuard. While OpenVPN is a mature and robust VPN protocol, WireGuard is a newer entrant that brings along modern cryptographic protocols, better performance, and lower latency. Here are some advantages of WireGuard over OpenVPN:

  1. Performance: WireGuard operates in the kernel space, which makes it faster and more efficient compared to OpenVPN operating in user space.
  2. Cryptography: WireGuard uses state-of-the-art cryptography that is better and more secure than OpenVPN’s cryptographic protocols.
  3. Simplicity: WireGuard has a simpler codebase, which makes it easier to audit for security vulnerabilities.
  4. Connection times: WireGuard establishes connections faster than OpenVPN, which is especially noticeable on mobile devices.
  5. Roaming support: With WireGuard, your VPN connection remains intact even as your device switches networks (e.g., moving between Wi-Fi and cellular networks).

Given these benefits, we’ll be configuring WireGuard in our PiVPN setup to ensure a secure, fast, and reliable VPN connection.


PiVPN simplifies the installation process with a one-liner. This script updates and installs necessary packages, then launches an interactive installer.

curl -L https://install.pivpn.io | bash

PiVPN Installation Step 1

Just like Pi-hole, PiVPN requires a static IP. We’ll also choose to force IPv6 through the VPN.

PiVPN Installation Step 2 PiVPN Installation Step 3

I’m using DHCP reservation to ensure a static IP exclusive to this Raspberry Pi.

PiVPN Installation Step 4

Now we’ll select a user to hold the configurations.

PiVPN Installation Step 5 PiVPN Installation Step 6

We choose WireGuard over OpenVPN for the VPN protocol.

PiVPN Installation Step 7

We configure the WireGuard port. Though the default is 51820, I’ll change it to 51821 due to an existing PiVPN on port 51820.

PiVPN Installation Step 8 PiVPN Installation Step 9

Since Pi-hole and PiVPN are on the same server, PiVPN will use Pi-hole as the VPN’s DNS server, providing ad-blocking on the go.

PiVPN Installation Step 10

We can use either a public IP or a DNS entry for our VPN. I’ll choose the public IP option.

PiVPN Installation Step 11

The installation now generates server keys.

PiVPN Installation Step 12

It’s recommended to enable unattended upgrades for security patches and updates.

PiVPN Installation Step 13 PiVPN Installation Step 14

Congrats! With PiVPN installed, let’s reboot the server and set up WireGuard once it’s back online.

PiVPN Installation Step 15 PiVPN Installation Step 16 PiVPN Installation Step 17

Port Forwarding

While our server is rebooting, we can go ahead and add a Port Forwarding to our router. I have AT&T internet and the router is configured to assign the WAN IP to the LAN client. That means that my public IP is assigned directly to my UniFi Dream Machine, which I use to manage my network. You must add the Port Forwarding to the device that manages that, it can be your internet provider router, your firewall, etc.

As I am using UniFi Network, I have to go to Settings –> Security –> Port Forwarding and Create Entry. I will give it a name, enable on both WAN and WAN2 (backup internet), from any source. Port is 51821 for me, but it’s the port you configured WireGuard. Forward IP is the server we installed PiVPN IP. Forward Port also 51821 and both protocols. I will not enable logging.

Port Forwarding Step 1 Port Forwarding Step 2

Client Configuration

Now that our server is back online and we are connected again, let’s create a new client configuration. For that, run the following command:

pivpn add

The command will prompt you to enter a name for the client. Let’s call it cloudtutorial. It will then generate the necessary keys and configuration files, update the server configuration, and update the hosts file for Pi-hole to recognize the new client.

Enter a Name for the Client: cloudtutorial
::: Client Keys generated
::: Client config generated
::: Updated server config
::: Updated hosts file for Pi-hole
::: WireGuard reloaded
::: Done! cloudtutorial.conf successfully created!
::: cloudtutorial.conf was copied to /home/your_username/configs for easy transfer.
::: Please use this profile only on one device and create additional
::: profiles for other devices. You can also use pivpn -qr
::: to generate a QR Code you can scan with the mobile app.

Now, to generate a QR code for easy configuration on mobile devices, run the following command on your PiVPN server:

pivpn -qr


Download the WireGuard app from Google Play Store or Apple App Store. Open the app, tap on the ‘+’ sign, and select ‘Scan from QR code’. Scan the QR code displayed on your terminal to import the configuration.

WireGuard App

Once connected, you can verify the connected clients on your server by running the following command:

pivpn -c
::: Connected Clients List :::
Name               Remote IP                Virtual IP        Bytes Received      Bytes Sent      Last Seen
cloudtutorial      1.6KiB              284B            Oct 21 2023 - 08:18:05
::: Disabled clients :::

Now, when you open the Pi-hole Query Log page at, you will see that the VPN is using Pi-hole for DNS resolution, and in this example, it’s already blocking some Google Analytics URLs.

Pi-hole with PiVPN

Connecting to WireGuard

There are other ways to connect to WireGuard, for more information follow this link.

Wrapping Up

Through this guide, we’ve addressed the essential need for online privacy by setting up a VPN using PiVPN and WireGuard on our Raspberry Pi. This setup not only secures our online presence but also provides ad-blocking on the go with Pi-hole integration. Whether at home or on the move, having a private, ad-free, and secure connection is now within easy reach. This is a significant stride towards a safer online experience wherever we venture in the digital realm.

This post is licensed under CC BY 4.0 by the author.

Enhancing DNS Privacy through Pi-hole Deployment