VPN Client Setup on Raspberry Pi (OpenVPN & WireGuard)
This guide covers setting up VPN clients on Raspberry Pi to create secure remote access from anywhere. You’ll learn how to configure both OpenVPN and WireGuard, including automatic reconnection and network routing.
Prerequisites
- Raspberry Pi with Raspbian/Raspberry Pi OS installed
- SSH or direct access to the Pi
- VPN server configuration file (.ovpn for OpenVPN or .conf for WireGuard)
- Basic command line knowledge
Option 1: OpenVPN Client Setup
Installation
Install the OpenVPN client:
1 | sudo apt-get update |
Configuration
Create the client configuration directory and add your VPN configuration file:
1 | # Create directory if it doesn't exist |
Paste your VPN provider’s configuration into this file. Typical configuration includes server address, port, certificates, and authentication details.
Start and Enable Service
Start the OpenVPN client and enable it to run on boot:
1 | # Start the service |
Note: The service name openvpn-client@client.service corresponds to the config file /etc/openvpn/client/client.ovpn. If your config file has a different name (e.g., myvpn.ovpn), use openvpn-client@myvpn.service.
Verify Connection
Check your public IP to confirm the VPN is working:
1 | curl ifconfig.me |
The IP address should match your VPN server’s location, not your actual location.
Automatic Reconnection
To ensure the VPN automatically reconnects if the connection drops, add these parameters to your configuration file:
1 | sudo nano /etc/openvpn/client/client.ovpn |
Add at the end of the file:
1 | # Keepalive: ping every 10 seconds, restart if no response for 60 seconds |
Restart the service to apply changes:
1 | sudo systemctl restart openvpn-client@client.service |
Option 2: WireGuard Client Setup
WireGuard is a modern, lightweight VPN protocol with better performance and simpler configuration than OpenVPN.
Installation
Install WireGuard:
1 | sudo apt update |
Configuration
Create and configure your WireGuard interface:
1 | # Create configuration file |
Add your WireGuard configuration (provided by your VPN server):
1 | [Interface] |
Set proper permissions:
1 | sudo chmod 600 /etc/wireguard/wg0.conf |
Start and Enable Service
Start WireGuard and enable it on boot:
1 | # Start the VPN |
Enable IP Forwarding (for routing traffic)
If you want your Raspberry Pi to route traffic through the VPN:
1 | # Enable IP forwarding |
Configure iptables for NAT
Set up Network Address Translation (NAT) to route traffic through the VPN:
1 | # Allow forwarding from ethernet to VPN |
Make iptables rules persistent across reboots:
1 | sudo apt install iptables-persistent -y |
During installation, choose “Yes” to save current IPv4 and IPv6 rules.
To save rules later:
1 | sudo netfilter-persistent save |
Network Configuration
Configure Static IP Address
For reliable VPN routing, set a static IP address for your Raspberry Pi:
1 | sudo nano /etc/dhcpcd.conf |
Add at the end of the file (adjust values for your network):
1 | # Static IP configuration |
Replace with your network values:
192.168.1.100- Desired static IP for your Pi192.168.1.1- Your router’s IP (gateway)8.8.8.8- Secondary DNS server (Google DNS)
Restart networking:
1 | sudo systemctl restart dhcpcd |
Troubleshooting
Check VPN Status
For OpenVPN:
1 | sudo systemctl status openvpn-client@client.service |
For WireGuard:
1 | sudo wg show |
Test Connectivity
1 | # Check if VPN interface exists |
Common Issues
OpenVPN won’t start:
- Check configuration file syntax:
sudo openvpn --config /etc/openvpn/client/client.ovpn - Verify certificates and keys are correct
- Check firewall settings
WireGuard connection drops:
- Add
PersistentKeepalive = 25to [Peer] section - Check if the server endpoint is reachable
- Verify firewall allows UDP traffic on WireGuard port
No internet after connecting:
- Verify DNS settings in VPN config
- Check if
AllowedIPs = 0.0.0.0/0for full tunnel - Test with:
nslookup google.com