Configure OpenVPN on CentOS using PrivateInternetAccess

I recently decided to start protecting myself against the evil spying of the US Government as well as all of the DMCA hounds that are crawling the internet. I have known about VPN’s for quite some time, but I honestly never thought about using one. With all the leaks that Edward Snowden has been publishing, it got me thinking about my own online privacy, and how to best protect myself.

A trusted friend of mine has been using a VPN for several months now, and he gave me a really great recommendation on a VPN provider. PrivateInternetAccess.com is what he recommended to me, and it was a superb choice. Here is their webpage if you would like to check them out: https://www.privateinternetaccess.com/ They offer a really great and really fast VPN service for about $40 per year (at the time of this writing). They also accept Bitcoin as a payment method, which is great for remaining anonymous. Another reason to choose PIA (as they are often times referred to) is because they offer several egress point all over the Globe. Their service is fast as well. I am routinely seeing 20 to 30 meg speeds when using the US East coast egress points.

Here is a Speedtest.net graph to show a recent test:

 

Now, let’s get started on getting this setup. I will provide step-by-step instructions below, and even provide some pre-built OpenVPN configuration files to make it even easier.

First you will need to signup with PrivateInternetAccess. Follow this link to signup and create your account: https://www.privateinternetaccess.com/pages/buy-vpn/ Once you get setup, they will email you with your credentials to use for the service. DON’T LOOSE THESE!

Then, you will need to perform the following to get CentOS or RHEL setup for the service.

  1. Install OpenVPN on your machine. I am using CentOS 6.5 64 bit, but the instructions will be similar on other distros.

    sudo yum install openvpn

  2. Create a password file that contains your PIA credentials so that OpenVPN can use them to connect. These credentials are the ones the PIA emails to you. This will keep OpenVPN from prompting you for your VPN password when it connects. Set the permissions to 400 and give ownership to root.

     

    echo “YOUR PIA USERNAME” > ~/.pia

    echo “YOUR PIA PASSWORD” >> ~/.pia

    sudo chown root:root ~/.pia && sudo chmod 400 ~/.pia

    sudo mv ~/.pia /root/

     

  3. Download the PIA configuration files. I have created the OpenVPN configuration files that will work with PIA already and included them in the command below. These config files are setup for all locations that PIA is currently offering, and I will do my best to keep them up to date.

    wget http://www.gigahype.com/wp-content/uploads/2014/01/openvpn_configs.tar

  4. Untar the config files and move them to the proper OpenVPN directory.

    tar –xvf openvpn_configs.tar

    sudo mv openvpn /etc/

  5. Now, we need to create a server.conf file so that OpenVPN will know which of our PIA configs to look to. We do this by creating a symlink to whichever PIA config file we want to use. It’s important to note that each of the PIA config files are designed for a certain region. This allows you to pick your egress point by country, state, or larger region. We will do an ls to list the files in the directory so you may choose whichever region you want to use.

    cd /etc/openvpn

    ls –l

    (You can now see all of the regions that I have configured. Pick which one you want to use for the next step)

    sudo ln -s /etc/openvpn/PICKYOURS.ovpn /etc/openvpn/server.conf

  6. If you use Google DNS, OpenDNS, or even your ISP’s DNS servers you expose yourself to DNS Leakage. DNS leakage occurs because the DNS providers are able to keep a record of all the sites that your computer requests IP’s for. I highly recommend you google search the term “DNS Leakage” to get a better idea of it. If you want to mitigate DNS Leakage, PIA provides secure and private DNS servers. This step is totally optional, but I highly recommend doing this if you want to increase your security and anonymity. PIA’s DNS servers are: 209.222.18.222 and 209.222.18.218 I will outline outline how to modify your DNS servers below:

     

    sudo cp /etc/resolv.conf ~/resolv.conf.backup

    sudo echo “nameserver 209.222.18.222″ > /etc/resolv.conf

    sudo echo “nameserver 209.222.18.218″ >> /etc/resolv.conf

     

  7. Now we will start the OpenVPN service and then make sure it is connecting to PIA and creating the proper VPN network adapter tun0

    sudo service openvpn start

    ifconfig tun0

    (the ifconfig tun0 command should return the information about the VPN network. It will look something similar to the picture below)

  8. If you would like to have OpenVPN always start at boot, you can do the following command. This is an optional step.

    sudo chkconfig openvpn on

 

That’s it! If everything went well, you know have OpenVPN fully configured on a headless CentOS machine and have set it up to connect automatically at boot. You can always double check to make sure it is working by looking for the tun0 adapter. If you have access to launch a web browser on the machine, you can point it to www.ipchicken.com and it will show you the external IP address you connected from. If you are going through the VPN adapter, it will be a different IP address than your ISP.

Please see my post on how to configure Transmission Bittorrent client to always send its traffic through the VPN connection to avoid being snooped on. You can find it here:

 

 

 

 

 

5 Comments

  1. Jeremy February 25, 2014 12:44 pm  Reply

    Thanks for the great walk through. I was working through it and this save me hours of troubleshooting.

  2. Andy K March 9, 2014 9:36 pm  Reply

    Awesome write up! Just what I needed for my personal CentOS server and my existing account with PIA. Things were looking good, but the tun0 interface was not created after starting the service (the OpenVPN started with an [OK]). Any ideas?

  3. Victoria May 6, 2014 12:33 am  Reply

    Thanks for the guide.
    Have done everything exactly. Got error – cannot access the password file. Moved password file into /etc/openvpn – everything is working now.
    Now I wonder, what I did wrong, why the service cannot access the password file in /root directory.

    I have another issue – when I switch resolv.conf, everything works until the connection break. After that openvpn cannot restore it, because it cannot resolve the address of the openvpn server. Switching back resolv.cong helps, but I would like to set up robust and unattended system.
    Would appreciate any advice.

    Thank you again.

Leave a Reply