Setup OpenVPN on Raspbian

Avatar
Jul 21, 2020
Image

Install Raspbian

Download raspbian here.

Enable SSH on first boot


To enable SSH on boot, make a file with the name ssh and no extension and place it in the boot partition. Now we can just use the Pi headless without needing a monitor and keyboard.

Install OpenVPN

  1. Update the Raspberry Pi
sudo apt-get update
sudo apt-get upgrade
  1. Install OpenVPN
sudo apt-get install openvpn

Configure OpenVPN


Download a .ovpn file, this is a configuration file for a specific vpn connection. I am using Windscribe.

Copy this file over to the following location

/etc/openvpn/client.conf

Now make a new file to save the credentials in.

$ sudo nano /etc/openvpn/cred.txt

Fill in the credentials in this file as follows

username
password

Now we need to make a few adjustments to client.conf

auth-user-pass "/etc/openvpn/cred.txt"
auth-nocache

script-security 2
up "/etc/openvpn/up.sh"
down "/etc/openvpn/down.sh"

There are two scripts that are called:

  • DOWN if the VPN loses the connection
  • UP when the VPN is reconnected

We are going to use this to let our torrent program stop/continue downloading based on the state of the VPN.

$ nano /etc/openvpn/up.sh

#!/bin/sh

echo "Starting Transmission Torrent Downloading"
transmission-remote --auth username:password --torrent all --start
$ nano /etc/openvpn/down.sh

#!/bin/sh

echo "Starting Transmission Torrent Downloading"
transmission-remote --auth username:password --torrent all --stop

We also need to give the proper permissions to this file

sudo chmod +x up.sh down.sh

Conclusion

In this article we have installed and configured OpenVPN on a Raspberry Pi. We have also written scripts that are executed when the status of the VPN connection changes.

Thanks for reading! If you have found a mistake, want to ask a question or have a comment, please send me an email.

arrow-up icon