Intro
This post will show my setup using PIA (Private Internet Access) with OpenVPN on a Linux machine. Specifically, where only certain applications will utilize the VPN and the rest of the traffic will go out the normal ISP's default route. It will also show how to access the PIA API via a shell script, to open a forwarding port for inbound traffic. Lastly, I will show how to take all of the OpenVPN and PIA information and feed it to programs like aria2c or curl. The examples below were done on Ubuntu 16.04.
Openvpn Pia Config
Packages and PIA Setup
OpenVPN password file
VPN Tunnel Network. 34,502 VPN and Proxy servers in 101 locations across 78 countries. All servers supports WireGuard®. PIA only uses bare-metal servers for.
- This is a simple script for DD-WRT. It replaces the Server IP/Name Textbox located in 'Services-VPN-OpenVPN Client' with a Pull-Down list of PIA's VPN servers. The currently active VPN server will be selected if it is on the list.
- The server list can also refer to multiple OpenVPN server daemons running on the same machine, each listening for connections on a different port, for example: remote smp-server1.mydomain 8000 remote smp-server1.mydomain 8001 remote smp-server2.mydomain 8000 remote smp-server2.mydomain 8001.
Pia Openvpn Config Files
Now that we have PIA login info lets make password file so we don't have to put in a password every time we start OpenVPN. We just need to make a file with the PIA username on one line and the PIA password on the second line. So just use you favorite text editor and do this. The file should be called 'pass' and put in the '/etc/openvpn/pia' directory. The scripts that are used later depend on this file being called 'pass' and put in this specific directory. An example of what the file looks like is below.
Change permission on this file so only root can read it
OpenVPN config file
This is the OpenVPN config file that works with PIA, and that also utilizes the scripts that will be talked about further down in the page. Use your favorite editor and copy and paste this text to a file called 'pia.conf' and put in the '/etc/openvpn/pia' directory.
Pia Openvpn Settings
OpenVPN route script
This is the script that the OpenVPN client will run at the end of startup. The magic happens in this script. Without this script OpenVPN will start the client and make the default route for the box the vpn connection. If you want that then go into the pia.conf file and comment out the 'script-security 2', 'route-noexec', and 'route up ..' lines, and just fire up the client 'sudo openvpn --config /etc/openvpn/pia/pia.conf' and your done.If you don't want the vpn to take over your default route then let's keep going. Now that you have left those lines in the pia.conf file, the following script will be run when the client starts, and it will set up a route that does not take over the default gateway, but just adds secondary vpn gateway for programs to use. Open your favorite text editor and copy in the script below into the file '/etc/openvpn/pia/openvpn-route.sh'.
Now run some final commands to get the script ready to work
PIA port forward script
The following script is run by the openvpn-route.sh script. It will contact a PIA server and tell it to open a port for incoming traffic on your vpn connection. This is so people on the internet can contact your machine through the vpn connection. Just a important note that currently only a certain list of PIA gateways support port forwarding. See the PIA support article on this for more info. Now, open your favorite text editor and copy in the script below into the file '/etc/openvpn/pia/pia_port_fw.sh'.
Starting OpenVPN
Finally we can start OpenVPN to connect with PIA. To do this run the the following command. It will keep the connection in the foreground so you can watch the output.
During startup the OpenVPN client and both of the scripts we made will report on the screen data about the connection and if there were any errors. The output will look like the following example.
Using the vpn connection
When the vpn started it dropped some files in /tmp. These files have the ip and port info we need to give to different programs when the startup. The scripts created the following files. Pia vpn on raspberry pi.
- /tmp/vpnip - ip address of the vpn
- /tmp/vpnportfw - incomming port being forwarded from the internet to the vpn
- /tmp/vpnint - interface of the vpn
Join the bavarian illuminati. Now you can use this info when you start certain programs. Here are some examples.
Final notes and warnings
If you start any programs and don't specifically bind them to the vpn interface or its ip address their connection will go out the default interface for the machine. Please remember this setup only sends specific traffic through the vpn so things like DNS requests still go through the non-vpn default gateway.
Remember only certain PIA gateways support port forwarding so if it is not working, try another PIA gateway. As of this writing, it seems that gateways that support port forwarding are ones not in the USA, like Toronto.
PIA has a Linux vpn client that you can download and use if you are into GUI's.
Pia Openvpn Config Files
Now that we have PIA login info lets make password file so we don't have to put in a password every time we start OpenVPN. We just need to make a file with the PIA username on one line and the PIA password on the second line. So just use you favorite text editor and do this. The file should be called 'pass' and put in the '/etc/openvpn/pia' directory. The scripts that are used later depend on this file being called 'pass' and put in this specific directory. An example of what the file looks like is below.
Change permission on this file so only root can read it
OpenVPN config file
This is the OpenVPN config file that works with PIA, and that also utilizes the scripts that will be talked about further down in the page. Use your favorite editor and copy and paste this text to a file called 'pia.conf' and put in the '/etc/openvpn/pia' directory.
Pia Openvpn Settings
OpenVPN route script
This is the script that the OpenVPN client will run at the end of startup. The magic happens in this script. Without this script OpenVPN will start the client and make the default route for the box the vpn connection. If you want that then go into the pia.conf file and comment out the 'script-security 2', 'route-noexec', and 'route up ..' lines, and just fire up the client 'sudo openvpn --config /etc/openvpn/pia/pia.conf' and your done.If you don't want the vpn to take over your default route then let's keep going. Now that you have left those lines in the pia.conf file, the following script will be run when the client starts, and it will set up a route that does not take over the default gateway, but just adds secondary vpn gateway for programs to use. Open your favorite text editor and copy in the script below into the file '/etc/openvpn/pia/openvpn-route.sh'.
Now run some final commands to get the script ready to work
PIA port forward script
The following script is run by the openvpn-route.sh script. It will contact a PIA server and tell it to open a port for incoming traffic on your vpn connection. This is so people on the internet can contact your machine through the vpn connection. Just a important note that currently only a certain list of PIA gateways support port forwarding. See the PIA support article on this for more info. Now, open your favorite text editor and copy in the script below into the file '/etc/openvpn/pia/pia_port_fw.sh'.
Starting OpenVPN
Finally we can start OpenVPN to connect with PIA. To do this run the the following command. It will keep the connection in the foreground so you can watch the output.
During startup the OpenVPN client and both of the scripts we made will report on the screen data about the connection and if there were any errors. The output will look like the following example.
Using the vpn connection
When the vpn started it dropped some files in /tmp. These files have the ip and port info we need to give to different programs when the startup. The scripts created the following files. Pia vpn on raspberry pi.
- /tmp/vpnip - ip address of the vpn
- /tmp/vpnportfw - incomming port being forwarded from the internet to the vpn
- /tmp/vpnint - interface of the vpn
Join the bavarian illuminati. Now you can use this info when you start certain programs. Here are some examples.
Final notes and warnings
If you start any programs and don't specifically bind them to the vpn interface or its ip address their connection will go out the default interface for the machine. Please remember this setup only sends specific traffic through the vpn so things like DNS requests still go through the non-vpn default gateway.
Remember only certain PIA gateways support port forwarding so if it is not working, try another PIA gateway. As of this writing, it seems that gateways that support port forwarding are ones not in the USA, like Toronto.
PIA has a Linux vpn client that you can download and use if you are into GUI's.