Skip to content

Instantly share code, notes, and snippets.

@DinkDonk
Last active January 16, 2024 00:22
Show Gist options
  • Save DinkDonk/5c3d30d5789398a3d203 to your computer and use it in GitHub Desktop.
Save DinkDonk/5c3d30d5789398a3d203 to your computer and use it in GitHub Desktop.
Raspberrypi (raspbian) VPN passthrough.

This will set up a passthrough between the eth0 interface and the wan0 interface, with a VPN connection in-between.
You can then connect a machine to the ethernet port of the raspberrypi, and that machines network traffic will pass through the vpn tunnel on the raspberrypi.

The device connected to the ethernet plug of the PI should be set up with:

IP: 192.168.0.2
Mask: 255.255.255.0
Gateway: 192.168.0.1

Set up interfaces

$ sudo nano /etc/network/interfaces
auto lo

iface lo inet loopback
iface eth0 inet static
    address 192.168.0.1
    netmask 255.255.255.0

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-ssid "<insert_wpa_id>"
wpa-psk "<insert_wpa_password>"
wireless-power off

Restart wifi

$ sudo ifdown wlan0
$ sudo ifup wlan0

Set up VPN

$ sudo apt-get install openvpn

Configure the VPN with your providers settings.

Set up routing

Enable routing

$ echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward

Save the routing change

$ sudo nano /etc/sysctl.conf

In this file, uncomment the line:

net.ipv4.ip_forward=1

Set up masquerade

$ 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 iptables config for next reboot

$ sudo apt-get install iptables-persistent

$ sudo sh -c "iptables-save > /etc/iptables/rules.v4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment