Tutorial: Using Raspberry Pi as an OpenVPN Client Router with Ethernet Sharing

Tutorial: Using Raspberry Pi as an OpenVPN Client Router with Ethernet Sharing

Tutorial: Using Raspberry Pi as an OpenVPN Client Router with Ethernet Sharing

15447-Raspberry-Pi-4-Model-B-4-GB-04

This guide will show you how to configure your Raspberry Pi as an OpenVPN client router. The Raspberry Pi will connect to your home Wi-Fi for internet access, and its Ethernet port (eth0) will be configured to share the VPN connection with a connected device (e.g., a PC).

Prerequisites

  • Raspberry Pi 4 with Raspbian OS
  • OpenVPN Configuration File (.ovpn file) from your VPN provider
  • Wi-Fi Connection to your home network

Step 1: Connect Raspberry Pi to Your Home Wi-Fi

Configure Wi-Fi using raspi-config:

sudo raspi-config

Navigate to Network Options > Wi-Fi, select your network, and enter the password. Verify the Wi-Fi connection:

ip a show wlan0

Step 2: Install OpenVPN and Configure the Client

Update the system and install necessary packages:

sudo apt update
sudo apt install openvpn iptables-persistent dnsmasq

Add OpenVPN configuration:

sudo cp /path/to/your/config.ovpn /etc/openvpn/client.conf

Start and enable OpenVPN:

sudo systemctl start openvpn@client
sudo systemctl enable openvpn@client

Step 3: Configure Ethernet (eth0) as a VPN Network

Set a static IP for eth0:

sudo nano /etc/dhcpcd.conf

Add the following lines to assign a static IP for eth0:

interface eth0
static ip_address=192.168.100.1/24

Step 4: Set Up dnsmasq for DHCP on Ethernet (eth0)

Configure dnsmasq to act as a DHCP server on eth0:

sudo nano /etc/dnsmasq.conf

Add the following lines to assign IP addresses to devices on eth0:

interface=eth0
dhcp-range=192.168.100.10,192.168.100.50,255.255.255.0,24h

Step 5: Enable IP Forwarding and Configure iptables

Enable IP forwarding:

sudo sysctl -w net.ipv4.ip_forward=1

Make the change permanent by adding it to /etc/sysctl.conf:

net.ipv4.ip_forward=1

Configure iptables to route Ethernet traffic through the VPN:

sudo iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o tun0 -j ACCEPT
sudo iptables -A FORWARD -i tun0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT

Save the iptables rules:

sudo sh -c "iptables-save > /etc/iptables/rules.v4"

Step 6: Connect Your PC and Test the VPN Connection

Connect PC to the Raspberry Pi’s Ethernet port and verify VPN routing:

curl ifconfig.me

Troubleshooting Tips

  • No Internet on PC: Check your VPN connection on the Raspberry Pi and verify iptables settings.
  • Wi-Fi Connection Drops on Pi: Make sure wlan0 is connected and stable.

This setup allows you to connect a device to your Raspberry Pi's Ethernet port, routing its traffic through the VPN.

Comments

Popular posts from this blog

How to Use a Raspberry Pi 4 as a Firewall to Block Adult Content and Protect Your Family

Tutorial: Using Raspberry Pi Pico and HW-488 Infrared Obstacle Avoidance Sensor to Play/Pause a Video on Your PC via USB