How to Host a VPN on a Raspberry Pi

Transforming a Raspberry Pi into a self-hosted Virtual Private Network (VPN) is an excellent, cost-effective way to secure your internet connection and access your home network remotely. This guide provides a straightforward overview of how to set up your own private VPN server using a Raspberry Pi and PiVPN, covering everything from the initial hardware preparation to connecting your remote devices safely.

Hardware and Software Requirements

Before beginning the installation, ensure you have the necessary components ready. A stable setup requires reliable hardware and an updated operating system to function efficiently as a 24/7 network server.

Step 1: Prepare the Raspberry Pi

Log into your Raspberry Pi via SSH or use a connected monitor and keyboard. Before installing any new software, it is vital to ensure the system repository and existing packages are completely up to date. Run the following commands in the terminal:

sudo apt update
sudo apt upgrade -y

It is also highly recommended to assign a static IP address to your Raspberry Pi through your router’s DHCP reservation settings. This ensures the Pi always retains the same local IP address on your home network.

Step 2: Install PiVPN

The most efficient way to host a VPN on a Raspberry Pi is by using PiVPN, an open-source script that automates the complex configuration process of enterprise-grade VPN protocols.

Launch the automated installer by executing this command:

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

An interactive configuration screen will appear to guide you through the setup.

Step 3: Configure Protocol and Security

During the PiVPN installation wizard, you will be prompted to make several key infrastructure choices:

Once the wizard finishes, restart your Raspberry Pi to apply all system changes.

Step 4: Configure Port Forwarding on Your Router

For your VPN server to receive incoming connections from the outside world, you must configure your home router to forward traffic to the Raspberry Pi.

  1. Access your router’s admin dashboard via a web browser.
  2. Locate the Port Forwarding or Virtual Server settings.
  3. Create a new rule forwarding the WireGuard port (Default: 51820) using the UDP protocol.
  4. Set the destination IP to the static local IP address of your Raspberry Pi.

Step 5: Create Profiles and Connect Devices

With the server running and the router configured, you can now generate credentials for your devices.

To create a new client profile, run the following command on your Pi:

pivpn add

Follow the prompts to name the profile (e.g., “Phone” or “Laptop”). To easily connect a smartphone, you can generate a scannable QR code by running:

pivpn -qr

Download the official WireGuard app on your smartphone or computer, scan the generated QR code or import the configuration file, and toggle the connection switch. Your traffic is now securely encrypted and routed directly through your home Raspberry Pi VPN server.