How to connect to PIA at start up?


I’m new here and have a question how to get this script working at startup. The script is my first ever so sorry if it’s not made right… At least I can get my IP changed to PIA’s IP in my OSMC when I go to terminal at the OSMC at startup and start this script. :slight_smile:

# This script kills openvpn and then opens it, checks the IP address before 
# and after the script has connected to Private Internet Access, you need to change
# the YOURPIALOCATION.ovpn to ie Germany.ovpn/UKLondon.ovpn/your preferred location.
# the last line of the script tests that tun0 is activated.
echo "Your IP-address at the moment: " 
sudo pkill openvpn
wget -qO- ; echo

echo "connecting to PIA please wait.."

sudo openvpn --cd /home/osmc/vpn-config --config YOURPIALOCATION.ovpn --auth-user-pass $

sleep 5

echo "Your IP-address now: "
wget -qO- ; echo
sleep 4

sudo ifconfig tun0
echo "if you see tun0 everything is ok and you can exit terminal

I have also tested the traceroute and the first line says (or and next line shows PIA’s address. I assume I’m not showing my own IP right? (Sorry, I’m so noob with OSMC)

I got my pia working following this post.

Hi the_bo,

thanks! I’ll try the last part of your instructions for the auto connect to PIA. I first tried to follow the Brian Hornsby blog and have tried it but the issue was that when I opened the OpenVPN app in OSMC the IP didn’t change. The OpenVPN app showed that I am connected to PIA but when I check my IP address it’s the same as without the PIA. Then I read from some other place that there might be OpenVPN already running and that’s why I kill OpenVPN first (and check my IP address which is the one when I check it from my laptop when it’s not connectet to PIA) and then start the OpenVPN and everything seems to work. I just would prefer to have autostart for the script I wrote. Now I need to manually exit from OSMC, go to terminal (pressing ESC when exiting) and then write to terminal ./myscriptname and when it’s finished I exit terminal.

The other question:

Do you think that if these both commands below show that my IP has changed and there’s tun0 everything is fine and I’m invisible?

wget -qO- ; echo


sudo ifconfig tun0

Thank you for the help so far!

Here’s the original discussion thread I used to write the script: Raspberry Pi issues [Solved] - Page 2 — PIA

check the post by “scopey”



I am surprised nobody mentioned flags for the openvpn command. It will
augment or override what is in the configuration file. It is a lot
easier than trying to alter all of the PIA config files. I am able to
put all my configs in any directory I want and select any PIA server
without altering any config files. I was able to get everything working
without the GUI, without changing configs, and without an resolv.conf
Here are the configuration steps I took to get it working without using the
GUI. If I get time I may write out detailed steps, but for now here is the quick version

  1. Create a directory in your home dir. I used ~/vpn (/home/pi/vpn) but you can put it anywhere you like.
  2. Extract all of the .ovpn files into that directory
  3. Copy or move ca.crt and crl.pem into the directory if you already installed them in /etc/openvpn
  4. Create the user.txt in the same directory. It should only have two lines. The first is your PIA username, the second is your PIA password
  5. Change user.txt to read only for your user id (chmod 400 user.txt)
  6. Make sure openvpn is not already running. You can do this with ps -ef | grep openvpn | grep -v grep If it is running, you need to stop it first. sudo pkill openvpn Several issues listed in this thread happened to me when I forgot to kill openvpn and started a second instance.
  7. In the directory you created in step 1, you should see the .opvn files, ca.crt, crl.pem and user.txt
    In this example, all my files are in /home/pi/vpn. To start the client issue the following command. If you are using a .ovpn file with a space in the name, make sure you put the \ in front of the space as I did below.
    sudo openvpn --cd /home/pi/vpn --config US\ East.ovpn --auth-user-pass user.txt --keepalive 10 60 --auth-nocache --daemon
    Hopefully it works for you. I did not have to modify any of the .ovpn files. For almost everything in the config file, there is also a command line switch.
    Good luck. If there is any interest, I’ll post more detailed instructions, but I hope this may help a few of you.